如何在python中使用带有统计信息的openCV的连接组件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35854197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:04:11  来源:igfitidea点击:

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

pythonopencvconnected-components

提问by Zack Knopp

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.

我正在寻找如何在 python 中使用 OpenCV 的 ConnectedComponentsWithStats() 函数的示例,请注意这仅适用于 OpenCV 3 或更新版本。官方文档只显示了 C++ 的 API,即使该函数在为 python 编译时存在。我在网上的任何地方都找不到。

回答by Zack Knopp

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]

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

标签是输入图像大小的矩阵,其中每个元素的值等于其标签。

Statsis 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:

Stats是函数计算的统计数据的矩阵。它的长度等于标签的数量,宽度等于统计数据的数量。它可以与 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.

  • cv2.CC_STAT_LEFTThe leftmost (x) coordinate which is the inclusive start of the bounding box in the horizontal direction.
  • cv2.CC_STAT_TOPThe topmost (y) coordinate which is the inclusive start of the bounding box in the vertical direction.
  • cv2.CC_STAT_WIDTHThe horizontal size of the bounding box
  • cv2.CC_STAT_HEIGHTThe vertical size of the bounding box
  • cv2.CC_STAT_AREAThe total area (in pixels) of the connected component

每个标签的统计输出,包括背景标签,有关可用统计信息,请参见下文。统计信息通过stats[label, COLUMN]访问 其中可用的列定义如下。

  • cv2.CC_STAT_LEFT最左边的 (x) 坐标,它是水平方向边界框的包含起点。
  • cv2.CC_STAT_TOP最顶部 (y) 坐标,它是垂直方向边界框的包含起点。
  • cv2.CC_STAT_WIDTH边界框的水平尺寸
  • cv2.CC_STAT_HEIGHT边界框的垂直尺寸
  • cv2.CC_STAT_AREA连接组件的总面积(以像素为单位)

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

质心是一个矩阵,其中包含每个质心的 x 和 y 位置。该矩阵中的行对应于标签编号。

回答by Dan Erez

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 :)

希望对大家有用:)

回答by Barel Levy

Adding to Zack Knoppanswer, 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 Knoppanswer on a grayscale image it didn't work and this was my solution.

当我尝试Zack Knopp在灰度图像上使用答案时,它不起作用,这是我的解决方案。