为什么在cv2filter2d,np.ff和np.ifft之后移动图像?
我用核心将图像震撼,然后试图在傅立叶域中进行反应。但是,重建的图像发生了变化吗?这样做的原因是什么?我的代码有问题吗?
原始图像:
重建图像:
代码复制:
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:
Reconstructed image:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论