MATLAB: Regionprops orientation behaves differently with logical and integers

orientationregionprops

It seems that regionprops 'orientation' behaves differentlty if we give a logical or an integer :
A = [ 0 0 0 1 1 0 ;
0 0 0 1 0 0 ;
1 1 0 0 0 0 ;
0 0 0 0 0 0 ;
0 0 0 0 1 0 ;
0 0 0 0 0 0 ]
a_orientation = regionprops(A,'Orientation')
% Orientation: 28.1550
B = logical(A)
b_orientation = regionprops(B,'Orientation')
% 3x1 struct array with fields:
% Orientation
b_orientation.Orientation
% ans =


% 0

%

% ans =
% 45.0000
%
% ans =
% 0
%
How could I make regionprops behave with logical the same way as with integers without changing the type ?

Best Answer

When you pass A (a double precision matrix) into regionprops, it is treated as a label matrix with only one labeled region corresponding to the ones. It essentially assumes the data you are giving to it is the output from bwlabel. The metrics are computed across all the pixels labeled 1.
When you pass B (a logical matrix), it is treated as a binary image, in which there are three distinct regions of ones (i.e. 8-connected components) found. Metrics are returned for each region.
I don't believe there is any way to get regionprops to treat a logical input differently without first converting its type. Logical inputs appear to always undergo a connected-component processing step that identifies each "island" of ones as separate regions to analyze.