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 Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by