简体   繁体   English

如何使过滤器在此代码中工作?

[英]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.

相关问题 如何在django中使这个自定义过滤器工作? - How can I make this custom filter work in django? 我如何制作这个过滤器 - How i can make this filter 如何减少代码行并使其与其他数据一起使用? - How can I reduce lines of code and make it work with other data? 如果输入已经预先确定,我怎样才能使我的代码工作? - How can I make my code work if the input is already predetermined? 如何使此代码用于 Tkinter 中的多项选择测验? - How can I make this code for a multiple choice quiz work in Tkinter? 如果输入是元组,我怎样才能使我的代码工作? - How can I make my code work if the input is a tuple? 我该怎么做才能使此代码起作用? - What can I do to make this code work? 我可以重写这段代码以使其运行得更快吗? - Can i rewrite this code to make it work faster? 安装tensorflow后如何使anaconda spyder代码完成再次工作 - how can i make anaconda spyder code completion work again after installing tensorflow 如何使这个微小的 Python 2.7 复数代码片段与 Python 3 一样工作? - How can I make this tiny Python 2.7 complex number code snippet work the same with Python 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM