Python上的拉普拉斯过滤器并不像我预期的那样工作

2024-06-17 08:58:04 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,希望你们都做得很好。我试图根据以下公式在Python上实现频域拉普拉斯滤波器,这是我从课堂讲稿中发现的:

enter image description here

这是我的Python函数:

import numpy as np
from matplotlib import pyplot as plt

# The function takes two dimension inputs for the filter image;
def highLaplacian(M, N):
    # Initializing the filter with ones; since the filter is a complex function,
    # it has two channels, representing the real and imaginary parts;
    # the data type is float32, since the pixels will take floating point values:
    filter = np.zeros((M, N, 2), dtype=np.float32)
    
    # Scanning through each pixel and calculating the negative of the sum of the
    # squares of the pixels, and assigning the value to the corresponding pixel
    # in the filter:
    for i in range(M):
        for j in range(N):
            filter[i][j] = -(i**2 + j**2)

    return filter

当我在一个图像上使用这个滤波器,这个图像被转移到傅里叶域;输出图像不会更改。我成功地实现了理想高通、巴特沃斯高通和高斯高通滤波器,这是我在本次讲座中使用的拉普拉斯滤波器。然而,我不明白为什么它对拉普拉斯人不起作用

这是在傅里叶域图像上使用拉普拉斯滤波器的主文件:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread("airfield-05small-auto.tif")
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)

filter = highLaplacian(m, n)
filterMag = 20 * np.log(cv2.magnitude(filter[:, :, 0], filter[:, :, 1]))

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[:, :, 0], img_back[:, :, 1])

imgplot = plt.imshow(img_back, cmap="gray")
plt.show()
cv2.imwrite("lap_output.png", filterMag)

Tags: andthe图像importimgforasnp