How to resolve "Index Exceeds Matrix Dimensions" error?

조회 수: 5 (최근 30일)
Issara Laosuwan
Issara Laosuwan 2016년 6월 3일
댓글: Issara Laosuwan 2016년 6월 3일
Hello! I tried to create a low-pass filter to mask for an image regarding to filtering of an image in frequency domain.
After I have compute my code, it return with a "Index Exceeds Matrix Dimensions" error.
warning off
a = imread('valley.jpg');
b = im2double(a);
[m,n] = size(b);
c = zeros(2*m,2*n);
[p,q] = size(c);
for i=1:p
for j=1:q
if i<=m && j<=n
c(i,j)= b(i,j);
else
c(i,j) = 0;
end
end
end
imshow(b); title('Original Image');
figure
imshow(c); title('Padded Image');
d = zeros(p,q);
for i=1:p
for j=1:q
d(i,j) = c(i,j).*(-1).^(i+j);
end
end
figure
imshow(d); title('Pre Processed Image for Calculating DFT');
e = fft2(d);
figure
imshow(e); title('2D DFT of the Pre Process Image');
[x,y] = freqspace(p,'meshgrid');
z = zeros(p,q);
for i=1:p
for j=1:q
z(i,j) = sqrt((x(i,j).^2)+(y(i,j).^2));
end
end
H = zeros(p,q);
for i=1:p
for j=1:q
if z(i,j)<=0.4
H(i,j)=1;
else
H(i,j)=0;
end
end
end
figure
imshow(H);title('Low Pass Filter Mask');
Index exceeds matrix dimensions.
Error in freqD(line 39)
z(i,j) = sqrt((x(i,j).^2)+(y(i,j).^2));
It would be kind of you guys to guide me for possible solution.
Thank you!

채택된 답변

Walter Roberson
Walter Roberson 2016년 6월 3일
When you pass in a scalar to freqspace() with 'meshgrid', you will get out a p by p matrix. You are trying to iterate to p by q.
In turn, your q is set as 2*n where
[m,n] = size(b);
and b is im2double() of your JPEG image. JPEG images are almost never 2D (only rare grayscale JPEG images are 2D), so you are very likely taking size() of a 3D image with two outputs instead of three outputs. When you do that, the second output is not going to be the number of columns, it is going to be the number of columns times the number of panes (3). To put it another way, when you use size(b) the product of the outputs is always going to exactly equal the number of elements in the matrix, rows times columns times panes, but your code is assuming it only returns rows and columns.
So your first resolution step is
[m, n, panes] = size(b);
That will fix n being 3 times to large. Then the step after that will be to pass [p q] to freqspace instead of just p
  댓글 수: 1
Issara Laosuwan
Issara Laosuwan 2016년 6월 3일
Thank you! I follow your guide. It work just fine now.
Just for curiosity:
Instead of
[m, n, panes] = size(b);
I can just convert the image into gray scale, and it would also work right?

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

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by