I get an error msg "index exceeds matrix dimensions". How can i solve this problem??? my code is given below..
Show older comments
clc; A = imread('C:\Users\asus\Desktop\2.jpg'); % P = rgb2gray(A); % imshow(P) % figure B = im2double(A); [m n] = size(A) L = imread('C:\Users\asus\Desktop\2.jpg'); % M = rgb2gray(L); % imshow(P) % figure N = im2double(L); % [m n] = size(N) RECT = [1 1 100 100]; R = imcrop(L,RECT); % imshow(P) % figure Q = im2double®; [p q] = size(Q) Q = reshape(R, p, q);
corr = 0;
for j=1:m-p for i=1:n-q % take a segment of the original image( same size as the template size) segment = B(i:(i-1)+p, j:(j-1)+q, :); % HERE I GET THAT ERROR
segment = reshape(segment, p, q);
output = corr2(Q, segment);
if output > corr
corr = output;
% disp(corr)
x = i;
y = j;
end
if corr==1
break;
end
end
if corr==1
break;
end
end
if corr==1 disp('match found') figure; subplot(1,2,1), imshow®, title('Template'); subplot(1,2,2), imshow(B(x:x+p, y:y+q, :)),title('Segment of the similar part'); else disp('match not found') % figure; imshow®, title('Template'); end
1 Comment
Jan
on 27 Apr 2014
Do you see, that your code is not readable? Have you seen the "{} Code" button. Press it to learn, how it is used.
Please post the complete error message, which contains e.g. also some information about the line, which causes the problem. The less we have to guess, the easier is an answer.
Answers (2)
Geoff Hayes
on 27 Apr 2014
Edited: Geoff Hayes
on 27 Apr 2014
Hi vinaya,
Put a breakpoint at the line that is causing the problem. You have identified already where that is, so that is great! The error that you observe is indicating that you are trying to access a matrix with an index that exceeds the dimensions of the matrix. For example, the following generates the error:
>> x=zeros(1,6); % a vector with six columns
>> x(7); % no index seven into the vector and so the error
Examine your inputs to B: either the row i:(i-1)+p or column j:(j-1)+q, :) indices are causing the problem. It may be that your segments don't evenly divide your image (B) and so you will have to compensate for this.
Geoff
Image Analyst
on 27 Apr 2014
I know what the problem is. You say
segment = B(i:(i-1)+p, j:(j-1)+q, :);
well, B came from A, so is A a color image? Well you say
[m n] = size(A)
so A had better not be a color image or else (the badly-named) n will not be the number of columns in the image but will be the number of columns times the number of color channels. For example, with a 256 by 256 by 3 color image, n = 256*3 = 768. But then you have this loop over i
for i=1:n-q
so the number of rows is going up what you think is the number of columns minus q (which is bizarre in itself that the rows index depends on the number of columns), but the thing is that n will be way more than the number of columns and eventually so will i and you'll see the error you're seeing.
First, read this: http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
Second, change the line to this:
[rows, columns, numberOfColorChannels] = size(A)
Third, make sure you're looping over the right indexes. The first index is rows and the second is columns. In a nested for loop, the inner loop should be over rows and the outer loop over columns.
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!