regionprops 0 Area NaN Centroid issue
9 views (last 30 days)
Show older comments
MathWorks Support Team
on 15 Jun 2018
Commented: Image Analyst
on 15 Jun 2019
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.
Accepted Answer
MathWorks Support Team
on 15 Jun 2018
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]
1 Comment
Image Analyst
on 15 Jun 2019
You can relabel the binary image if you removed something from the labeled image, like what they did
A = [...
1 0 2 0 0 3 0 4 0 5;
1 0 2 0 0 3 0 4 0 5;
1 0 2 0 0 3 0 4 0 5;
1 0 2 0 0 3 0 4 0 5;
1 0 2 0 0 3 0 4 0 5];
A(A==2) = 0 % Get rid of 2 from labeled image and make it zero.
% Now there is no 2 in the labeled image and you'll get nans for measurements from the missing #2.
props = regionprops(A)
% Show what we get for the missing 2:
props(2)
% Now the fix. Simply relabel the labeled image
binaryImage = A~= 0 % Create a new binary image.
newA = bwlabel(binaryImage) % Give new labels to that new binary image.
% newA =
% 1 0 0 0 0 2 0 3 0 4
% 1 0 0 0 0 2 0 3 0 4
% 1 0 0 0 0 2 0 3 0 4
% 1 0 0 0 0 2 0 3 0 4
% 1 0 0 0 0 2 0 3 0 4
% Get new props. props will have only 4 structures this time instead of 5.
props = regionprops(newA) % Use newA this time, not the original A
% Show what we get for the 4 blobs:
props(1)
props(2)
props(3)
props(4)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!