15

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?

Cricketer
  • 2,391
  • 4
  • 24
  • 29
  • 3
    Study this example. http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html – LovaBill Jul 10 '13 at 07:55
  • 2
    possible duplicate of [connected components in OpenCV](http://stackoverflow.com/questions/12688524/connected-components-in-opencv) – kiranpradeep May 18 '15 at 08:06

2 Answers2

33

Starting from version 3.0, OpenCV has connectedComponents function.

Antonio
  • 19,451
  • 13
  • 99
  • 197
Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153
11

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

Francesco Callari
  • 11,300
  • 2
  • 25
  • 40