python - opencv connectedComponentsWithStats

标签 python opencv

第一次发帖!

我刚刚安装了 python-opencv。根据 python,我的版本是:

>>> import cv2
>>> cv2.__version__ '2.4.8'

我的 Ubuntu 版本是 14.04。

然后我开始了一个 python-opencv 教程,它建议了这段代码:

img = cv2.imread('OpenCV_Chessboard.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# find Harris corners
gray        = np.float32(gray)
dst         = cv2.cornerHarris(gray, 2, 3, 0.04)
dst         = cv2.dilate(dst,None)
ret, dst    = cv2.threshold(dst,0.01*dst.max(),255,0)
dst         = np.uint8(dst)

# find centroids
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)

# define the criteria to stop and refine the corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)

# Now draw them
res = np.hstack((centroids,corners))
res = np.int0(res)
img[res[:,1],res[:,0]]=[0,0,255]
img[res[:,3],res[:,2]] = [0,255,0]

cv2.imwrite('subpixel5.png',img)

当尝试执行未更改的代码时,我得到了这个:

File "pyopencv_test.py", line 21, in <module>
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
AttributeError: 'module' object has no attribute 'connectedComponentsWithStats'

快速搜索表明我需要 python-opencv 3 而不是我当前的版本 2.4.8。我不知道如何将opencv版本更新为opencv 3,以便python自动访问它。有什么帮助吗?非常感谢逐步说明!

最佳答案

'connectedComponentsWithStats' 方法只能在 opencv3.x 上使用。

关于python - opencv connectedComponentsWithStats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172977/

相关文章:

python - 查找 pandas 数据框中列的经度和纬度

Python:使用自定义比较器对字典数组进行排序?

opencv - 在 OpenCV 中使用 imread() 加载图像时如何捕获损坏的 JPEG?

OpenCV:设置每个 RGB 像素的亮度,使最亮的 channel (R、G 或 B)最大

python - 使用matplotlib生成平滑线图

python - write_pajek 的 networkx 问题

c++ - 如何将 OpenCV 3.2 图像保存为 csv 文件,然后加载显示?

c++ - opencv: namedWindow() 错误 "can' t 打开显示”!

python - 在opencv中使用houghlines仅打印一行

python - 将输出重定向到文件 python 时回车无法正常工作