How do I fix an error when regionprops finds no blob?

조회 수: 3 (최근 30일)
Aaron Abel
Aaron Abel 2019년 12월 5일
댓글: Aaron Abel 2019년 12월 6일
In my binary image, there is no blob/ 0 pixel /white pixel. It shows me an error.
Here is my code:
Obj = regionprops(binaryImage, gambarAsliParkir1, 'Area' , 'BoundingBox');
[~, numbersOBJECT] = bwlabel(binaryImage);
clear P1;
for i=1:numbersOBJECT
area= Obj(i).Area;
P1(i)=area; %#ok<AGROW>
end
  댓글 수: 5
Aaron Abel
Aaron Abel 2019년 12월 6일
편집: Aaron Abel 2019년 12월 6일
I forgot to add the last syntax.
Obj = regionprops(binaryImage, gambarAsliParkir1, 'Area' , 'BoundingBox');
[~, numbersOBJECT] = bwlabel(binaryImage);
clear P1;
for i=1:numbersOBJECT
area= Obj(i).Area;
P1(i)=area; %#ok<AGROW>
end
pixel1=sum(P1);
the error is
Undefined function or variable 'P1'.
Yes, as you said, P1 is undefined.
I need to sum the number of the pixels and find the largest object, then I can crop it, if there are objects in binaryImage.
Thank you very much, I found it succesful that I replaced the loop with
P1 = [Obj.Area];
At the moment, I don't need
bwlabel
anymore.
But, I find this problem
Obj = regionprops(binaryImage, gambarAsliParkir1, 'Area' , 'BoundingBox');
clear ruangParkir1;
P1=[Obj.Area];
pixel1=sum(P1);
pixel1max=max(P1);
P1=P1==pixel1max;
ObjBB = Obj(P1).BoundingBox;
Image1= imcrop(gambarAsliParkir1, ObjBB);
This is the error.
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Error in untitled>pushbutton3_Callback (line 677)
ObjBB = Obj(P1).BoundingBox;
Walter Roberson
Walter Roberson 2019년 12월 6일
Your code is ambiguous in the case that the two largest blobs have exactly the same area; what do you want to do in that case?

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

채택된 답변

Image Analyst
Image Analyst 2019년 12월 6일
편집: Image Analyst 2019년 12월 6일
Label it before
[labeledImage, numberOfRegions] = bwlabel(binaryImage);
props = regionprops(labeledImage, gambarAsliParkir1, 'Area' , 'BoundingBox');
allAreas = [props.Area] % Vector with list of all the areas.
pixel1 = sum(allAreas) % Sum them up
No for loop needed.
You really don't even need to call bwlabel, though it's often useful to get a labeled image if you want to do something like filtering on computer/derived measurements (like circularity), or to give unique colors to individual blobs with label2rgb() which makes it easy to determine if nearby blobs are connected or not. So you could do
props = regionprops(binaryImage, gambarAsliParkir1, 'Area' , 'BoundingBox');
allAreas = [props.Area] % Vector with list of all the areas.
pixel1 = sum(allAreas) % Sum them up
  댓글 수: 1
Aaron Abel
Aaron Abel 2019년 12월 6일
Thank you for answering, I appreciate it. Your information is useful. I will try it.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by