1

我对connectedComponents(or 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。

我犯错了吗?

4

1 回答 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)应该可以正常工作。但它没有

于 2018-03-29T15:48:43.833 回答