Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 365
Cricketer Avatar asked Jul 10 '13 07:07

Cricketer


People also ask

What is connected components OpenCV?

Connected component labeling (also known as connected component analysis, blob extraction, or region labeling) is an algorithmic application of graph theory used to determine the connectivity of “blob”-like regions in a binary image.

What is connected component image processing?

Brief Description. Connected components labeling scans an image and groups its pixels into components based on pixel connectivity, i.e. all pixels in a connected component share similar pixel intensity values and are in some way connected with each other.

How are connected components implemented in Python?

Applying Connected Component Labeling in Python: Connected Component Labeling can be applied using the cv2. connectedComponents() function in OpenCV. The function is defined so as to show the original image and the image after Connected Component Labeling. The input the the function is the path to the original image.


2 Answers

Starting from version 3.0, OpenCV has connectedComponents function.

like image 112
Alessandro Jacopson Avatar answered Oct 01 '22 17:10

Alessandro Jacopson


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

like image 31
Francesco Callari Avatar answered Oct 01 '22 19:10

Francesco Callari