调用OpenCV函数时冲突的Numpy和OpenCV2数据类型

Kof*_*man 8 python opencv numpy

使用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)
Run Code Online (Sandbox Code Playgroud)

这会产生以下错误:

    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
Run Code Online (Sandbox Code Playgroud)

thr.dtypenp.uint8这样我不知道为什么会出现这个错误.OpenCV 2不是从numpy数据类型映射到相应的OpenCV数据类型吗?

我很乐意找到解决这个问题的方法.

Nik*_*yrh 1

无法重现...

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
Run Code Online (Sandbox Code Playgroud)

您可以仔细检查使用的数据类型。

python 2.7.3
numpy 1.6.1
cv2 2.4.10
Run Code Online (Sandbox Code Playgroud)