为什么在cv2filter2d,np.ff和np.ifft之后移动图像?

发布于 2025-01-24 11:13:53 字数 2374 浏览 4 评论 0原文

我用核心将图像震撼,然后试图在傅立叶域中进行反应。但是,重建的图像发生了变化吗?这样做的原因是什么?我的代码有问题吗?

原始图像:

“苹果”

重建图像:

“

代码复制:

import matplotlib.pyplot as plt
import numpy as np

def zero_pad_to_same_size(target,ref):
    border_pad, remainder = np.divmod(np.array(ref.shape) - np.array(target.shape), 2)
    top = border_pad[0]
    bottom = border_pad[0] + remainder[0]
    left = border_pad[1]
    right = border_pad[1] + remainder[1]
    result = cv2.copyMakeBorder(target, top, bottom, left, right, borderType=cv2.BORDER_CONSTANT, value=0)
    return result

path = r"apple.jpg"

img = cv2.imread(path,flags=0)
img = cv2.normalize(img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_64F)
plt.figure(), plt.imshow(img), plt.colorbar(), plt.title("img")
img_resize = cv2.resize(img,dsize=(301,301))

# create the gauss kernel
ksize = 31
sigma = 1
gaussian_kernel = cv2.getGaussianKernel(ksize=ksize, sigma=sigma)
kernel_2D = gaussian_kernel @ gaussian_kernel.transpose()
plt.figure(), plt.imshow(kernel_2D), plt.colorbar(), plt.title("gaussian kernel")

# do the convolution with the gauss kernel
blurred_img = cv2.filter2D(src=img_resize, ddepth=-1, kernel=kernel_2D, borderType=cv2.BORDER_CONSTANT)
plt.figure(), plt.imshow(blurred_img), plt.colorbar(), plt.title("blurred with gaussian")

# pad the gauss kernel, so the img and the gauss kernel have the same size
kernel_2D_padded = zero_pad_to_same_size(target=kernel_2D,ref=blurred_img)

blurred_img_fft = np.fft.fft2(blurred_img)
kernel_2D_padded_fft = np.fft.fft2(kernel_2D_padded)

reconst = np.fft.ifft2(blurred_img_fft/kernel_2D_padded_fft)
plt.figure(), plt.imshow(np.abs(reconst)), plt.colorbar(), plt.title("reconst simple psf padded")

# wiener
SNR = 1 / 0.2
wiener = np.fft.ifftn((kernel_2D_padded_fft*np.conjugate(kernel_2D_padded_fft)/((kernel_2D_padded_fft*np.conjugate(kernel_2D_padded_fft)+1/SNR)))*blurred_img_fft/kernel_2D_padded_fft)
plt.figure(), plt.imshow(np.abs(wiener)), plt.colorbar(), plt.title("reconst wiener")
plt.show()

I convolute an image with a kernel and then trying to deconvolute in the Fourier domain. However, the reconstructed image is shifted? What is the reason for this? Is something wrong with my code?

Original image:

apple

Reconstructed image:

reconst

Code to reproduce:

import matplotlib.pyplot as plt
import numpy as np

def zero_pad_to_same_size(target,ref):
    border_pad, remainder = np.divmod(np.array(ref.shape) - np.array(target.shape), 2)
    top = border_pad[0]
    bottom = border_pad[0] + remainder[0]
    left = border_pad[1]
    right = border_pad[1] + remainder[1]
    result = cv2.copyMakeBorder(target, top, bottom, left, right, borderType=cv2.BORDER_CONSTANT, value=0)
    return result

path = r"apple.jpg"

img = cv2.imread(path,flags=0)
img = cv2.normalize(img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_64F)
plt.figure(), plt.imshow(img), plt.colorbar(), plt.title("img")
img_resize = cv2.resize(img,dsize=(301,301))

# create the gauss kernel
ksize = 31
sigma = 1
gaussian_kernel = cv2.getGaussianKernel(ksize=ksize, sigma=sigma)
kernel_2D = gaussian_kernel @ gaussian_kernel.transpose()
plt.figure(), plt.imshow(kernel_2D), plt.colorbar(), plt.title("gaussian kernel")

# do the convolution with the gauss kernel
blurred_img = cv2.filter2D(src=img_resize, ddepth=-1, kernel=kernel_2D, borderType=cv2.BORDER_CONSTANT)
plt.figure(), plt.imshow(blurred_img), plt.colorbar(), plt.title("blurred with gaussian")

# pad the gauss kernel, so the img and the gauss kernel have the same size
kernel_2D_padded = zero_pad_to_same_size(target=kernel_2D,ref=blurred_img)

blurred_img_fft = np.fft.fft2(blurred_img)
kernel_2D_padded_fft = np.fft.fft2(kernel_2D_padded)

reconst = np.fft.ifft2(blurred_img_fft/kernel_2D_padded_fft)
plt.figure(), plt.imshow(np.abs(reconst)), plt.colorbar(), plt.title("reconst simple psf padded")

# wiener
SNR = 1 / 0.2
wiener = np.fft.ifftn((kernel_2D_padded_fft*np.conjugate(kernel_2D_padded_fft)/((kernel_2D_padded_fft*np.conjugate(kernel_2D_padded_fft)+1/SNR)))*blurred_img_fft/kernel_2D_padded_fft)
plt.figure(), plt.imshow(np.abs(wiener)), plt.colorbar(), plt.title("reconst wiener")
plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文