Matrix Dimensions Must Agree

I have the following code and am trying to apply the Gaussian filter: I do not know how the fix the matrix dimensions mismatch problem in this case and in general. Can I request some help. I wasnt able to fix this on my own.
CODE:
clc;
I = imread('C:\Documents and Settings\122244\Desktop\CT.JPG');
figure, imshow(I,[]);
J = fftshift(fft2(I));
L = log(1+abs(J));
figure, imshow(mat2gray(L),[]);
[j,k] = size(J);
gs=zeros(j,k);
sig=20;
for m=1:j;
for n=1:k;
gs(m,n)=exp(-((m-366)^2+(n-366)^2)/(2*sig^2));
end
end
gs = fftshift(fft2(gs));
M = log(1+abs(gs));
figure, imshow(M,[]);
FLT = gs.*J;% Error line where I get matrix dimension error.
FI = abs((ifft2(FLT)));
figure,imshow(FI,[]);
I couldnt find any help online as well.
Regards, ---Ish

 채택된 답변

Sean de Wolski
Sean de Wolski 2012년 3월 6일

0 개 추천

dbstop if error
then run your file
what is?
size(gs)
size(J)
It should be obvious at this point.

댓글 수: 5

Ishtiaq Bercha
Ishtiaq Bercha 2012년 3월 6일
J = 512 x 512 x 3
gs = 512 x 1536
As you see gs is based on the size of J which is a CT image. However it seems like gs is simply taking the product of the second and third dimension of the array J.
what is dbstop if error. I guess I will add it to the code.
Believe me or not I am doing this like for the first time, programing with MATLAB that is.
Sean de Wolski
Sean de Wolski 2012년 3월 6일
Welcome to MATLAB (and Answers)!
It sounds like gs had its 2nd and 3rd slice transformed to 2d. This is because you used fft2 on the whole array at once.
Instead, use fft2 on each slice of the array:
for ii = 3:-1:1
gs(:,:,ii) = fftshift(fft2(gs(:,:,ii)));
end
That is of course unless you wanted fftn - n-dimensional fft. It's hard to say which is correct not knowing the application or the data.
Ishtiaq Bercha
Ishtiaq Bercha 2012년 3월 6일
The thing is that I am deling with a single 2D image and for some odd reason I am getting 512 x 512 x 3. There arent more then one slices. There is only one image. Is it possible to get rid of 3? I think it will resolve the rest of the issues automatically since gs is defined off of J.
Thanks
---Ish
Sean de Wolski
Sean de Wolski 2012년 3월 6일
So it's being read in as rgb with all three slices (r,g,b) being the same? If this is the case:
gs = gs(:,:,1); % keep only first slice.
Ishtiaq Bercha
Ishtiaq Bercha 2012년 3월 6일
I did that to the original image and it seems to have resolved the issue! Thanks a bunch!
---Ish

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by