0
$\begingroup$

The situation

I need to apply a high pass filter to an image. The approach I'm following uses Fourier transform to apply a circular filter which would eliminate low frequencies.

Say I have a frequency threshold below which frequencies should be filtered out, i.e. cut-off frequency.

The resulting spectogram shape of the FTT operation is the same than the image, hence I am not sure how to link the radius value of the mask to the cut-off frequency of interest.

The code

# read image
image = cv2.imread('path/image_name.jpg', 0)
img = np.asarray(image)

# FFT
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:,:,0], dft_shift[:,:,1]))

# center of image
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)

# create mask - circular filter
mask = np.ones((rows, cols, 2), np.uint8)
r=4.34
center = [crow, ccol]
x, y = np.ogrid[:rows, :cols]
mask_area = (x - center[0])**2 + (y - center[1])**2 <= r*r
mask[mask_area] = 0

# apply filter
fshift = dft_shift * mask

# return to spatial domain
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1])

The question

What is the intuition for the radius choice for the mask? I understand that a higher the radius will filter out more frequencies. However, how would I know that frequencies above the threshold are not being affected by the filter?

$\endgroup$
2
  • $\begingroup$ Welcome to SE.SP! The choice of $r$ will depend on what frequencies you’re interested in. (I know, professor answer). Why are you aiming to high pass filter the image? What are you using the high pass filtered image for? $\endgroup$
    – Peter K.
    Commented Nov 10, 2021 at 12:24
  • $\begingroup$ Hi @PeterK. the image is a x-ray image and the high pass filter is aiming to enhance the bone structure by removing the low frequencies. For this particular application, the filter is not generic but needs to filter above a specific value. My question is also related to this other post but with a different approach. $\endgroup$
    – davipeix
    Commented Nov 10, 2021 at 13:15

1 Answer 1

1
$\begingroup$

This question, I believe, really requires an "it depends" answer.

I think you need to get some example images and work with them for different values of r.

I've adapted your code to do this and applied it to this image:

Original

and then some of the different r values:

r=4 r=4.1

r=4.2 r=5.0


Python code below

import cv2
import numpy as np
import matplotlib.pyplot as plt

# read image
image = cv2.imread('Q79057.jpg', 0)
img = np.asarray(image)

plt.figure(1)
plt.imshow(image)
plt.title('Original Image')

# FFT
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:,:,0], dft_shift[:,:,1]))

# center of image
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)

figNo = 2
# create mask - circular filter
for r in np.arange(4.0,5.1,0.1):
    mask = np.ones((rows, cols, 2), np.uint8)
    center = [crow, ccol]
    x, y = np.ogrid[:rows, :cols]
    mask_area = (x - center[0])**2 + (y - center[1])**2 <= r*r
    mask[mask_area] = 0

    # apply filter
    fshift = dft_shift * mask

    # return to spatial domain
    f_ishift = np.fft.ifftshift(fshift)
    img_back = cv2.idft(f_ishift)
    img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1])

    plt.figure(figNo)
    plt.imshow(img_back)
    plt.title('r = ' + str(r))
    figNo = figNo + 1
$\endgroup$
1
  • $\begingroup$ Thanks for your reply @PeterK. As I see, this FFT methodology has no strict control over the frequency values to be deleted. Trial and error over the r parameter leads to uncertainty. Since the cut-off frequency is a requirement for this particular application, I guess other methodologies should be explored. Do you know any approach which would allow the required control over the threshold? $\endgroup$
    – davipeix
    Commented Nov 10, 2021 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.