Question

Trying to write a simple lowpass filter in python to run against lena. Then I'd like to run an inverse filter to run against the lowpass and try to get the original back (well, as close to original). I'm new to programming in python and not quite sure where to start. I tried rearranging a highpass filter code but it doesn't look right.

import matplotlib.pyplot as plt
import numpy as np
import scipy.misc
from scipy import ndimage
import Image 

#lowpass
def plot(data, title):
    plot.i += 1
    plt.subplot(2,2,plot.i)
    plt.imshow(data)
    plt.gray()
    plt.title(title)
plot.i = 0

 # Load the data...
img = scipy.misc.lena()
data = np.array(img, dtype=float)
plot(data, 'Original')

#narrow lowpass filter
kernel = np.array([[1, 1, 1],
               [1,  -8, 1],
               [1, 1, 1]])
lp_3 = ndimage.convolve(data, kernel)
plot(lp_3, '3x3 Lowpass')

# A slightly "wider" lowpass filter 
kernel = np.array([[1, 1, 1, 1, 1],
               [1,  -1,  -2,  -1, 1],
               [1,  -2,  -4,  -2, 1],
               [1,  -1,  -2,  -1, 1],
               [1, 1, 1, 1, 1]])
lp_5 = ndimage.convolve(data, kernel)
plot(lp_5, '5x5 Lowpass')
plt.show()
Was it helpful?

Solution

You should definitely check your kernel first. It does not look like a lowpass (averaging) kernel at all. Try first something like

kernel = np.ones((n,n))

if you want to do a very simple lowpass filter n by n (i.e. blurring):

lowpass result

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top