MATLAB: Regionprops 0 Area NaN Centroid issue

areaboundingboxcentroidimageImage Processing Toolboxmissing labelsnanprocessinguint16zero

I am using "regionprops" for image analysis. It generally works well, but for 1 image now "regionprops" is returning an area of zero.
Before "regionprops" I threshold the image and then use "bwlabel".
rp =
struct with fields:
Area: 0
Centroid: [NaN NaN]
BoundingBox: [0.5000 0.5000 0 0]
Do you understand this? How can a region have area 0 (=no pixel)?
Not sure if this is related, but for memory issues my labelled image is uint16 and not double.

Best Answer

This expected behavior for "regionprops". Please see the explanation below for more information.
You can reproduce this behavior by using a labelmatrix. Any image that is not binary or a bwconncomp structure is considered as Labelmatrix by "regionprops". When you use an uint8 (for example) image as input, each integer value is considered as marking a different region. If some integer values are missing, there’s still a field created for that region but "regionprops" returns this structure with Area 0 & Centroid NaN.
% The test image below image has a max of 5 labels.
% Regionprops expects to find 5 regions labelled 1 to 5.
% Since label ‘2’ is missing in the image, regionprops will treat that as an empty region.
>>A = [ 1 0 0 0 0 3 0 4 0 5;
1 0 0 0 0 3 0 4 0 5;
1 0 0 0 0 3 0 4 0 5;
1 0 0 0 0 3 0 4 0 5;
1 0 0 0 0 3 0 4 0 5];
>>F = regionprops(A)
F =
5×1 struct array with fields:
Area
Centroid
BoundingBox
% Querying the properties of the empty region labeled as ‘2’
>> F(2)
ans =
struct with fields:
Area: 0
Centroid: [NaN NaN]
BoundingBox: [0.5000 0.5000 0 0]