[英]How can I make the filter work in this code?
I am trying to apply the Butterworth low pass filter on the image but for some reason the values in lpFilter is not copied in lpFilter_matrix I tried to do it with a nested loop but still didn't work and the values of lpFilter_matrix remained zero.我正在尝试在图像上应用 Butterworth 低通滤波器,但由于某种原因,lpFilter 中的值没有复制到 lpFilter_matrix 我尝试使用嵌套循环但仍然没有工作,并且 lpFilter_matrix 的值保持为零。
import cv2
import numpy as np
from matplotlib import pyplot as plt
image=cv2.imread(r"C:/Users/Admin/Pictures/eee.jpg",0)
image_float32=np.float32(image)
dft =cv2.dft(image_float32,flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift=np.fft.fftshift(dft)
magnitude_spectrum=np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
rows, cols = image.shape
crow, ccol = rows//2 , cols//2
r, c = np.mgrid[0:rows:1, 0:cols:1]
c -= crow
r -= ccol
d = np.sqrt(np.power(r, 2.0) + np.power(c, 2.0))
lpFilter_matrix = np.zeros((rows, cols, 2), np.uint8)
d0=10
n=2
lpFilter = 1.0 / (1 + np.power(d/d0, 2*n))
lpFilter_matrix[:, :, 0] = lpFilter
lpFilter_matrix[:, :, 1] = lpFilter
fshift = dft_shift*lpFilter_matrix
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
plt.figure(figsize=(14,9))
plt.subplot(121)
plt.imshow(image, cmap = 'gray')
plt.title('Input Image')
plt.axis('off')
plt.subplot(122)
plt.imshow(img_back, cmap = 'gray')
plt.title('filtered Image')
plt.axis('off')
I think your issue is that your filter needs to be float and not 8-bit.我认为您的问题是您的过滤器需要浮动而不是 8 位。 So as I said in my comment, change this line.
所以正如我在评论中所说,改变这一行。
lpFilter_matrix = np.zeros((rows, cols, 2), np.uint8) with lpFilter_matrix = np.zeros((rows, cols, 2), np.float32 ) lpFilter_matrix = np.zeros((rows, cols, 2), np.uint8) 与 lpFilter_matrix = np.zeros((rows, cols, 2), np.float32 )
So this works for me in Python/OpenCV.所以这在 Python/OpenCV 中对我有用。
Input:输入:
import cv2
import numpy as np
from matplotlib import pyplot as plt
# read image as grayscale and convert to float
image=cv2.imread("lena.jpg",0)
image_float32=np.float32(image)
# do dft
dft =cv2.dft(image_float32,flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift=np.fft.fftshift(dft)
# get the magnitude
magnitude_spectrum=np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
# initialize filter and center it
rows, cols = image.shape
crow, ccol = rows//2 , cols//2
r, c = np.mgrid[0:rows:1, 0:cols:1]
c -= crow
r -= ccol
# make filter circular
d = np.sqrt(np.power(r, 2.0) + np.power(c, 2.0))
lpFilter_matrix = np.zeros((rows, cols, 2), np.float32)
# specify filter arguments
d0=10
n=2
# compute butterworth filter
lpFilter = 1.0 / (1 + np.power(d/d0, 2*n))
lpFilter_matrix[:, :, 0] = lpFilter
lpFilter_matrix[:, :, 1] = lpFilter
# apply filter
fshift = dft_shift*lpFilter_matrix
# do idft
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
# convert back to real output from complex
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
fig = plt.figure(figsize=(14,9))
plt.subplot(121)
plt.imshow(image, cmap = 'gray')
plt.title('Input Image')
plt.axis('off')
plt.subplot(122)
plt.imshow(img_back, cmap = 'gray')
plt.title('filtered Image')
plt.axis('off')
plt.show()
fig.savefig('lena_butterworth.jpg')
plt.close(fig)
Results:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.