Digital Image Proceesing Lab manual, Exercises of Digital Image Processing

that manual will help you to understand of DIP some filters

Typology: Exercises

2017/2018

Uploaded on 12/02/2018

wardah
wardah 🇵🇰

1 document

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan
Session:-2015 Computer
Digital Image Processing
Lab Manual No 10
Image Segmentation
Course Instructor:
Dr. M. Haroon Yousaf
Semester:
Autumn 2018
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Digital Image Proceesing Lab manual and more Exercises Digital Image Processing in PDF only on Docsity!

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

Digital Image Processing

Lab Manual No 10

Image Segmentation

Course Instructor: Dr. M. Haroon Yousaf

Semester: Autumn 2018

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

Objectives:-

The objectives of this session is to understand following concepts.

 What is image segmentation and why is it relevant?  What is image thresholding and how is it implemented in MATLAB?  What are the most commonly used image segmentation techniques and how do they work?

Introduction:-

Segmentation is one of the most crucial tasks in image processing and computer vision. Image segmentation is the operation that marks the transition between low-level image processing and image analysis: the input of a segmentation block in a machine vision system is a preprocessed image, whereas the output is a representation of the regions within that image. This representation can take the form of the boundaries among those regions (e.g., when edge-based segmentation techniques are used) or information about which pixel belongs to which region (e.g., in clustering-based segmentation). Once an image has been segmented, the resulting individual regions (or objects) can be described, represented, analyzed, and classified.

Segmentation is defined as the process of partitioning an image into a set of non-overlapping regions whose union is the entire image. These regions should ideally correspond to objects and their meaningful parts, and background. Most image segmentation algorithms are based on one of two basic properties that can be extracted from pixel values—discontinuity and similarity—or a combination of them. Segmentation of nontrivial images is a very hard problem—made even harder by non-uniform lighting, shadows, overlapping among objects, poor contrast between objects and background, and so on—that has been approached from many different angles, with limited success to this date.

Figure 1 illustrates the problem. At the top, it shows the color and grayscale versions of a hard test image that will be used later in this session. Segmenting this image into its four main objects (Lego bricks) and the background is not a simple task for contemporary image segmentation algorithms, due to uneven lighting, projected shadows, and occlusion among objects. Attempting to do so without resorting to color information makes the problem virtually impossible to solve for the techniques described in this session.

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

(e.g., morphology, image statistics, graph theory), type of features (e.g., intensity, color, texture, motion), and approach (e.g., top-down, bottom-up, graph-based).

There is no universally accepted taxonomy for classification of image segmentation algorithms either. In this chapter, we have organized the different segmentation methods into the following categories:

 Intensity-based methods, also known as non-contextual methods, work based on pixel distributions (i.e., histograms). The best-known example of intensity-based segmentation technique is thresholding.  Region-based methods, also known as contextual methods, rely on adjacency and connectivity criteria between a pixel and its neighbors. The best known examples of region-based segmentation techniques are region growing and split and merge.  Other methods, where we have grouped relevant segmentation techniques that do not belong to any of the two categories above. These include segmentation based on texture, edges, and motion, among others.

INTENSITY-BASED SEGMENTATION:-

Intensity-based methods are conceptually the simplest approach to segmentation. They rely on pixel statistics—usually expressed in the form of a histogram to determine which pixels belong to foreground objects and which pixels should be labeled as background. The simplest method within this category is image thresholding, which will be described in detail in the remaining part of this section.

Image Thresholding:-

The basic problem of thresholding is the conversion of an image with many gray levels into another image with fewer gray levels, usually only two. This conversion is usually performed by comparing each pixel intensity with a reference value (threshold, hence the name) and replacing the pixel with a value that means “white” or “black” depending on the outcome of the comparison. Thresholding is a very popular image processing technique, due to its simplicity, intuitive properties, and ease of implementation.

Thresholding an image is a common preprocessing step in machine visual systems in which there are relatively few objects of interest whose shape is more important than surface properties (such as texture) and whose average brightness is relatively higher or lower than the other elements in the image. The test image in Figure 1c is an example of image suitable for image thresholding. Its histogram (Figure 2) has two distinct modes, the narrowest and most prominent one (on the

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

left) corresponding to background pixels, the broadest one (on the right) reflecting the intensity distribution of pixels corresponding to the coins.

FIGURE 2 The histogram for the image in Figure 1c: an example of histogram suitable for partitioning using a single threshold.

Mathematically, the process of thresholding an input image f(x,y) and producing a binarized version of it, g(x,y), can be described as

where T is the threshold. If the same value of T is adopted for the entire image, the process is called global thresholding. When the choice of value for T at a point of coordinates (x,y) depends on statistical properties of pixel values in a neighborhood around (x,y), it will be referred to as local or regional thresholding.

In MATLAB:-

The IPT has a function to convert a grayscale image into a binary (black-and-white) image, im2bw , that takes an image and a threshold value as input parameters.

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

the sake of comparison, the results obtained with a manually selected T = 0.25 are shown in Figure 3b.

FIGURE 3 Image thresholding results for the image in Figure 1c using iterative threshold selection algorithm (a) and manually selected threshold (b).

Optimal Thresholding:-

Many optimal strategies for selecting threshold values have been suggested in the literature. These strategies usually rely on assumed statistical models and consist of modeling the thresholding problem as a statistical inference problem. Unfortunately, such statistical models usually cannot take into account important factors such as borders and continuity, shadows, non- uniform reflectance, and other perceptual aspects that would impact a human user making the same decision. Consequently, for most of the cases, manual threshold selection by humans will produce better results than statistical approaches would [BD00]. The most popular approach under this category was proposed by Otsu4 in 1979 [Ots79] and implemented as an IPT function: graythresh.

Applying that function to the image in Figure 1c results in an optimal value for T = 0.4941, which—in this particular case—is remarkably close to the one obtained with the (much simpler) iterative method described earlier (T = 0.4947). However, as shown in Figure 3, neither of these methods produce a better (from a visual interpretation standpoint) result than the manually chosen threshold.

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

The Impact of Illumination and Noise on Thresholding:-

Illumination and reflectance patterns play a critical role in thresholding. Even an easy input image (such as the coins image), which could be successfully segmented using global thresholding, poses a much harder challenge if the illumination pattern changes from constant (uniform) to gradual (Figure 4). The resulting image (Figure 5a) is significantly darker overall and the corresponding histogram (Figure 5b) shows an expected shift to the left. Consequently, using the same value of threshold (T = 25) that produced very good results before (Figure 3b) will lead to an unacceptable binarized image (Figure 5c).

Noise can also have a significant impact on thresholding, as illustrated in Figure 5(d–f). In this case a Gaussian noise of mean zero and variance 0.03 has been applied to the image, resulting in the image in Figure 5d, whose histogram, shown in Figure 5e, has lost its original bimodal shape. The result of segmenting the image using T = 0.25 is shown in Figure 5f. Although not as bad as one could expect, it would need post-processing (noise reduction) to be truly useful. In summary, in both cases, the images changed significantly, their histograms lost their bimodal shape, and the originally chosen value for global threshold (T = 0.25) was no longer adequate. In addition, no other value could be easily chosen just by inspecting the histogram.

FIGURE 4 An example of uneven illumination pattern used to generate the image in Figure 5a.

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

In MATLAB:-

The block processing technique is implemented using the blkproc function.

EXAMPLE 1:-

In this example, we divide the image in Figure 5a into six vertical slices, treating each of them separately and using gray thresh to choose a different value of threshold for each slice. The resulting image (Figure 6b) is significantly better than the one we would have obtained using a single value (T = 0.3020, also calculated using graythresh ) for the entire image (Figure 6a). The left portion of Figure 6b gives away some hints at where the slices were placed.

FIGURE 6 Local thresholding. Using a single threshold for the entire image (a) and dividing it up into six slices and choosing a different threshold for each vertical slice (b).

REGION-BASED SEGMENTATION:-

Region-based segmentation methods are based on the fact that a pixel cannot be considered a part of an object or not based solely on its gray value (as intensity-based methods do). They incorporate measures of connectivity among pixels in order to decide whether these pixels belong to the same region (or object) or not. Mathematically, region-based segmentation methods can be described as a systematic way to partition an image I into n regions, R1,R2,···,Rn, such that the following properties hold [GW08]:

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

The first property states that the segmentation will be complete, that is, each pixel in the image will be labeled as belonging to one of the n regions. Property 2 requires that all points within a region be 4- or 8- connected. Property 3 states that the regions cannot overlap. Property 4 states which criterion must be satisfied so that a pixel is granted membership in a certain region, for example, all pixel values must be within a certain range of intensities. Finally, property 5 ensures that two adjacent regions are different in the sense of predicate P.

These logical predicates are also called homogeneity criteria, H(Ri).

Region Growing:-

The basic idea of region growing methods is to start from a pixel and grow a region around it, as long as the resulting region continues to satisfy a homogeneity criterion. It is, in that sense, a bottom-up approach to segmentation, which starts with individual pixels (also called seeds) and produces segmented regions at the end of the process. The key factors in region growing are as follows:

The Choice of Similarity Criteria:

For monochrome images, regions are analyzed based on intensity levels (either the gray levels themselves or the measures that can easily be calculated from them, for example, moments and texture descriptors) and connectivity properties.

The Selection of Seed Points:

These can be determined interactively (if the application allows) or based on a preliminary cluster analysis of the image, used to determine groups of pixels that share similar properties, from which a seed (e.g., corresponding to the centroid of each cluster) can be chosen.

The Definition of a Stopping Rule:

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

EXAMPLE 2:-

Figure 7 shows an example of region growing on small test image, where the logical predicate for uniformity is given by

where μi is the average intensity of all pixels in Ri except the reference pixel at (x,y) and ∆ is a user-selected threshold. In this example, ∆ = 3. Figure 7 shows the seed pixels (a), and the results of the first (b) and last (c) iterations.

FIGURE 7 Region growing: (a) seed pixels; (b) first iteration; (c) final iteration.

EXAMPLE 3:-

Figure 8 shows the results of applying a region growing algorithm to the two test images originally introduced in Figure 1. Part (a) shows the hard input image with the seed points (specified interactively by the user) overlaid. Part (b) shows the results of using region growing (pseudo colored for easier visualization): four regions plus background. A quick inspection shows that the results are virtually useless, primarily due to poor contrast between the two darker and two brighter bricks and the influence of projected shadows.

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

FIGURE 8 Region growing results for two test images. See text for details.

Limitations of Region Growing:-

The basic region growing algorithm described in this section has several limitations, listed below [Eff00]:

 It is not very stable: significantly different results are obtained when switching between 4-connectivity and 8-connectivity criteria.  Segmentation results are very sensitive to choice of logical uniformity predicate.  The number of seeds provided by the user may not be sufficient to assign every pixel to a region. If two or more seeds that should belong to the same region are incorrectly provided to the algorithm, it will be forced to create distinct regions around them although only one region should exist.

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

Procedure:-

Global Thresholding:-

The first method of thresholding that we will explore involves visually analyzing the histogram of an image to determine the appropriate value of T (the threshold value).

  1. Load and display the test image. I = imread(’coins.png’);

figure, imshow(I), title(’Original Image’);

  1. Display a histogram plot of the coins image to determine what threshold level to use.

figure, imhist(I), title(’Histogram of Image’);

Question 1:-

Which peak of the histogram represents the background pixels and which peak represents the pixels associated with the coins?

The histogram of the image suggests a bimodal distribution of grayscale values. This means that the objects in the image are clearly separated from the background. We can inspect the X and Y values of the histogram plot by clicking the “inspect” icon and then selecting a particular bar on the graph.

FIGURE 9 Histogram plot with data cursor selection.

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

  1. Inspect the histogram near the right of the background pixels by activating the data cursor. To do so, click on the “inspect” icon.

Figure 9 illustrates what the selection should look like. The data cursor tool suggests that values between 80 and 85 could possibly be used as a threshold, since they fall immediately to the right of the leftmost peak in the histogram. Let us see what happens if we use a threshold value of 85.

  1. Set the threshold value to 85 and generate the new image.

T = 85;

I_thresh = im2bw(I,( T / 255));

figure, imshow(I_thresh), title(’Threshold Image (heuristic)’);

Question 2:-

What is the purpose of the im2bw function?

Question 3:-

Why do we divide the threshold value by 255 in the im2bw function call?

You may have noticed that several pixels—some white pixels in the background and a few black pixels where coins are located—do not belong in the resulting image. This small amount of noise can be cleaned up using the noise removal techniques.

The thresholding process we just explored is known as the heuristic approach. Although it did work, it cannot be extended to automated processes. Imagine taking on the job of thresholding a thousand images using the heuristic approach! MATLAB’s IPT function gray thresh uses Otsu’s method [Ots79] for automatically finding the best threshold value.

  1. Use the graythresh function to generate the threshold value automatically.

T2 = graythresh(I);

I_thresh2 = im2bw(I,T2);

figure, imshow(I_thresh2), title(’Threshold Image (graythresh)’);

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

This function will be used to define each new block of pixels in our image. Basically all we want to do is perform thresholding on each block individually, so the code to do so will be similar to the code we previously used for thresholding.

  1. Add this line of code under the function definition. y = im2bw(x,graythresh(x));

When the function is called, it will be passed a small portion of the image, and will be stored in the variable x. We define our output variable y as a black and white image calculated by thresholding the input.

  1. Save your function as adapt_thresh.m in the current directory.

We can now perform the operation using the blkproc function. We will adaptively threshold the image, 10×10 pixel blocks at a time.

  1. Perform adaptive thresholding by entering the following command in the command window. Note that it may take a moment to perform the calculation, so be patient.

I_thresh = blkproc(I,[10 10],@adapt_thresh);

  1. Display the original and new image.

figure subplot(1,2,1), imshow(I), title(’Original Image’);

subplot(1,2,2),imshow(I_thresh),title(’Adaptive Thresholding’);

The output is not quite what we expected. If you look closely, however, the operation was successful near the text, but everywhere else it was a disaster. This suggests that we need to add an extra step to our function to compensate for this unwanted effect. Over the next few steps, let us examine the standard deviation of the original image where there is text, and where there is not.

  1. Calculate the standard deviation of two 10×10 blocks of pixels; one where there is text and another where there is not.

std_without_text = std2(I(1:10, 1:10))

std_with_text = std2(I(100:110, 100:110))

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

COMPUTER ENGINEERING DEPARTMENT

Digital Image Processing Lab Instructor:-Engr. Aamir Arsalan

Question 5:-

What is the difference between the standard deviation of the two blocks of pixels? Explain.

Since there is such a difference between a block with and without text, we can use this information to improve our function. Let us replace the one line of code we previously wrote with the new code that will include an if statement: if the standard deviation of the block of pixels is low, then simply label it as background; otherwise, perform thresholding on it. This change should cause the function to only perform thresholding where text exists. Everything else will be labeled as background.

  1. Replace the last line of our function with the following code. Save the function after the alteration. if std2(x) < 1 y = ones(size(x,1),size(x,2));

else

y = im2bw(x,graythresh(x));

end

Question 6:-

How does our function label a block of pixels as background?

  1. Now rerun the block process (in the command window) to see the result.

I_thresh2 = blkproc(I,[10 10],@adapt_thresh);

figure,

subplot(1,2,1), imshow(I), title(’Original Image’);

subplot(1,2,2),imshow(I_thresh2),title(’Adaptive Thresholding’);

Question 7:-

How does the output of the new function compare with the old?