OpenCV how to find a list of connected components in a binary image

40,211

Solution 1

You need to cv::floodFill the contours returned by cv::findCountours. See this example for findContours, and this one for floodFill

Solution 2

Starting from version 3.0, OpenCV has connectedComponents function.

Share:
40,211
Cricketer
Author by

Cricketer

Updated on December 10, 2020

Comments

  • Cricketer
    Cricketer over 3 years

    I am using OpenCV for a C++ application. I have a 8 bit binary image that has some objects. The objects are all colored 255, whereas everything in the background is colored 0. Each object has no vacant (black) pixels inside it. In other words, each object is fully white. The objects are NOT connected to each other. Here's what I want to extract from this:

    I want to extract some kind of list of objects, from which I have some notion of the location of each object in that list. This could be using cvConnectedComponents() or anything else. I need some indication of where each object is located in the image. This could be in the form of bounding rectangle for each object or median or center based on some computation or anything that gives me a measure of the objects location in the image. Any pointers to what OpenCV functions to look into?

  • Jason Newton
    Jason Newton almost 9 years
    I think the answer with this should be changed now that 3.0 is [finally] is out.
  • Gulzar
    Gulzar almost 4 years
    @JasonNewton changed to what