使用OpenCV 2 Python API时遇到了很大的问题.没有更多单独的OpenCV Matrix类型.每个矩阵实际上都是一个numpy矩阵.到现在为止还挺好.在需要特定数据类型的这些矩阵上调用OpenCV函数时会出现问题.OpenCV似乎在调整numpy数据类型与OpenCV数据类型时遇到了问题.例如,numpy矩阵np.uint8
似乎不被认为是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数据类型吗?
我很乐意找到解决这个问题的方法.