简体   繁体   English

Python中如何使用OpenCV的connectedComponentsWithStats?

[英]How to use OpenCV's connectedComponentsWithStats 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.我正在寻找如何在 Python 中使用 OpenCV 的connectedComponentsWithStats() function 的示例。请注意,这仅适用于 OpenCV 3 或更新版本。 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.官方文档只显示 API 为 C++,即使 function 在为 Python 编译时存在。我在网上找不到它。

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: 它可以与OpenCV文档一起使用:

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. 统计数据通过统计数据[label,COLUMN]访问其中可用列定义如下。

  • cv2.CC_STAT_LEFT The leftmost (x) coordinate which is the inclusive start of the bounding box in the horizontal direction. cv2.CC_STAT_LEFT最左边的(x)坐标,它是水平方向上包含框的包含性开始。
  • cv2.CC_STAT_TOP The topmost (y) coordinate which is the inclusive start of the bounding box in the vertical direction. cv2.CC_STAT_TOP最顶层(y)坐标,它是垂直方向上边界框的包含性开始。
  • cv2.CC_STAT_WIDTH The horizontal size of the bounding box cv2.CC_STAT_WIDTH边界框的水平大小
  • cv2.CC_STAT_HEIGHT The vertical size of the bounding box cv2.CC_STAT_HEIGHT边界框的垂直大小
  • cv2.CC_STAT_AREA The total area (in pixels) of the connected component cv2.CC_STAT_AREA连接组件的总面积(以像素为单位)

Centroids is a matrix with the x and y locations of each centroid. 质心是一个矩阵,每个质心的x和y位置。 The row in this matrix corresponds to the label number. 该矩阵中的行对应于标签号。

Adding to Zack Knopp answer, If you are using a grayscale image you can simply use: 添加到Zack Knopp答案,如果您使用灰度图像,您可以简单地使用:

import cv2
import numpy as np

src = cv2.imread("path\\to\\image.png", 0)
binary_map = (src > 0).astype(np.uint8)
connectivity = 4 # or whatever you prefer

output = cv2.connectedComponentsWithStats(binary_map, connectivity, cv2.CV_32S)

When I tried using Zack Knopp answer on a grayscale image it didn't work and this was my solution. 当我尝试在灰度图像上使用Zack Knopp答案时,它不起作用,这是我的解决方案。

I have come here a few times to remember how it works and each time I have to reduce the above code to : 我来过这里几次以记住它是如何工作的,每次我必须将上面的代码减少到:

_, thresh = cv2.threshold(src,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
connectivity = 4  # You need to choose 4 or 8 for connectivity type
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh , connectivity , cv2.CV_32S)

Hopefully, it's useful for everyone :) 希望它对每个人都有用:)

the input image needs to be single channel.输入图像需要是单通道的。 so first convert to grayscale, otherwise it causes error in opencv 4.x you need to convert to grayscale and then the Zack's answer.所以首先转换为灰度,否则会导致 opencv 4.x 中的错误,您需要转换为灰度,然后是 Zack 的答案。

src = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM