connectedComponents
(或connectedComponentsWithStats
)是Python (2.7.12)中的opencv (3.3.0)函数,我有一个问题。一个简单的代码如下:
import numpy as np
import cv2
img = np.zeros((4,4), dtype = np.uint8)
img[1,1] = 255
img[2,2] = 255
output = cv2.connectedComponents(img, 4)
print output[1]
它回来了
[[0 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 0]]
这很奇怪,因为我要求连接4(而不是8)的连接组件。因此,(1, 1)
和(2, 2)
中的两个像素没有连接,应该给出两个不同的连接分量,例如标记为1和2。
我搞错了吗?
发布于 2018-03-29 15:48:43
替换
output = cv2.connectedComponents(img, 4)
通过
output = cv2.connectedComponents(img, connectivity=4)
会给你
[[0 0 0 0]
[0 1 0 0]
[0 0 2 0]
[0 0 0 0]]
另外,提供所有3个参数
output = cv2.connectedComponents(img, 4, cv2.CV_32S)
我不是百分之百的原因。我会把这个留给Python专家。根据我的理解,cv2.connectedComponents(img, 4)
应该工作得很好。但事实并非如此
https://stackoverflow.com/questions/49553646
复制相似问题