gpt4 book ai didi

python - 用于 Python 的 OpenCV - AttributeError : 'module' object has no attribute 'connectedComponents'

转载 作者:太空狗 更新时间:2023-10-29 18:26:30 24 4
gpt4 key购买 nike

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

我已经修复了一个错误,现在代码如下所示:

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”):

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++库中:

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

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

编辑:我正在使用 OpenCV 2.4.9

最佳答案

对于搜索此内容的任何人,答案是我有来自 Sourceforge 的 OpenCV 2.9,但我需要他们在 git 上的 repo 中的 3.0 版本才能使用此功能。

关于python - 用于 Python 的 OpenCV - AttributeError : 'module' object has no attribute 'connectedComponents' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24699766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com