Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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]

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]

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]

C = conv2(img,Z);

% I have tested that this convolution is correct using cross section profile vectors for img and C and the resulting x-y plots are what i expect from the convolution.

% 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.

% Code for attempted 2D deconvolution

Fimg = fft2(img,599,599);

% zero padding added to increase result to 599x599 array

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

iFR = ifft2(R);

I'm expecting iFR to be close to Z but I'm getting something completely different. 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. 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.

like image 905
Sean James Jamieson Avatar asked Nov 02 '22 13:11

Sean James Jamieson


1 Answers

R = FC/Fimg needs to be R = FC./Fimg; You need to do division element-wise.

like image 111
JustinBlaber Avatar answered Nov 09 '22 04:11

JustinBlaber