MATLAB: How to fix an error when regionprops finds no blob

digital image processingimage analysisimage processingImage Processing Toolboximage segmentationMATLAB

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

Best Answer

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