python - opencv(Python)中的connectedComponents不关心连接性?

标签 python python-2.7 opencv image-processing opencv3.3

我对 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/

相关文章:

python - python 3.6中的attrgetter函数

python - 排列numpy中每列内容的最佳方法

python-3.x - 如何在Python中将值为0-20的2D数组存储到灰度图像

c++ - 使用 openGL 在 C++ 中进行数据聚类

python脚本获取60天未登录的IAM用户列表

python - 不是 pcap 捕获文件(坏魔法)-scapy python

python - 无法在 python 2.7.x 控制台中输入 (č ć š ž đ )

python-2.7 - 使用 python 客户端将 csv 从 blobstore 上传到 google 驱动器

ruby - Windows Liquid 异常 : Failed to get header 上的 Jekyll

opencv - 使用来自单应性结果的 cv2.solvePnP 来获取相机姿势