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

3 Ansichten (letzte 30 Tage)
Aaron Abel
Aaron Abel am 5 Dez. 2019
Kommentiert: Aaron Abel am 6 Dez. 2019
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 Kommentare
Aaron Abel
Aaron Abel am 6 Dez. 2019
Bearbeitet: Aaron Abel am 6 Dez. 2019
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 am 6 Dez. 2019
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 6 Dez. 2019
Bearbeitet: Image Analyst am 6 Dez. 2019
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 Kommentar
Aaron Abel
Aaron Abel am 6 Dez. 2019
Thank you for answering, I appreciate it. Your information is useful. I will try it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by