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 (将#修改为@)