问题
I am trying to apply a bounding box for the discontiguous region in the sample below. I found something in Matlab help docs on regionprops
but it did not explain anything about how to do it. I need the smallest Box that can contain all the blobs in the image.
回答1:
By default, when introduced with a logical
type input mask, regionprops automatically apply bwlabel to the mask and computes properties for each connected component of the input mask.
In your case, this is not a desirable behavior, since you want all the white pixels to be treated as part of the same component. To overcome this default behavior you simply need to cast the input mask from logical
to other data type.
st = regionprops( uint8( BW ), 'BoundingBox' ); %// cast to uint8
rect = st.BoundingBox; %// the bounding box of all white pixels
%// display the results
figure;
imshow( BW, 'border', 'tight' );
hold on;
rectangle('Position', rect, 'EdgeColor', 'r', 'LineWidth', 1.5 );
Resulting with
来源:https://stackoverflow.com/questions/27342380/how-can-i-apply-bounding-box-for-a-discontiguous-region-in-matlab