在频域中应用高斯滤波器的低通和拉普拉斯



我正在尝试在频域中应用这两个滤波器。首先是低通滤波器,其次是高斯滤波器的拉普拉斯滤波器。 尽管我的图像被正确过滤,但输出是环绕的。此外,输出图像被移动(看起来好像图像已被复制(。

下面是输入和输出: 之前和之后过滤器

这是我的代码:

# Padding the image
image = Pad(image)
# The mask for low-pass filter
rows, cols = image.shape
center = (rows, cols)
crow, ccol = rows/2, cols/2
Low_mask = np.zeros((rows, cols), dtype=np.float32)
Low_mask[crow-cutoff:crow+cutoff, ccol-cutoff:ccol+cutoff] = 1
# Shifting the mask (low-pass)
Low_mask_dft = np.fft.fft2(Low_mask)
Low_mask_dft_shift  = np.fft.fftshift(Low_mask_dft)
# Shifting the image
image_dft = np.fft.fft2(image)
image_dft_shift = np.fft.fftshift(image_dft)
# Performing the convolution
image_fdomain = np.multiply(image_dft_shift, Low_mask_dft_shift)
# Shifting the mask (LOG)
LOGmask = GaussKernel(center)
LOGmask_dft = np.fft.fft2(LOGmask)
LOGmask_dft_shift = np.fft.fftshift(LOGmask_dft)
# Performing the convolution
frequency_image = np.multiply(image_fdomain, LOGmask_dft_shift)
# Now, return the image back to it's original form
result = np.fft.ifftshift(frequency_image)
result = np.fft.ifft2(result)
result = np.absolute(result)
return result

你需要做的是决定你正在使用的边界条件。
频域(对于离散数据(的自然是循环/循环卷积,这意味着圆形边界条件。

一旦您设置并相应地准备数据,一切都会按要求工作。

我创建了小的MATLAB脚本(您将能够轻松地在Python中复制它(来展示应该如何完成它。

最主要的是:

numRows = size(mI, 1);
numCols = size(mI, 2);
% Convolution in Spatial Domain
% Padding for Cyclic Convolution
mOGaussianRef = conv2(PadArrayCircular(mI, kernelRadius), mGaussianKernel, 'valid');
mOLogRef = conv2(PadArrayCircular(mI, kernelRadius), mLog, 'valid');
% Convolution in Frequency Domain
% Padding and centering of the Kernel
mGaussianKernel(numRows, numCols) = 0;
mGaussianKernel = circshift(mGaussianKernel, [-kernelRadius, -kernelRadius]);
mLog(numRows, numCols) = 0;
mLog = circshift(mLog, [-kernelRadius, -kernelRadius]);
mOGaussian  = ifft2(fft2(mI) .* fft2(mGaussianKernel), 'symmetric');
mOLog       = ifft2(fft2(mI) .* fft2(mLog), 'symmetric');
convErr = norm(mOGaussianRef(:) - mOGaussian(:), 'inf');
disp(['Gaussian Kernel - Cyclic Convolution Error (Infinity Norm) - ', num2str(convErr)]);
convErr = norm(mOLogRef(:) - mOLog(:), 'inf');
disp(['LoG Kernel - Convolution Error (Infinity Norm) - ', num2str(convErr)]);

这导致:

Gaussian Kernel - Cyclic Convolution Error (Infinity Norm) - 3.4571e-06
LoG Kernel - Convolution Error (Infinity Norm) - 5.2154e-08

也就是说,它做了它应该做的事情。

我的 Stack Overflow Q50614085 Github 存储库中的完整代码。

如果您想了解如何针对其他边界条件(或线性卷积(执行此操作,请查看FreqDomainConv.m

最新更新