简体   繁体   English

在Matlab问题中使用FFT进行二维反卷积

[英]2D Deconvolution using FFT in Matlab Problems

I have convoluted an image I created in matlab with a 2D Gaussian function which I have also defined in matlab and now I am trying to deconvolve the resultant matrix to see if I get the 2D Gaussian function back using the fft2 and ifft2 commands. 我在matlab中使用2D高斯函数创建了一个图像,我也在matlab中定义了这个图像,现在我试图对结果矩阵进行去卷积,看看是否使用fft2和ifft2命令得到了2D高斯函数。 However the matrix I get as a result is incorrect (to my knowledge). 但是我得到的矩阵不正确(据我所知)。 Here is the code for what I have done thus far: 这是我到目前为止所做的代码:

% Code for input image (img) [300x300 array] %code for input image(img)[300x300 array]

N = 100;
t = linspace(0,2*pi,50);
r = (N-10)/2;
circle = poly2mask(r*cos(t)+N/2+0.5, r*sin(t)+N/2+0.5,N,N);
img = repmat(circle,3,3);

% Code for 2D Gaussian Function with c = 0 sig = 1/64 (Z) [300x300 array] 二维高斯函数的%代码,c = 0 sig = 1/64(Z)[300x300数组]

x = linspace(-3,3,300);
y = x';
[X Y] = meshgrid(x,y);
Z = exp(-((X.^2)+(Y.^2))/(2*1/64));

% Code for 2D Convolution of img with Z (C) [599x599 array] 使用Z(C)[599x599数组]进行img二维卷积的%代码

C = conv2(img,Z);

% I have tested that this convolution is correct using cross section profile vectors for img and C and the resulting xy plots are what i expect from the convolution. %我已经使用img和C的横截面轮廓矢量测试了这个卷积是正确的,并且得到的xy图是我对卷积的期望。

% From my knowledge of convolution, the algorithm works as a multiplier in Fourier space, therefore by dividing the Fourier transform of my output (convoluted image) by my input (img) I should get back the point spread function (Z - 2D Gaussian function) after the inverse Fourier transform is applied to this result by division. 根据我对卷积的了解,该算法在傅立叶空间中作为乘法器,因此通过我的输入(im​​g)除以输出的傅立叶变换(旋转图像),我应该得到点扩散函数(Z - 2D高斯函数) )通过除法将逆傅立叶变换应用于该结果之后。

% Code for attempted 2D deconvolution 尝试2D反卷积的%代码

Fimg = fft2(img,599,599);

% zero padding added to increase result to 599x599 array 添加%零填充以将结果增加到599x599阵列

FC = fft2(C);
R = FC/Fimg;

% I now get this error prompt: Warning: Matrix is close to singular or badly scaled. %我现在得到这个错误提示:警告:矩阵接近单数或严重缩放。 Results may be inaccurate. 结果可能不准确。 RCOND = 2.551432e-22 RCOND = 2.551432e-22

iFR = ifft2(R);

I'm expecting iFR to be close to Z but I'm getting something completely different. 我期待iFR接近Z,但我得到了完全不同的东西。 It may be an approximation of Z with complex values but I can't seem to check it since I don't know how to plot a 3D complex matrix in matlab. 它可能是具有复杂值的Z的近似值,但我似乎无法检查它,因为我不知道如何在matlab中绘制3D复杂矩阵。 So if anyone can tell me whether my answer is correct or incorrect and how to get this deconvolution to work? 所以,如果有人能告诉我我的答案是正确还是不正确以及如何让这种去卷积起作用? I'd be much appreciated. 我将不胜感激。

R = FC/Fimg needs to be R = FC./Fimg; R = FC/Fimg需要为R = FC./Fimg; You need to do division element-wise. 你需要按元素划分。

Here are some Octave (version 3.6.2) plots of that deconvolved Gaussian. 这是去卷积高斯的一些Octave(版本3.6.2)图。

% deconvolve in frequency domain
Fimg = fft2(img,599,599);
FC = fft2(C);
R = FC ./ Fimg;
r = ifft2(R);

% show deconvolved Gaussian
figure(1);
subplot(2,3,1), imshow(img), title('image');
subplot(2,3,2), imshow(Z), title('Gaussian');
subplot(2,3,3), imshow(C), title('image blurred by Gaussian');
subplot(2,3,4), mesh(X,Y,Z), title('initial Gaussian');
subplot(2,3,5), imagesc(real(r(1:300,1:300))), colormap gray, title('deconvolved Gaussian');
subplot(2,3,6), mesh(X,Y,real(r(1:300,1:300))), title('deconvolved Gaussian');

% show difference between Gaussian and deconvolved Gaussian
figure(2);
gdiff = Z - real(r(1:300,1:300));
imagesc(gdiff), colorbar, colormap gray, title('difference between initial Gaussian and deconvolved Guassian');

在此输入图像描述在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM