OpenCV和Python:连接组件分析 [英] OpenCV and Python: Connected components analysis

查看:256
本文介绍了OpenCV和Python:连接组件分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a working connected components analysis code working in C. It's actually a copy from the book "Learning Opencv".

现在我重写所有的代码Python和我不能在Python API中找到一些函数,例如cvStartFindContours。

Now I am rewriting all that code to Python and I cannot find some of that function in the Python API, like cvStartFindContours.

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

I am wondering is somebody has a basic connected components analysis function implemented in Python. I know there are some libraries, but I am searching for something simpler, just a function or a piece of code.

我不需要任何大,因为我有两个或三个白色圆圈的纯黑色图像,我想要找到圆圈数及其中心。

I don't need anything "big" because I have a plain black image with 2 or 3 white circles, and I want to find the number of circles and its center.

我知道我可以自己编码,但我喜欢使用某人的函数或简单的库。

I know I can probably code on myself but I prefer to use somebody's function or simple library.

编辑:我解决了以下方式。

I solved it the following way.

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


推荐答案

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

There is BSD license connected components code (in Cython) as part of scikit-image:

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

如果您安装了软件包, / p>

If you have the package installed, it is as simple as

from skimage import measure
import numpy as np

L = measure.label(image)
print "Number of components:", np.max(L)

这篇关于OpenCV和Python:连接组件分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆