How to resolve "Index Exceeds Matrix Dimensions" error?
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!