- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在寻找如何在 Python 中使用 OpenCV 的 connectedComponentsWithStats()
函数的示例。请注意,这仅适用于 OpenCV 3 或更高版本。官方文档仅显示了 C++ 的 API,即使该函数在为 Python 编译时存在。我在网上的任何地方都找不到。
最佳答案
函数的工作原理如下:
# 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]
标签是输入图像大小的矩阵,其中每个元素的值等于其标签。
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_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
质心 是一个矩阵,其中包含每个质心的 x 和 y 位置。此矩阵中的行对应于标签编号。
关于python - 如何在 Python 中使用 OpenCV 的 connectedComponentsWithStats?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35854197/
我正在尝试使用 Java 版本的 OpenCV(Java 8 和 OpenCV 3.2.0.1)查找连接组件的质心。但是我找不到关于如何利用返回变量的详细文档。我发现这篇文章解释了数据的结构,但它是针
第一次发帖! 我刚刚安装了 python-opencv。根据 python,我的版本是: >>> import cv2 >>> cv2.__version__ '2.4.8' 我的 Ubuntu 版本
您是否必须通过设计cv2.connectedComponentsWithStats黑底白字图像与白底黑字图像相反?做一个和另一个我得到不同的结果。 示例代码: import os import cv2
我正在尝试从二值图像中获取一个或多个 Blob 的质心(最好仅当 Blob 的面积超过阈值时)。 findContours 解决方案有效,但并不完美,FeatureDetetector.SIMPLEB
我正在寻找如何在 Python 中使用 OpenCV 的 connectedComponentsWithStats() 函数的示例。请注意,这仅适用于 OpenCV 3 或更高版本。官方文档仅显示了
使用这个简单的独立演示: #include #include int main(int argc, char *argv[]) { // unsigned char data[] = {1,
我是一名优秀的程序员,十分优秀!