Conflicting Numpy and OpenCV2 Types When Calling OpenCV Functions

I have a big problem when using the OpenCV 2 Python API. There are no more separate types of OpenCV matrices. Each matrix is ​​actually a numpy matrix. So far, so good. The problem arises when calling OpenCV functions on these matrices, which require a certain data type. OpenCV seems to have problems matching numpy data types with OpenCV data types. For example, numpy matrices with np.uint8 do not seem to be recognized as cv_8uc1 .

Here is a concrete example when it goes wrong, when it tries to do a remote conversion on a threshold image:

  # 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) 

This results in the following error:

  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 , so I don't know why this error appears. Does OpenCV 2 map from numpy types to corresponding OpenCV data types?

I would be very happy to find a solution to this problem.

+8
python numpy opencv
source share
1 answer

Failed to play ...

 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 

You can double check the data types used.

 python 2.7.3 numpy 1.6.1 cv2 2.4.10 
+1
source share

All Articles