how to remove index exceeds matrix dimensions

1 次查看(过去 30 天)
I'm using this code,getting error ??? Index exceeds matrix dimensions.
Error in ==> aaaa at 8 c=cell2mat(BW(1));
K=imread('C:\Users\use\Documents\MATLAB\lrc1\abc.jpg');
B=im2bw(K);
C=imfill(B,'holes');
[Label,Total]=bwlabel(C,8);
num=4;
[row, col] = find(Label==num);
BW=bwboundaries(Label==num);
c=cell2mat(BW(1));
Perimeter=0;
for i=1:size(c,1)-1
Perimeter=Perimeter+sqrt((c(i,1)-c(i+1,1)).^2+(c(i,2)-c(i+1,2)).^2);
end
display(Perimeter); so can any one please help me to remove this??????
  3 个评论
ajay Hiremath
ajay Hiremath 2015-1-18
thank you for response but sir I replaced i to m so still the same error am getting.... what to do????
Stephen23
Stephen23 2015-1-18
编辑:Stephen23 2015-1-18
According to the documentation "bwboundaries returns B, a P-by-1 cell array, where P is the number of objects and holes." If BW is empty, then this means it cannot find any objects in the image that match the criteria you have used. You need to check that your code is really doing what you think it should be doing... this is called code debugging, and is an important skill to master.
In particular you need to consider what happens to your data at each step. It will help if you actually look at your data: try using all and any or some other tools to get some ideas of what each step has changed. For example I found that when I ran your code with an example image (I used the MATLAB logo), the matrix C consists entirely of trues, no falses anywhere:
>> find(~C)
ans =
Empty matrix: 0-by-1
So there are no objects in the image at this point. We can also display the data at each step:
K = imread('temp.jpg');
figure
imshow(K)
B = im2bw(K);
figure
imshow(B)
C = imfill(B,'holes');
figure
imshow(C)
The first image is the color logo, the second a B&W logo, and the third is all white, without any logo... your code C = imfill(B,'holes') has actually removed the object from the image. Read the documentation for imfill, and play around with it in your command window.
Do not write a script all in one go: this guarantees a problem like this one. Instead, write and test each line of code as you write it. Check the data at each step. Use the documentation.
I do not have your image so I cannot know where or why the problem is occurring: this is your job to bug-fix and improve the code.
It is also important to note that currently your code, because of this indexing BW(1) requires the existence of atleast one object in the image. What happens if there is no object in the image?

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2015-1-18
编辑:Stephen23 2015-1-18
Have a look at the variable BW in your workspace, or if this is a script then simply type BW in the command line. You will find that BW is (probably) empty, in which case trying to index the first element BW(1) does not make any sense as there is not first element, thus the error.
Do you expect BW to be not empty?
  4 个评论
ajay Hiremath
ajay Hiremath 2015-1-18
m getting perimeter=0 if c=cell2mat(BW) so it is not entering into loop sir...
Image Analyst
Image Analyst 2015-1-18
I don't know what that means. ajay, did you read my comment above and answer below? Specifically about attaching your image?

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2015-1-18
After the call to bwlabel, you should wrap the rest of the code in
if Total >= 1
so that it only does it if you actually have at least one blob in your binary image. Total is the total number of blobs in C. You don't want to do that code if C is totally devoid of blobs do you? So check for that.
Secondly, you can get the fourth boundary in C by using braces rather than cell2mat:
c = BW{1}; % Get boundary of the 4th blob in C.
by the way, c and BW are horribly deceptive and non-descriptive names. The Mathwork usually give the name BW to an image, not to a list of x,y coordinates like you did. And I'd call c "firstBoundary".
Third, the Perimeter is already calculated for you if you use regionprops(). What's wrong with doing that?
  2 个评论
ajay Hiremath
ajay Hiremath 2015-1-18
sir, then please send your code becos I did whatever you said and m getting perimeter=0 sir...
Image Analyst
Image Analyst 2015-1-18
Looks like Stephen solved it for you since this answer is marked as Accepted. However, if you still would like my code, upload your temp.jpg image, and I'd be happy to. By the way, it's not advised to use JPG images for image analysis because of the compression artifacts they have.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Denoising and Compression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by