i
i
i
i
i
i
i
i
216 9. Signal Processing
When resampling images, we usually specify a source rectangle in the units of
the old image that specifies the part we want to keep in the new image. For exam-
ple, using the pixel sample positioning convention from Chapter 3, the rectangle
we’d use to resample the entire image is (−0.5,n
old
x
− 0.5) × (−0.5,n
old
y
− 0.5).
Given a source rectangle (x
l
,x
h
) × (y
l
,y
h
), the sample spacing for the new im-
age is Δx =(x
h
− x
l
)/n
new
x
in x and Δy =(y
h
− y
l
)/n
new
y
in y. The lower-left
sample is positioned at (x
l
+Δx/2,y
l
+Δy/2).
Modifying the 1D pseudocode to use this convention, and expanding the call
to the reconstruct function into the double loop that is implied, we arrive at:
function resample(sequence a, float x
l
, float x
h
,intn, filter f)
create sequence b of length n
r = f.radius
x
0
= x
l
+Δx/2
for i =0to n − 1 do
s =0
x = x
0
+ iΔx
for j = x − r to x + r do
s = s + a[j]f(x − j)
b[i]=s
return b
This routine contains all the basics of resampling an image. One last issue that
remains to be addressed is what to do at the edges of the image, where the simple
version here will access beyond the bounds of the input sequence. There are
several things we might do:
• Just stop the loop at the ends of the sequence. This is equivalent to padding
the image with zeros on all sides.
• Clip all array accesses to the end of the sequence—that is, return a[0] when
we would want to access a[−1]. This is equivalent to padding the edges of
the image by extending the last row or column.
• Modify the filter as we approach the edge so that it does not extend beyond
the bounds of the sequence.
The first option leads to dim edges when we resample the whole image, which
is not really satisfactory. The second option is easy to implement; the third is
probably the best performing. The simplest way to modify the filter near the edge
of the image is to renormalize it: divide the filter by the sum of the part of the filter
that falls within the image. This way, the filter always adds up to 1 over the actual
image samples, so it preserves image intensity. For performance, it is desirable