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