OpenCV和Python:连接组件分析

Dr *_*ban 2 python opencv image-processing computer-vision

我有一个工作连接组件分析代码在C中工作.它实际上是"学习Opencv"一书的副本.

现在我将所有代码重写为Python,我在Python API中找不到某些功能,比如cvStartFindContours.

我想知道是否有人在Python中实现了基本的连通组件分析功能.我知道有一些库,但我正在寻找更简单的东西,只是一个函数或一段代码.

我不需要任何"大"的东西,因为我有一个带有2或3个白色圆圈的纯黑色图像,我想找到圆圈的数量及其中心.

我知道我可以自己编码,但我更喜欢使用某人的功能或简单的库.

编辑:我通过以下方式解决了它.

def find_connected_components(img):
    """Find the connected components in img being a binary image.
    it approximates by rectangles and returns its centers
    """

    storage = cv.CreateMemStorage(0)
    contour = cv.FindContours(img, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
    centers = []

    while contour:
        # Approximates rectangles
        bound_rect = cv.BoundingRect(list(contour))

        centers.append(bound_rect[0] + bound_rect[2] / 2, bound_rect[1] + bound_rect[3] / 2)

        contour = contour.h_next()
Run Code Online (Sandbox Code Playgroud)

Ste*_*alt 6

有BSD许可证连接组件代码(在Cython中)作为scikit-image的一部分:

https://github.com/scikit-image/scikit-image/blob/master/skimage/measure/_ccomp.pyx

如果您安装了软件包,那就很简单了

from skimage import measure
import numpy as np

L = measure.label(image)
print "Number of components:", np.max(L)
Run Code Online (Sandbox Code Playgroud)


Jas*_*ton 5

有点过时的回复,但也有这个补丁:http: //code.opencv.org/attachments/467/opencv-connectedcomponents.patch

应该是那里更快的实现之一,仍然很容易调用.应该在未来的某个时候融入主线......

编辑:它已经在主线上等待3.0发布了.不要问我为什么他们不早点发布它!

免责声明 - 我是作者:-)