Chapter 9
Image Segmentation
9.1 Introduction
Segmentation refers to the operation of partitioning an image into component parts
or into separate objects. In this chapter, we shall investigate two very important topics:
thresholding and edge detection.
9.2 Thresholding
Single Thresholding
A grayscale image is turned into a binary (black and white) image by first choosing a
gray level T in the original image, and then turning every pixel black or white according to
whether its gray value is greater than or less than T :
A pixel becomes
white if its gray level is > T ,
black if its gray level is T .
Thresholding is a vital part of image segmentation, where we wish to isolate objects from
the background. It is also an important component of robot vision.
Thresholding can be done very simply in any of our systems. Suppose we have an 8-bit
image, stored as the variable
X. Then the command
X>T or X<T
will perform the thresholding. For example, consider an image of some birds flying across
a sky; it can be thresholded to show the birds alone in MATLAB or Octave:
MATLAB/Octave
>> f = imread(’flying.png’);
>> imshow(f); figure,imshow(f<50)
and in Python:
Python
In : f = io.imread(’flying.png’)
In : io.imshow(f)
In : fig = plt.figure(); fig.show(io.imshow(f<50))
These commands will produce the images shown in Figure 9.1. The resulting image can
223
224 A Computational Introduction to Digital Image Processing, Second Edition
FIGURE 9.1: Thresholded image of flying birds
then be further processed to find the number, or average size of the birds.
To see how this works, recall that in each system, an operation on a single number,
when applied to a matrix, is interpreted as being applied simultaneously to all elements of
the matrix; this is vectorization, which we have seen earlier. In MATLAB or Octave the
command
X>T
will thus return 1 (for true) for all those pixels for which the gray values are
greater than
T, and 0 (for false) for all those pixels for which the gray values are less than
or equal to
T. The result is a matrix of 0’s and 1’s, which can be viewed as a binary image.
In Python, the result will be a Bo olean array whose elements are
True or False. Such an
array can however be viewed without any further processing as a binary image. A Boolean
array can be turned into a floating point array by re-casting the array to the required data
type:
Python
In : (f<50).dtype
Out: dtype(’bool’)
In : f1 = (f<50).astype(’float64’)
In : f1.dtype
Out: dtype(’float64’)
The flying birds image shown above has dark objects on a light background; an image
with light objects over a dark background may be treated the same:
MATLAB/Octave
>> p = imread(’paperclips.png’)
>> imshow(p),figure,imshow(p>140)
will produce the images shown in Figure 9.2.
As well as the above method, MATLAB and Octave have the
im2bw function, which
thresholds an image of any data type, using the general syntax
im2bw(image,level)
where level is a value between 0 and 1 (inclusive), indicating the fraction of gray values
to be turned white. This command will work on grayscale, colored, and indexed images
of data type
uint8, uint16 or double. For example, the thresholded flying and paperclip
images above could be obtained using
MATLAB/Octave
>> im2bw(f,0.3);
>> im2bw(p,0.55);
Image Segmentation 225
FIGURE 9.2: Thresholded image of paperclips
The
im2bw
function automatically scales the value
level
to a gray value appropriate to the
image type, and then performs a thresholding by our firs t method.
As well as isolating objects from the background, thresholding provides a very simple
way of showing hidden aspects of an image. For example, the image of some handmade
paper
handmade.png appears mostly white, as nearly all the gray values are very high.
However, thresholding at a high level produces an image of far greater interest. We can use
the commands
MATLAB/Octave
>> h = imread(’handmade.png’);
>> imshow(h),figure,imshow(h>242)
to provide the images shown in Figure 9.3.
FIGURE 9.3: The paper image and result after thresholding
Double Thresholding
Here we choose two values T
1
and T
2
and apply a thresholding operation as:
A pixel becomes
white if its gray level is between T
1
and T
2
,
black if its gray level is otherwise.
We can implement this by a simple variation on the above metho d:
226 A Computational Introduction to Digital Image Processing, Second Edition
X>T1 & X<T2
Since the ampersand acts as a logical “and,” the result will only produce a one where both
inequalities are satisfied. Consider the following sequence of commands:
MATLAB/Octave
>> x = imread(’xray.png’);
>> imshow(x),
figure
,imshow(x>50 & x<80)
The output is shown in Figure 9.4. Note how double thresholding isolates the boundaries of
FIGURE 9.4: The image xray.png and the result after double thresholding
the lungs, which single thresholding would be unable to do. Similar results can be obtained
using
im2bw:
MATLAB/Octave
>> imshow(im2bw(x,0.2)&~im2bw(x,0.3))
In Python, the command is almost the same:
Python
In : io.imshow((x>40) & (x<80))
9.3 Applications of Thresholding
We have seen that thresholding can be useful in the following situations:
1. When we want to remove unnecessary detail from an image, to concentrate on es-
sentials. Examples of this were given in the flying birds and paperclip images: by
removing all gray level information, the birds and paperclips were reduced to binary
objects. But this information may be all we need to investigate sizes, shapes, or
numbers of objects.
Image Segmentation 227
2. To bring out hidden detail. This was illustrated with paper and x-ray images. In
both, the detail was obscured because of the similarity of the gray levels involved.
But thresholding can be vital for other purposes:
3. When we want to remove a varying background from an image. An example of
this was the paper clips image shown previously; the paper clips are all light, but
the background in fact varies considerably. Thresholding at an appropriate value
completely removes the background to show just the objects.
9.4 Choosing an Appropriate Threshold Value
We have seen that one of the important uses of thresholding is to isolate objects from
their background. We can then measure the sizes of the objects, or count them. Clearly
the success of these operations depends very much on choosing an appropriate threshold
level. If we choose a value too low, we may decrease the size of some of the objects, or
reduce their number. Conversely, if we choose a value too high, we may begin to include
extraneous background material.
Consider, for example, the image
pinenuts.png, and suppose we try to threshold using
the
im2bw f unction and various threshold values t for 0 < t < 1.
MATLAB/Octave
>> n = imread(’pinenuts.png’);
>> imshow(n);
>> n1 = im2bw(n,0.35);
>> n2 =im2bw(n,0.55);
>> figure,imshow(n1),figure,imshow(n2)
All the images are shown in Figure 9.5. One approach is to investigate the histogram of the
n: Original image n1: Threshold too low n2: Threshold too high
FIGURE 9.5: Attempts at thresholding
image, and see if there is a clear spot to break it up. Sometimes this can work well, but not
always.
Figure 9.6 shows various histograms. In each case, the image consists of objects on a
background. But only for some histograms is it easy to see where we can split it. In both
the blood and daisies images, we could split it up about half way, or at the “valley” between
the peaks, but for the paramecium and pinenut images, it is not so clear, as there would
appear to be three peaks—in each case, one at the extreme right.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.35.148