Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use openCV's connected components with stats in python?

I am looking for an example of how to use OpenCV's ConnectedComponentsWithStats() function in python, note this is only available with OpenCV 3 or newer. The official documentation only shows the API for C++, even though the function exists when compiled for python. I could not find it anywhere online.

like image 919
Zack Knopp Avatar asked Mar 07 '16 21:03

Zack Knopp


People also ask

What is stats in connectedComponentsWithStats?

Stats is a matrix of the stats that the function calculates. It has a length equal to the number of labels and a width equal to the number of stats. It can be used with the OpenCV documentation for it: Statistics output for each label, including the background label, see below for available statistics.

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.

What is cv2 connectedComponentsWithStats?

The most popular method is cv2. connectedComponentsWithStats which returns the following information: The bounding box of the connected component. The area (in pixels) of the component. The centroid/center (x, y)-coordinates of the component.

What are connected components in image processing?

Connected components, in a 2D image, are clusters of pixels with the same value, which are connected to each other through either 4-pixel, or 8-pixel connectivity.


1 Answers

The function works as follows:

# Import the cv2 library import cv2 # Read the image you want connected components of src = cv2.imread('/directorypath/image.bmp') # Threshold it so it becomes binary ret, thresh = cv2.threshold(src,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) # You need to choose 4 or 8 for connectivity type connectivity = 4   # Perform the operation output = cv2.connectedComponentsWithStats(thresh, connectivity, cv2.CV_32S) # Get the results # The first cell is the number of labels num_labels = output[0] # The second cell is the label matrix labels = output[1] # The third cell is the stat matrix stats = output[2] # The fourth cell is the centroid matrix centroids = output[3] 

Labels is a matrix the size of the input image where each element has a value equal to its label.

Stats is a matrix of the stats that the function calculates. It has a length equal to the number of labels and a width equal to the number of stats. It can be used with the OpenCV documentation for it:

Statistics output for each label, including the background label, see below for available statistics. Statistics are accessed via stats[label, COLUMN] where available columns are defined below.

  • cv2.CC_STAT_LEFT The leftmost (x) coordinate which is the inclusive start of the bounding box in the horizontal direction.
  • cv2.CC_STAT_TOP The topmost (y) coordinate which is the inclusive start of the bounding box in the vertical direction.
  • cv2.CC_STAT_WIDTH The horizontal size of the bounding box
  • cv2.CC_STAT_HEIGHT The vertical size of the bounding box
  • cv2.CC_STAT_AREA The total area (in pixels) of the connected component

Centroids is a matrix with the x and y locations of each centroid. The row in this matrix corresponds to the label number.

like image 125
Zack Knopp Avatar answered Sep 16 '22 11:09

Zack Knopp