Search code examples
opencvjavacvobject-detection

Get position and size of an object in a binary image


Does someone have an idea to get the size and the position from an object? The Object is detected in a binary image with white pixels:

For example: Detected / Original

http://ivrgwww.epfl.ch/supplementary_material/RK_CVPR09/Images/segmentation/2_sal/0_12_12171.jpg http://ivrgwww.epfl.ch/supplementary_material/RK_CVPR09/Images/comparison/orig/0_12_12171.jpg

I know about the CvMoments- Method. But I don't know how to use it in this case.

By the way: How can I make my mask more clearly?


Solution

  • Simple algorithm:

    1. Delete small areas of white pixels using morphological operations (erosion).
    2. Use findContours to find all contours.
    3. Use countNonZero or contourArea to find area of each contour.
    4. Cycle throught all points of each contour and find mean of them. This will be the center of contour.