微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

OpenCV Python 频域滤波结果不如预期

如何解决OpenCV Python 频域滤波结果不如预期

希望你们一切顺利。

现在我正在做作业,需要我在频域中创建高通滤波器。我创建了所有过滤器。在我应用我的过滤器之后(当然在将输入图像转换到频域然后应用逆傅立叶到空间域之后),输出图像是紫色色调,而输入图像是灰度级。这是我的理想高通滤波器的输出

enter image description here

这是输入图像:

enter image description here

这是我的高通滤波器功能

import numpy as np

# The function takes two dimension inputs for the filter image;
# the third filter is D0,which defines the circle area of the High Pass Filter.
def idealHighPass(M,N,D0):
    # Initializing the filter with ones; since the filter is a complex function,# it has two channels,representing the real and imaginary parts:
    filter = np.ones((M,2),dtype=np.uint8)
    
    # Scanning through each pixel and calculating the distance of each pixel
    # to the image center. If the pixel is within D0,it is changed to 0:
    for i in range(M):
        for j in range(N):
           filter[i][j] = 0

    return filter

这是我的主要代码

import cv2
import numpy as np
from matplotlib import pyplot as plt
from idealHighPass import idealHighPass
from highButterworth import highButterworth
from highGaussian import highGaussian

#img = cv2.imread("airfield-05small-auto.tif")
img = cv2.imread("input.png")
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

m,n = img.shape

ft = cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)
ft_shifted = dft_shift = np.fft.fftshift(ft)

# - IDEAL HIGH-PASS FILTER EXECUTION START -

filter = idealHighPass(m,n,80)
filter = filter.astype((np.float32))

applied = ft_shifted * filter
fshift_mask_mag = 20 * np.log(cv2.magnitude(applied[:,:,0],applied[:,1]))
f_ishift = np.fft.ifftshift(applied)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,img_back[:,1])
cv2.imwrite("high_passed.png",img_back)
print(img_back.shape)

imgplot = plt.imshow(img_back)
plt.show()

# - IDEAL HIGH-PASS FILTER EXECUTION END -

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。