我在使用 OpenCV 2 Python API 时遇到了一个大问题。不再有单独的 OpenCV 矩阵类型。每个矩阵实际上都是一个 numpy 矩阵。到目前为止,一切都很好。在这些需要特定数据类型的矩阵上调用 OpenCV 函数时会出现问题。 OpenCV 似乎在协调 numpy 数据类型与 OpenCV 数据类型方面存在问题。例如,带有 np.uint8
的 numpy 矩阵似乎不会被识别为 cv_8uc1
。
这是一个具体的例子,它在尝试对阈值图像进行距离变换时出错:
# threshold operation
val, thr = cv2.threshold(img, 64, 255, cv2.THRESH_BINARY )
# storage matrix for the distance map
map = np.zeros((rows,cols,1), np.uint8)
# attempt to apply distance transform
out = cv2.distanceTransform(thr, cv2.DIST_LABEL_CCOMP, 3, map)
这会产生以下错误:
OpenCV Error: Unsupported format or combination of formats (source
imagemust be 8uC1 and the distance map must be 32fC1 (or 8uC1 in
case of simple L1 distance transform)) in cvDistTransform
....
2.4.8/modules/imgproc/src/distransform.cpp:723: error: (-210)
source image must be 8uC1 and the distance map must be 32fC1
(or 8uC1 in case of simple L1 distance transform) in function
cvDistTransform
thr.dtype
是np.uint8
所以我不知道为什么会出现这个错误。 OpenCV 2 不会从 numpy 数据类型映射到相应的 OpenCV 数据类型吗?
我会很高兴找到这个问题的解决方案。
最佳答案
无法重现...
import cv2
import numpy as np
thr = np.random.rand(100,100).astype(np.uint8)
map = np.zeros((100,100,1), np.uint8)
out = cv2.distanceTransform(thr, cv2.DIST_LABEL_CCOMP, 3, map)
# no errors
您可以仔细检查使用的数据类型。
python 2.7.3
numpy 1.6.1
cv2 2.4.10
关于python - 调用 OpenCV 函数时冲突的 Numpy 和 OpenCV2 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20911423/