I have a big problem when using the OpenCV 2 Python API. There are no more separate OpenCV Matrix types. Every matrix is actually a numpy matrix. So far so good. The problem arises when calling OpenCV functions on these matrices that require a specific data type. OpenCV seems to have problems reconciling numpy data types with OpenCV datatypes. For instance, numpy matrices with np.uint8
don't seem to be recognized as cv_8uc1
.
Here's a concrete example where it goes wrong, when trying to do a distance transform on a thresholded 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 produces 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
is np.uint8
so I don't know why this error appears. Does OpenCV 2 not map from the numpy datatypes to the corresponding OpenCV data types?
I would be very glad to find a solution to this problem.
Could not reproduce...
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 could double-check used datatypes.
python 2.7.3
numpy 1.6.1
cv2 2.4.10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With