Image Segmentation 233
blood.png paramecium1.png
pinenuts.png daisies.png
FIGURE 9.9: Images to be thresholded
This is an example of putting all the images into a cell array, and iterating over the array
using
cellfun, using a previously described function (in this case thresh). A cell array is
useful here as it can hold arbitrary objects.
MATLAB only supp orts Otsu’s method:
MATLAB
>> cellfun(@(x) graythresh(x),{b,p,n,d},’UniformOutput’,false)
ans =
[0.7451] [0.7529] [0.4745] [0.5137]
In Python, we can iterate over the images by putting them in a list:
Python
In : for x in [b,p,n,d]:
...: print [fl.threshold
_
otsu(x), fl.threshold
_
isodata(x)]
...:
[190, 190]
[192, 192]
[121, 122]
[131, 131]
Note that Python returns an 8-bit integer if that is the initial image type, whereas both
MATLAB and Octave return a value of type
double.
Once the optimum threshold value has been obtained, it can be applied to the image
using
im2bw:
MATLAB/Octave
>> imshow(im2bw(b,graythresh(b))))
or
234 A Computational Introduction to Digital Image Processing, Second Edition
Python
In : io.imshow(b>fl.threshold
_
otsu(b))
and similarly for all the others.
The results are shown in Figure 9.10. Note that for each image the result given is quite
satisfactory.
Blood Paramecia
Pinenuts Daisies
FIGURE 9.10: Thresholding with values obtained with Otsu’s method
9.5 Adaptive Thresholding
Sometimes it is not possible to obtain a single threshold value that will isolate an object
completely. This may happen if both the object and its background vary. For example,
suppose we take the paramecium image and adjust it so that both the objects and the
background vary in brightness across the image. This can be done in MATLAB or Octave
as:
MATLAB/Octave
>> p = im2double(imread(’paramecium1.png’))
>> [r,c] = size(p);
>> [y,x] = meshgrid(linspace(0,1,c),linspace(0,1,r));
>> p2 = 1-p+y/2;
>> imshow(p2)
and in Python as
Image Segmentation 235
Python
In : p = io.imread(’paramecium1.png’).astype(float)
In : x,y = np.mgrid[0:r,0:c].astype(float)
In : p2 = 255.0-p+y/2
In : io.imshow(p2)
Figure 9.11 shows an attempt at thresholding, using
graythresh
, with MATLAB or Octave:
MATLAB/Octave
>> t = graythresh(p2)
ans
t = 0.43529
>> figure,imshow(im2bw(p2,t))
or with Python:
Python
In : t = fl.threshold
_
otsu(p2); t
Out: 290.7939453125
In : io.imshow(p2>t)
As you see, the result is not particularly good; not all of the objects have been isolated
from their background. Even if different thresholds are used , the results are similar. To see
(a) Paramecium image: p2 (b) Thresholding attempt
FIGURE 9.11: An attempt at thresholding
why a single threshold won’t work, look at a plot of pixels across the image, as shown in
Figure 9.12(a).
In this figure, the line of pixels is being shown as a function; the threshold is shown on
the right as a horizontal line. It can be seen that no position of the plane can cut off the
objects from the background.
What can be done in a situation like this is to cut the image into small pieces, and apply
thresholding to each piece individually. Since in this particular example the brightness
changes from left to right, we shall cut up the image into six pieces and apply a threshold
to each piece individually. The vectors
starts and ends contain the column indices of the
start and end of each blo ck.
236 A Computational Introduction to Digital Image Processing, Second Edition
(a) A horizontal line of pixels (b) Thresholding attempt
FIGURE 9.12: An attempt at thresholding—functional version
MATLAB/Octave
>> out = zeros(r,c);
>> starts = 1:c/6:c
starts =
1 163 325 487 649 811
>> ends = 0:c/6:c
ends =
162 324 486 648 810 972
>> for i = 1:6,
> temp = p2(:,starts(i):ends(i))
> out((:,starts(i):ends(i)) = im2bw(temp,graythresh(temp));
> end
In Python, the commands are very similar:
Python
In : starts = range(0,c-1,162)
In : ends = range(162,c+1,162)
In : z = np.zeros((r,c))
In : for i in range(6):
...: temp = p2[:,starts[i]:ends[i]]
...: z[:,starts[i]:ends[i]]=(temp>fl.threshold
_
otsu(temp))
*
1.0
...:
Figure 9.13(a) shows how the image is sliced up, and Figure 9.13(b) shows the results after
thresholding each piece. The above commands can be done much more simply by using the
command
blockproc, which applies a particular function to each block of the image. We
can define our function with
MATLAB/Octave
>> thresh = @(z) im2bw(z,graythresh(z));
Notice that this uses the same as the command used above to create each piece of the
previous threshold, except that now
x is used to represent a general input variable.
The f unction can then be applied to the image
p2 with
MATLAB/Octave
>> blockproc(p2,[r,c/6],thresh);
Image Segmentation 237
(a) Cutting up the image (b) Thresholding each part separately
FIGURE 9.13: Adaptive thresholding
What this command means is that we apply our function
thresh to each distinct 648 ×162
block of our image.
9.6 Edge Detection
Edges contain some of the most useful information in an image. We may use edges to
measure the size of objects in an image; to isolate particular objects from their background;
to recognize or classify objects. There are a large number of edge-finding algorithms in
existence, and we shall look at some of the more straightforward of them. The general
command in MATLAB or Octave for finding edges is
edge(image,’method’,parameters. . . )
where the parameters available depend on the method used. In Python, there are many
edge detection methods in the
filter module of skimage. In this chapter, we shall show
how to create edge images using basic filtering methods, and discuss the uses of those edge
functions.
An edge may be loosely defined as a local discontinuity in the pixel values which exceeds
a given threshold. More informally, an edge is an observable difference in pixel values. For
example, consider the two 4 × 4 blocks of pixels shown in Figure 9.14.
51 52 53 59
54 52 53 62
50 52 53 68
55 52 53 55
50 53 155 160
51 53 160 170
52 53 167 190
51 53 162 155
FIGURE 9.14: Blocks of pixels
In the right-hand block, there is a clear difference between the gray values in the second
and third columns, and for these values the differences exceed 100. This would be easily
discernible in an image—the human eye can pick out gray differences of this magnitude
with relative ease. Our aim is to develop methods that will enable us to pick out the edges
of an image.
..................Content has been hidden....................

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