120 A Computational Introduction to Digital Image Processing, Second Edition
FIGURE 5.25: An image with an ROI, and the ROI mask
MATLAB
>> a = fspecial(’average’,15);
>> ma = roifilt2(a,m,roi);
>> u = fspecial(’unsharp’);
>> mu = roifilt2(u,m,roi);
>> l = fspecial(’log’);
>> ml = roifilt2(l,m,roi);
>> imshow(ma),figure,imshow(mu),figure,imshow(ml)
The images are shown in Figure 5.26.
Average filtering Unsharp masking Laplacian of Gaussian
FIGURE 5.26: Examples of the use of
roifilt2
Neighborhood Processing 121
FIGURE 5.27: ROI filtering with a polygonal mask
Regions of Interest in Octave and Python
Neither Octave nor Python have roipoly or roifilt2 commands. However, in each it
is quite straightforward to create a mask defining the region of interest, and use that mask
to restrict the act of filtering to the region.
With the monkey above, and the head region, suppose that the matrices for the image
and the ROI are M and R, respectively. If M
f
is the result of the image matrix after
filtering, then
M
f
R + M (1 R)
will provide the result we want. This is shown in Figure 5.27 where the rightmost image is
the sum of the other two.
In Octave, this can be achieved with:
Octave
> m2 = imread(’monkey.png’); m = m2(56:281,221:412);
> [r,c] = size(m);
> xi = [60 27 14 78 130 139]
> yi = [14 38 127 177 160 69]
> roi = poly2mask(yi,xi,r,c);
> f = fspecial(’gaussian’,9,3);
> mg = imfilter(m,f));
> mr = imadd(mg.
*
roi,m.
*
~roi)
And in Python, using zeros
_
like which creates an array of zeros the same size as its
input, as well as
polygon f rom the draw module of skimage:
122 A Computational Introduction to Digital Image Processing, Second Edition
Python
In: m2 = io.imread(’monkey.png’); m = m2[55:281,220:412]
In: r,c = m.shape
In: xi = np.array([60,27,14,78,130,139])
In: yi = np.array([14,38,127,177,160,69])
In: roi = np.zeros
_
like(m)
In: r,c = polygon(yi,xi)
In: roi[c,r] = 1
In: mg = ut.img
_
as
_
ubyte(fl.gaussian
_
filter(m,g))
In: mr = mg
*
roi + m
*
(1-roi)
5.11 Programs
This is a simple program (which can be run in MATLAB or Octave) f or bilateral filtering.
MATLAB/Octave
function out = bilateral(im,w,sigma
_
d,sigma
_
r)
im = im2double(im);
[r,c] = size(im);
out = zeros(r,c);
A = padarray(im,[w,w],’symmetric’);
G = fspecial(’gaussian’,2
*
w+1,sd); # the domain filter
for i = 1+w:r+w-1
for j = 1+w:c+w-1
R = A(i-w:i+w,j-w:j+w); # region to be computed
H = exp(-(R-A(i,j)).^2/(2
*
sr^2)); # the range filter
F = H.
*
G;
out(i-w,j-w) = sum(F(:).
*
R(:))/sum(F(:));
end;
end;
close(h);
end
Exercises
1. The array below represents a small grayscale image. Compute the images that result
when the image is convolved with each of the masks (a) to (h) shown. At the edge of
the image use a restricted mask. (In other words, pad the image with zeros.)
20 20 20 10 10 10 10 10 10
20 20 20 20 20 20 20 20 10
20 20 20 10 10 10 10 20 10
20 20 10 10 10 10 10 20 10
Neighborhood Processing 123
20 10 10 10 10 10 10 20 10
10 10 10 10 20 10 10 20 10
10 10 10 10 10 10 10 10 10
20 10 20 20 10 10 10 20 20
20 10 10 20 10 10 20 10 20
(a)
-1 -1 0
-1 0 1
0 1 1
(b)
0 -1 -1
1 0 -1
1 1 0
(c)
-1 -1 -1
2 2 2
-1 -1 -1
(d)
-1 2 -1
-1 2 -1
-1 2 -1
(e)
-1 -1 -1
-1 8 -1
-1 -1 -1
(f)
1 1 1
1 1 1
1 1 1
(g)
-1 0 1
-1 0 1
-1 0 1
(h)
0 -1 0
-1 4 -1
0 -1 0
2. Check your answers to the previous question with using imfilter (if you are using
MATLAB or Octave), or
ndi.correlate
if you are using Python.
3. Describe what each of the masks in the previous question might be used for. If you
can’t do this, wait until Question 5 below.
4. Devise a 3 × 3 mask for an “identity filter,” which causes no change in the image.
5. Choose an image that has a lot of fine detail, and load it.
Apply all the filters listed in Question 1 to this image. Can you now see what each
filter does?
6. Apply larger and larger averaging filters to this image. What is the smallest sized
filter f or which the fine detail cannot be seen?
7. Repeat the previous question with Gaussian filters with the following parameters:
Size
Standard deviation
[3,3] 0.5 1 2
[7,7]
1 3 6
[11,11]
1 4 8
[21,21]
1 5 10
At what values do the fine details disappear?
8. Can you see any observable difference in the results of average filtering and of using
a Gaussian filter?
9. If you are using MATLAB or Octave, read through the help page of the
fspecial
function, and apply some of the other filters to the cameraman image and to the
mandrill image.
10. Apply different Laplacian filters to an image of your choice at and to the cameraman
images. Which pro duces the best edge image?
11. Is the 3 ×3 median filter separable? That is, can this filter be implemented by a 3 ×1
filter f ollowed by a 1 × 3 filter?
12. Repeat the above question for the maximum and minimum filters.
124 A Computational Introduction to Digital Image Processing, Second Edition
13. Apply a 3 × 3 averaging filter to the middle 9 values of the matrix
a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
and then apply another 3 × 3 averaging filter to the result.
Using your answer, describe a 5 × 5 filter that has the effect of two averaging filters.
Is this filter separable?
14. Use the appropriate commands to produce the outputs shown in Figure 5.5, starting
with the diagonally increasing image
20 40 60 80 100
40 60 80 100 120
60 80 100 120 140
80 100 120 140 160
100 120 140 160 180
15. Display the difference between the cmax and cmin images obtained in Section 5.8.
You can do this with an image subtraction.
What are you seeing here? Can you account f or the output of these commands?
16. If you are using MATLAB or Octave, then use the
tic and toc timer functions to
compare the use of
nlfilter and colfilt functions. If you are using the ipython
enhanced s hell of Python, try the
%time f unction
17. Use colfilt (MATLAB/Octave) or generic
_
filter (Python) to implement the
geometric mean and alpha-trimmed mean filters.
18. If you are using MATLAB or Octave, show how to implement the root-mean-square
filter.
19. Can unsharp masking be used to reverse the effects of blurring? Apply an unsharp
masking filter after a 3 ×3 averaging filter, and describe the result.
20. Rewrite the Kuwahara filter as a single function that can be applied with either
colfilt (MATLAB/Octave) or generic
_
filter (Python).
..................Content has been hidden....................

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