[iainversefiltering] [Up] [iaotsudemo] Lessons

iamagnify
Illustrate the interpolation of magnified images

Description

In this demonstration, the interpolation of magnified image is explained using the frequency domain. The interpolation is a low pass filter that can be applied either in the spatial domain, which is the methods known as nearest neighbor or pixel replication and bi-linear interpolation. Or in the frequency domain, with an ideal filter or a butterworth filter.

Demo Script

Reading and ROI selection

The image is read and a 64x64 ROI is selected and displayed

>>> fin = iaread('lenina.pgm')

                  
>>> iashow(fin)
(256, 256) Min= 36 Max= 255 Mean=88.958 Std=46.38
>>> froi = fin[137:137+64,157:157+64]

                  
>>> iashow(froi)
(64, 64) Min= 38 Max= 255 Mean=82.117 Std=29.83
>>> print froi.shape
(64, 64)
fin froi

DFT

The DFT of the small image is taken and its spectrum displayed

>>> import Numeric, FFT

                  
>>> fd = froi.astype(Numeric.Float)

                  
>>> F = FFT.fft2d(fd)

                  
>>> iashow(froi)
(64, 64) Min= 38 Max= 255 Mean=82.117 Std=29.83
>>> iashow(iadftview(F))
(64, 64) Min= 23 Max= 254 Mean=103.943 Std=22.48
froi iadftview(F)

Expansion by 4 without interpolation

The image is expanded by 4, but filling the new pixels with 0

>>> fx4 = Numeric.zeros(4*Numeric.array(froi.shape))

                  
>>> fx4[::4,::4] = froi

                  
>>> iashow(froi)
(64, 64) Min= 38 Max= 255 Mean=82.117 Std=29.83
>>> iashow(fx4)
(256, 256) Min= 0 Max= 255 Mean=5.132 Std=21.23
froi fx4

DFT of the expansion without interpolation

Using the expansion propertie of the DFT (only valid for the discrete case), the resulting DFT is a periodical replication of the original DFT.

>>> fdx4 = fx4.astype(Numeric.Float)

                  
>>> Fx4 = FFT.fft2d(fdx4)

                  
>>> iashow(fx4)
(256, 256) Min= 0 Max= 255 Mean=5.132 Std=21.23
>>> iashow(iadftview(Fx4))
(256, 256) Min= 23 Max= 254 Mean=103.943 Std=22.48
fx4 iadftview(Fx4)

Filtering by mean filtering - nearest neighbor

Filtering the expanded image using an average filter of size 4x4 is equivalent of applying a nearest neighbor interpolator. The zero pixels are replaced by the nearest non-zero pixel. This is equivalent to interpolation by pixel replication.

>>> k = Numeric.ones((4,4))

                  
>>> fx4nn = iapconv(fdx4, k)

                  
>>> iashow(fx4)
(256, 256) Min= 0 Max= 255 Mean=5.132 Std=21.23
>>> iashow(fx4nn.astype(Numeric.Int))
(256, 256) Min= 38 Max= 255 Mean=82.117 Std=29.82
fx4 fx4nn.astype(Numeric.Int)

Interpretation of the mean filtering in the frequency domain

Filtering by the average filter in space domain is equivalent to filter in the frequency domain by the sync filter.

>>> kzero = Numeric.zeros(fx4.shape)

                  
>>> kzero[0:4,0:4] = k

                  
>>> K = FFT.fft2d(kzero)

                  
>>> iashow(iadftview(K))
(256, 256) Min= 0 Max= 255 Mean=83.562 Std=63.86
>>> Fx4nn = K * Fx4

                  
>>> iashow(iadftview(Fx4nn))
(256, 256) Min= 0 Max= 255 Mean=84.113 Std=25.81
iadftview(K) iadftview(Fx4nn)

Filtering by pyramidal kernel, linear interpolation

Filtering by a pyramidal kernel in space domain is equivalent to make a bi-linear interpolation. The zero pixels are replaced by a weighted sum of the neighbor pixels, the weight is inversely proportional to the non-zero pixel distance.

>>> klinear = Numeric.array([1,2,3,4,3,2,1])/4.

                  
>>> k2dlinear = Numeric.matrixmultiply(Numeric.reshape(klinear, (7,1)), Numeric.reshape(klinear, (1,7)))

                  
>>> fx4li = iapconv(fdx4, k2dlinear)

                  
>>> iashow(fx4)
(256, 256) Min= 0 Max= 255 Mean=5.132 Std=21.23
>>> iashow(fx4li.astype(Numeric.Int))
(256, 256) Min= 38 Max= 255 Mean=81.746 Std=29.43
fx4 fx4li.astype(Numeric.Int)

Interpretation of the pyramid filtering in the frequency domain

Filtering by the pyramid filter in space domain is equivalent to filter in the frequency domain by the square of the sync filter.

>>> klizero = Numeric.zeros(fx4.shape).astype(Numeric.Float)

                  
>>> klizero[0:7,0:7] = k2dlinear

                  
>>> Klinear = FFT.fft2d(klizero)

                  
>>> iashow(iadftview(Klinear))
(256, 256) Min= 0 Max= 255 Mean=33.597 Std=56.60
>>> Fx4li = Klinear * Fx4

                  
>>> iashow(iadftview(Fx4li))
(256, 256) Min= 0 Max= 255 Mean=46.760 Std=35.63
iadftview(Klinear) iadftview(Fx4li)

Using an ideal filter

Filtering by cutoff period of 8

>>> H8 = iabwlp(fx4.shape, 8, 10000)

                  
>>> iashow(iadftview(H8))
(256, 256) Min= 0 Max= 255 Mean=12.482 Std=55.01
>>> G8 = Fx4 * H8

                  
>>> iashow(iadftview(G8))
(256, 256) Min= 0 Max= 254 Mean=5.354 Std=24.07
>>> g_ideal = FFT.inverse_fft2d(G8)

                  
>>> print max(Numeric.ravel(g_ideal.imag))
2.96873389363e-014
>>> g_ideal = ianormalize(g_ideal.real, [0,255])

                  
>>> iashow(g_ideal)
(256, 256) Min= 0.0 Max= 255.0 Mean=55.482 Std=31.85
iadftview(H8) iadftview(G8)
g_ideal

Using a Butterworth filter of order 5

Filtering by cutoff period of 8

>>> HB8 = iabwlp(fx4.shape, 8, 5)

                  
>>> iashow(iadftview(HB8))
(256, 256) Min= 0 Max= 255 Mean=16.973 Std=56.38
>>> GB = Fx4 * HB8

                  
>>> iashow(iadftview(GB))
(256, 256) Min= 0 Max= 254 Mean=11.519 Std=28.05
>>> g_b = FFT.inverse_fft2d(GB)

                  
>>> print max(Numeric.ravel(g_b).imag)
3.19653155422e-014
>>> g_b = ianormalize(g_b.real, [0,255])

                  
>>> iashow(g_b)
(256, 256) Min= 0.0 Max= 255.0 Mean=52.387 Std=31.15
iadftview(HB8) iadftview(GB)
g_b

Display all four for comparison

Top-left: nearest neighbor, Top-right: linear, Bottom-left: ideal, Bottom-right: Butterworth

>>> aux1 = Numeric.concatenate((fx4nn[0:256,0:256], fx4li[0:256,0:256]), 1)

                  
>>> aux2 = Numeric.concatenate((g_ideal, g_b), 1)

                  
>>> iashow(Numeric.concatenate((aux1, aux2)))
(512, 512) Min= 0.0 Max= 255.0 Mean=68.026 Std=33.69
Numeric.concatenate((aux1, aux2))

[iainversefiltering] [Up] [iaotsudemo] http://www.python.org