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

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Aaron Abel
Aaron Abel il 6 Dic 2019
Modificato: Aaron Abel il 6 Dic 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 il 6 Dic 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?

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 6 Dic 2019
Modificato: Image Analyst il 6 Dic 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 Commento
Aaron Abel
Aaron Abel il 6 Dic 2019
Thank you for answering, I appreciate it. Your information is useful. I will try it.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Convert Image Type in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by