我需要将 HPF 和 LPF 应用于傅里叶图像并执行逆变换,然后比较它们。我执行以下算法,但没有结果:
img = cv2.imread('pic.png')
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift))
# need to add HPF and LPF
hpf = ...
lpf = ... # maybe 1 - hpf ?
# inverse
result = (lpf + (1 + alpha) * hpf)
Run Code Online (Sandbox Code Playgroud)
你能告诉我该怎么做吗?
fmw*_*w42 13
您使用白色圆圈黑色背景并将其应用于 FFT 幅度以执行低通滤波器。高通滤波器是低通滤波器的相反极性——白底黑圈。您可以通过对圆应用高斯滤波器来减轻结果中的“振铃”效应。这是低通滤波器的示例。
输入:
import numpy as np
import cv2
# read input and convert to grayscale
img = cv2.imread('lena.png')
# do dft saving as complex output
dft = np.fft.fft2(img, axes=(0,1))
# apply shift of origin to center of image
dft_shift = np.fft.fftshift(dft)
# generate spectrum from magnitude image (for viewing only)
mag = np.abs(dft_shift)
spec = np.log(mag) / 20
# create circle mask
radius = 32
mask = np.zeros_like(img)
cy = mask.shape[0] // 2
cx = mask.shape[1] // 2
cv2.circle(mask, (cx,cy), radius, (255,255,255), -1)[0]
# blur the mask
mask2 = cv2.GaussianBlur(mask, (19,19), 0)
# apply mask to dft_shift
dft_shift_masked = np.multiply(dft_shift,mask) / 255
dft_shift_masked2 = np.multiply(dft_shift,mask2) / 255
# shift origin from center to upper left corner
back_ishift = np.fft.ifftshift(dft_shift)
back_ishift_masked = np.fft.ifftshift(dft_shift_masked)
back_ishift_masked2 = np.fft.ifftshift(dft_shift_masked2)
# do idft saving as complex output
img_back = np.fft.ifft2(back_ishift, axes=(0,1))
img_filtered = np.fft.ifft2(back_ishift_masked, axes=(0,1))
img_filtered2 = np.fft.ifft2(back_ishift_masked2, axes=(0,1))
# combine complex real and imaginary components to form (the magnitude for) the original image again
img_back = np.abs(img_back).clip(0,255).astype(np.uint8)
img_filtered = np.abs(img_filtered).clip(0,255).astype(np.uint8)
img_filtered2 = np.abs(img_filtered2).clip(0,255).astype(np.uint8)
cv2.imshow("ORIGINAL", img)
cv2.imshow("SPECTRUM", spec)
cv2.imshow("MASK", mask)
cv2.imshow("MASK2", mask2)
cv2.imshow("ORIGINAL DFT/IFT ROUND TRIP", img_back)
cv2.imshow("FILTERED DFT/IFT ROUND TRIP", img_filtered)
cv2.imshow("FILTERED2 DFT/IFT ROUND TRIP", img_filtered2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# write result to disk
cv2.imwrite("lena_dft_numpy_mask.png", mask)
cv2.imwrite("lena_dft_numpy_mask_blurred.png", mask2)
cv2.imwrite("lena_dft_numpy_roundtrip.png", img_back)
cv2.imwrite("lena_dft_numpy_lowpass_filtered1.png", img_filtered)
cv2.imwrite("lena_dft_numpy_lowpass_filtered2.png", img_filtered2)
Run Code Online (Sandbox Code Playgroud)
掩模 1(低通滤波器):
掩模 2(低通滤波器模糊):
结果1:
结果 2(振铃减少):
添加
这里是高通滤波器处理(边缘检测器)。
import numpy as np
import cv2
# read input and convert to grayscale
#img = cv2.imread('lena_gray.png', cv2.IMREAD_GRAYSCALE)
img = cv2.imread('lena.png')
# do dft saving as complex output
dft = np.fft.fft2(img, axes=(0,1))
# apply shift of origin to center of image
dft_shift = np.fft.fftshift(dft)
# generate spectrum from magnitude image (for viewing only)
mag = np.abs(dft_shift)
spec = np.log(mag) / 20
# create white circle mask on black background and invert so black circle on white background
radius = 32
mask = np.zeros_like(img)
cy = mask.shape[0] // 2
cx = mask.shape[1] // 2
cv2.circle(mask, (cx,cy), radius, (255,255,255), -1)[0]
mask = 255 - mask
# blur the mask
mask2 = cv2.GaussianBlur(mask, (19,19), 0)
# apply mask to dft_shift
dft_shift_masked = np.multiply(dft_shift,mask) / 255
dft_shift_masked2 = np.multiply(dft_shift,mask2) / 255
# shift origin from center to upper left corner
back_ishift = np.fft.ifftshift(dft_shift)
back_ishift_masked = np.fft.ifftshift(dft_shift_masked)
back_ishift_masked2 = np.fft.ifftshift(dft_shift_masked2)
# do idft saving as complex output
img_back = np.fft.ifft2(back_ishift, axes=(0,1))
img_filtered = np.fft.ifft2(back_ishift_masked, axes=(0,1))
img_filtered2 = np.fft.ifft2(back_ishift_masked2, axes=(0,1))
# combine complex real and imaginary components to form (the magnitude for) the original image again
# multiply by 3 to increase brightness
img_back = np.abs(img_back).clip(0,255).astype(np.uint8)
img_filtered = np.abs(3*img_filtered).clip(0,255).astype(np.uint8)
img_filtered2 = np.abs(3*img_filtered2).clip(0,255).astype(np.uint8)
cv2.imshow("ORIGINAL", img)
cv2.imshow("SPECTRUM", spec)
cv2.imshow("MASK", mask)
cv2.imshow("MASK2", mask2)
cv2.imshow("ORIGINAL DFT/IFT ROUND TRIP", img_back)
cv2.imshow("FILTERED DFT/IFT ROUND TRIP", img_filtered)
cv2.imshow("FILTERED2 DFT/IFT ROUND TRIP", img_filtered2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# write result to disk
cv2.imwrite("lena_dft_numpy_mask_highpass.png", mask)
cv2.imwrite("lena_dft_numpy_mask_highpass_blurred.png", mask2)
cv2.imwrite("lena_dft_numpy_roundtrip.png", img_back)
cv2.imwrite("lena_dft_numpy_highpass_filtered1.png", img_filtered)
cv2.imwrite("lena_dft_numpy_highpass_filtered2.png", img_filtered2)
Run Code Online (Sandbox Code Playgroud)
掩模 1(高通滤波器):
掩模 2(高通滤波器模糊):
结果1:
结果2:
加法2
这里是高boost滤波器处理。高增强滤波器是一种锐化滤波器,只是 1 + 分数 * 高通滤波器。请注意,为了便于使用和解释,此处的高通滤波器创建的范围是 0 到 1,而不是 0 到 255。
import numpy as np
import cv2
# read input and convert to grayscale
#img = cv2.imread('lena_gray.png', cv2.IMREAD_GRAYSCALE)
img = cv2.imread('lena.png')
# do dft saving as complex output
dft = np.fft.fft2(img, axes=(0,1))
# apply shift of origin to center of image
dft_shift = np.fft.fftshift(dft)
# generate spectrum from magnitude image (for viewing only)
mag = np.abs(dft_shift)
spec = np.log(mag) / 20
# create white circle mask on black background and invert so black circle on white background
# as highpass filter
radius = 32
mask = np.zeros_like(img, dtype=np.float32)
cy = mask.shape[0] // 2
cx = mask.shape[1] // 2
cv2.circle(mask, (cx,cy), radius, (1,1,1), -1)[0]
mask = 1 - mask
# high boost filter (sharpening) = 1 + fraction of high pass filter
mask = 1 + 0.5*mask
# blur the mask
mask2 = cv2.GaussianBlur(mask, (19,19), 0)
# apply mask to dft_shift
dft_shift_masked = np.multiply(dft_shift,mask)
dft_shift_masked2 = np.multiply(dft_shift,mask2)
# shift origin from center to upper left corner
back_ishift = np.fft.ifftshift(dft_shift)
back_ishift_masked = np.fft.ifftshift(dft_shift_masked)
back_ishift_masked2 = np.fft.ifftshift(dft_shift_masked2)
# do idft saving as complex output
img_back = np.fft.ifft2(back_ishift, axes=(0,1))
img_filtered = np.fft.ifft2(back_ishift_masked, axes=(0,1))
img_filtered2 = np.fft.ifft2(back_ishift_masked2, axes=(0,1))
# combine complex real and imaginary components to form (the magnitude for) the original image again
img_back = np.abs(img_back).clip(0,255).astype(np.uint8)
img_filtered = np.abs(img_filtered).clip(0,255).astype(np.uint8)
img_filtered2 = np.abs(img_filtered2).clip(0,255).astype(np.uint8)
cv2.imshow("ORIGINAL", img)
cv2.imshow("SPECTRUM", spec)
cv2.imshow("MASK", mask)
cv2.imshow("MASK2", mask2)
cv2.imshow("ORIGINAL DFT/IFT ROUND TRIP", img_back)
cv2.imshow("FILTERED DFT/IFT ROUND TRIP", img_filtered)
cv2.imshow("FILTERED2 DFT/IFT ROUND TRIP", img_filtered2)
cv2.waitKey(0)
cv2.destroyAllWindows()
# write result to disk
cv2.imwrite("lena_dft_numpy_roundtrip.png", img_back)
cv2.imwrite("lena_dft_numpy_highboost_filtered1.png", img_filtered)
cv2.imwrite("lena_dft_numpy_highboost_filtered2.png", img_filtered2)
Run Code Online (Sandbox Code Playgroud)
结果1:
结果2:
| 归档时间: |
|
| 查看次数: |
9330 次 |
| 最近记录: |