更新时间:2021-07-28 01:02:28
该函数的工作方式如下:
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.
统计数据是该函数计算的统计数据的矩阵.它的长度等于标签数,宽度等于统计数.它可以与OpenCV文档一起使用:
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:
每个标签(包括背景标签)的统计信息输出,请参阅 请参阅下面的可用统计信息.可以通过访问统计信息 stats [label,COLUMN] ,其中在下面定义了可用的列.
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.
质心是一个矩阵,其中每个质心的x和y位置.此矩阵中的行对应于标签号.
Centroids is a matrix with the x and y locations of each centroid. The row in this matrix corresponds to the label number.