我对 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。
我是不是搞错了?
最佳答案
更换
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)
我并不是100%的原因。我将把这个问题留给 Python 专家来解决。根据我的理解 cv2.connectedComponents(img, 4)
应该可以正常工作。但事实并非如此
关于python - opencv(Python)中的connectedComponents不关心连接性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49553646/