Image Geometry 135
MATLAB/Octave
>> [r,c] = size(m);
>> m2 = zeros(2
*
r,2
*
c);
>> m2(1:2:1
*
r,1:2:2
*
c) = m
ans =
16 0 2 0 3 0 13 0
0 0 0 0 0 0 0 0
5 0 11 0 10 0 8 0
0 0 0 0 0 0 0 0
9 0 7 0 6 0 12 0
0 0 0 0 0 0 0 0
4 0 14 0 15 0 1 0
0 0 0 0 0 0 0 0
In Python, interleaving is also easily done:
Python
In : m = array([[16,2,3,13],[5,11,10,8],[9,7,6,12],[4,14,15,1]]);
In : r,c = m.shape
In : m2 = zeros((2
*
r,2
*
c))
In : m2[::2,::2] = m
We can now replace the zeros by applying a spatial filter to this matrix. The spatial filters
1 1 0
1 1 0
0 0 0
1
4
1 2 1
2 4 2
1 2 1
implement nearest neighbor interpolation and bilinear interpolation, respectively. We can
test this with a few commands:
136 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> imfilter(m2,[1 1 0;1 1 0;0 0 0])
ans
=
16 16 2 2 3 3 13 13
16 16 2 2 3 3 13 13
5 5 11 11 10 10 8 8
5 5 11 11 10 10 8 8
9 9 7 7 6 6 12 12
9 9 7 7 6 6 12 12
4 4 14 14 15 15 1 1
4 4 14 14 15 15 1 1
>> format bank
>> imfilter(m2,[1 2 1;2 4 2;1 2 1]/4)
ans =
16.00 9.00 2.00 2.50 3.00 8.00 13.00 6.50
10.50 8.50 6.50 6.50 6.50 8.50 10.50 5.25
5.00 8.00 11.00 10.50 10.00 9.00 8.00 4.00
7.00 8.00 9.00 8.50 8.00 9.00 10.00 5.00
9.00 8.00 7.00 6.50 6.00 9.00 12.00 6.00
6.50 8.50 10.50 10.50 10.50 8.50 6.50 3.25
4.00 9.00 14.00 14.50 15.00 8.00 1.00 0.50
2.00 4.50 7.00 7.25 7.50 4.00 0.50 0.25
(The format bank provides an output with only two decimal places.) In Python, the filters
can be applied as:
Python
In : ndi.convolve(m2,array([[0,0,0],[0,1,1],[0,1,1]]),mode=’constant’)
In : ndi.convolve(m2,array([[1,2,1],[2,4,2],[1,2,1]])/4.0,mode=’constant’)
We can check these with the commands
MATLAB/Octave
>> m2b=imresize(m,[8,8],’nearest’);m2b
>> m2b=imresize(m,[7,7],’bilinear’);m2b
In the second command we only scaled up to 7 ×7, to ensure that the interpolation points
lie exactly half-way between the original data values. The filter
1
64
1 4 6 4 1
4 16 24 16 4
6 24 36 24 6
4 16 24 16 4
1 4 6 4 1
can be used to approximate bicubic interpolation.
We can try all of these with the cameraman’s head, doubling its size.
Image Geometry 137
MATLAB/Octave
>> hz = uint8(zeros(size(head)
*
2));
>> hz(1:2:2nd,1:2:end) = head;
>> imshow(hz)
>> imshow(imfilter(hz, [1 1 0;1 1 0;0 0 0])
>> imshow(imfilter(hz,[1 2 1;2 4 2;1 2 1]/4)
>> bfilt=[1 4 6 4 1;4 16 24 16 4;6 24 36 24 6;4 16 24 16 4;1 4 6 4 1]/64;
>> imshow(imfilter(hz,bfilt))
or in Python with
Python
In : r,c = head.shape
In : hz = np.zeros((2
*
r,2
*
c)).astype(’uint8’)
In : hz[::2,::2] = head
In : ne = array([[0,0,0],[0,1,1],[1,1,1]])
In : bi = array([[1,2,1],[2,4,2],[1,2,1]])/4.0
In : bc = array
([[1,4,6,4,1],[4,16,24,16,4],[6,24,36,24,6],[4,16,24,16,4],[1,4,6,4,1]])
/64.0
In : io.imshow(ndi.correlate(hz,ne))
and similarly for the others. The results are shown in Figure 6.16. We can enlarge more
by simply taking the result of the filter, applying a zero-interleave to it, and then another
filter.
Zero interleaving Nearest neighbor Bilinear Bicubic
FIGURE 6.16: Enlargement by spatial filtering
6.5 Scaling Smaller
The business of making an image smaller is also called image minimization; one way
is to take alternate pixels. If we wished to produce an image one-sixteenth the size of the
original, we would take out only those pixels (i, j) for which i and j are both multiples of
four. This method is called image subsampling and corresponds to the
nearest option of
imresize, and is very easy to implement.
However, it does not give very good results at high frequency components of an image.
We shall give a simple example; we shall construct a large image consisting of a white square
with a single circle on it. The
meshgrid command provides row and column indices which
span the array:
138 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> [x,y] = meshgrid(-255:256);
>> z = sqrt(x.^2+y.^2);
>> t = 1 - (z>254.5 & z<256);
>> imshow(t)
or
Python
In : r = range(-256,256)
In : [x,y] = np.meshgrid(r,r)
In : z = sqrt(x
**
2 + y
**
2)
In : t = 1-((z>254.5) & (z<256))
*
1
In : io.imshow(t)
Now we can resize it by taking out most pixels:
MATLAB/Octave
>> t2 = imresize(t,0.25,’nearest’);
or
Python
In : t2 = tr.rescale(t,0.25,order=0)
and this is shown in Figure 6.17(a). Notice that because of the way that pixels were removed,
the resulting circle contains gaps. If we were to use one of the other methods:
MATLAB/Octave
>> t3 = imresize(t,0.25,’bicubic’);
or
Python
In : t3 = tr.rescale(t,0.25,order=3)
a low pass filter is applied to the image first. The result is shown in Figure 6.17(b). The
image in Figure 6.17(b) can be made binary by thresholding (which will be discussed in
greater detail in Chapter 9). In this case
MATLAB/Octave
>> t4 = imresize(t,0.25,’bicubic’)>0.9;
or
Python
In : t4 = tr.rescale(t,0.25,order=3)>0.9
does the trick.
Image Geometry 139
(a) Nearest neighbor minimization (b) Bicubic interpolation for minimization
FIGURE 6.17: Minimization
6.6 Rotation
Having done the hard work of interpolation for scaling, we can easily apply the same
theory to image rotation. First recall that the mapping of a point (x, y) to another (x
, y
)
through a counter-clockwise rotation of θ as shown in Figure 6.18 is obtained by the matrix
product
x
y
=
cos θ sin θ
sin θ cos θ
x
y
.
(x, y)
(x
, y
)
θ
FIGURE 6.18: Rotating a point through angle θ
Similarly, since the matrix involved is orthogonal (its inverse is equal to its transpose),
we have:
x
y
=
cos θ sin θ
sin θ cos θ
x
y
.
..................Content has been hidden....................

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