适用于Python的OpenCV-AttributeError:“模块"对象没有属性"connectedComponents" [英] OpenCV for Python - AttributeError: 'module' object has no attribute 'connectedComponents'

查看:239
本文介绍了适用于Python的OpenCV-AttributeError:“模块"对象没有属性"connectedComponents"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下针对OpenCV的分水岭算法: https://opencv-python-tutroals.readthedocs.org/zh_CN/latest/py_tutorials/py_imgproc/py_watershed/py_watershed.html#watershed

I'm trying the watershed algorithm using the following tutorial for OpenCV: https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_watershed/py_watershed.html#watershed

我已经解决了一个错误,现在代码如下:

I already fixed an error, now the code looks like this:

import numpy as np
import cv2
from matplotlib import pyplot as plt
from sys import argv

img = cv2.imread(argv[1])
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# noise removal
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

# sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)

# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)

# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)

# Add one to all labels so that sure background is not 0, but 1
markers = markers+1

# Now, mark the region of unknown with zero
markers[unknown==255] = 0

markers = cv2.watershed(img,markers)
img[markers == -1] = [255,0,0]

cv2.imwrite("watershed_img.png",img)
cv2.imwrite("watershed_markers.png",markers)

当我尝试运行它时,出现以下错误(文件名为"watersh.py"):

When I try to run it, I receive the following error (the file name is "watersh.py"):

Traceback (most recent call last):
File "watersh.py", line 26, in <module>
ret, markers = cv2.connectedComponents(sure_fg)
AttributeError: 'module' object has no attribute 'connectedComponents'

我发现该函数存在于OpenCV的C ++库中:

I found that the function exists in the C++ library of OpenCV:

http://docs.opencv.org/trunk/modules/imgproc /doc/structural_analysis_and_shape_descriptors.html?highlight=connected

我的问题是,是否有另一个名称的实现,或者它在Python中根本不存在?如果没有,我该如何解决错误?

My question is, is there an implementation for it under another name or it doesn't exist at all in Python? If not, how can I solve the error?

我正在使用OpenCV 2.4.9

edit: I'm using OpenCV 2.4.9

推荐答案

对于任何对此进行搜索的人,答案是我有Sourceforge的OpenCV 2.9,但我需要他们在git上的回购中使用3.0版本才能正常使用此功能

For anyone searching for this, the answer is that I had OpenCV 2.9 from Sourceforge, but I needed the 3.0 version from their repo on git for this function to work.

这篇关于适用于Python的OpenCV-AttributeError:“模块"对象没有属性"connectedComponents"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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