image regions corrupt after applying squared filter

Issue

I have this image:

Original image

I am applying a low-pass square filter to that:

def SquareFilter(imgShape,size):
    filterSquare = np.zeros(imgShape)
    filterSquare[
        imgShape[0]//2-size//2 : imgShape[0]//2+size//2,
        imgShape[1]//2-size//2 : imgShape[1]//2+size//2
    ] = 1
    return filterSquare

im = cv2.imread("Images/wall.jpg",0)

freq = np.fft.fft2(im)
freq2 = np.fft.fftshift(freq)
filtered_g = freq2 * SqFilt
asd = np.fft.ifftshift(np.fft.ifft2(filtered_g))

fel = cv2.normalize(abs(asd), None, 0, 255, cv2.NORM_MINMAX)

cv2.imwrite('Results/result.png',fel)

but the result is completely different, I think image is divides into 4 region. I don’t want this.

Result

Solution

When you take the discrete fourier transform of an image using the FFT, you assume that it lies on an infinite plane of such images repeating at the boundaries. Your filter is not only a low-pass filter, it is also a shift filter. To fix your code, take the ifftshift before the ifft2:

asd = np.fft.ifft2(np.fft.ifftshift(filtered_g))

Answered By – Jordan Bonecutter

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.