Search code examples
matlabimage-processingcomputer-visionimage-segmentationmathematical-morphology

How to find Center of Mass for my entire binary image?


I'm interested in finding the coordinates (X,Y) for my whole, entire binary image, and not the CoM for each component seperatly. How can I make it efficiently? I guess using regionprops, but couldn't find the correct way to do so.


Solution

  • You can define all regions as a single region for regionprops

    props = regionprops( double( BW ), 'Centroid' ); 
    

    According to the data type of BW regionprops decides whether it should label each connected component as a different region or treat all non-zeros as a single region with several components.


    Alternatively, you can compute the centroid by yourself

    [y x] = find( BW );
    cent = [mean(x) mean(y)];