130 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> c = imread(’cameraman.png’);
>> head = c(33:96,90:153);
>> imshow(head)
>> head4n = imresize(head,4,’nearest’);imshow(head4n)
>> head4b = imresize(head,4,’bilinear’);imshow(head4b)
Python has a rescale function in the transform module of skimage:
Python
In : c = io.imread(’cameraman.png’)
In : head = c[32:96,89:153]
In : io.imshow(head)
In : head4n = tr.rescale(head,2,order=0)
In : head4n = tr.rescale(head,2,order=1)
The order parameter of the rescale method provides the order of the interpolating p oly-
nomial: 0 corresponds to nearest neighbor and 1 to bilinear interpolation.
The head is shown in Figure 6.8 and the results of the scaling are shown in Figure 6.9.
FIGURE 6.8: The head
Nearest neighbor interpolation gives an unacceptable blo cky effect; edges in particular
appear very jagged. Bilinear interpolation is much smoother, but the trade-off here is a
certain blurriness to the result. This is unavoidable: interpolation can’t predict values: we
can’t create data from nothing! All we can do is to guess at values that fit b est with the
original data.
6.3 General Interpolation
Although we have presented nearest neighbor and bilinear interpolation as two different
methods, they are in fact two special cases of a more general approach. The idea is this:
we wish to interpolate a value f(x
) for x
1
≤ x
≤ x
2
, and suppose x
− x
1
= λ. We define
an interpolation function R(u), and set
f(x
) = R (− λ ) f (x
1
) + R(1 − λ)f(x
2
). (6.2)