OpenCV TypeError: Expected cv::UMat for argument 'src' - What is this?
PythonOpencvTypeerrorSrcPython Problem Overview
Disclaimer: huge openCV noob
> Traceback (most recent call last):
>
> File "lanes2.py", line 22, in
What exactly is 'src' referring to?
Python Solutions
Solution 1 - Python
src
is the first argument to cv2.cvtColor
.
The error you are getting is because it is not the right form. cv2.Umat()
is functionally equivalent to np.float32()
, so your last line of code should read:
gray = cv2.cvtColor(np.float32(imgUMat), cv2.COLOR_RGB2GRAY)
Solution 2 - Python
gray = cv2.cvtColor(cv2.UMat(imgUMat), cv2.COLOR_RGB2GRAY)
UMat is a part of the Transparent API (TAPI) than help to write one code for the CPU and OpenCL implementations.
Solution 3 - Python
The following can be used from numpy
:
import numpy as np
image = np.array(image)
Solution 4 - Python
Not your code is the problem this is perfectly fine:
gray = cv2.cvtColor(imgUMat, cv2.COLOR_RGB2GRAY)
The problem is that imgUMat is None
so you probably made a mistake when loading your image:
imgUMat = cv2.imread("your_image.jpg")
I suspect you just entered the wrong image path.
Solution 5 - Python
Just add this at start:
image = cv2.imread(image)
Solution 6 - Python
Is canny
your own function? Do you use Canny from OpenCV inside it? If yes check if you feed suitable argument for Canny
- first Canny
argument should meet following criteria:
- type:
<type 'numpy.ndarray'>
- dtype:
dtype('uint8')
- being single channel or simplyfing: grayscale, that is 2D array, i.e. its
shape
should be 2-tuple
ofint
s (tuple
containing exactly 2 integers)
You can check it by printing respectively
type(variable_name)
variable_name.dtype
variable_name.shape
Replace variable_name
with name of variable you feed as first argument to Canny
.
Solution 7 - Python
This is a general error, which throws sometimes, when you have mismatch between the types of the data you use. E.g I tried to resize the image with opencv, it gave the same error. Here is a discussion about it.
Solution 8 - Python
Some dtype are not supported by specific OpenCV functions. For example inputs of dtype np.uint32 create this error. Try to convert the input to a supported dtype (e.g. np.int32 or np.float32)
Solution 9 - Python
Convert your image matrix to ascontiguousarray using np.ascontiguousarray
as bellow:
gray = cv2.cvtColor(np.ascontiguousarray(imgUMat), cv2.COLOR_RGB2GRAY)
Solution 10 - Python
that is referring to the expected dtype of your image
"image".astype('float32') should solve your issue
Solution 11 - Python
Sometimes I have this error when videostream from imutils package doesn't recognize frame or give an empty frame. In that case, solution will be figuring out why you have such a bad frame or use a standard VideoCapture(0) method from opencv2
Solution 12 - Python
I got round thid by writing/reading to a file. I guessed cv.imread would put it into the format it needed. This code for anki Vector SDK program but you get the idea.
tmpImage = robot.camera.latest_image.raw_image.save('temp.png')
pilImage = cv.imread('temp.png')
Solution 13 - Python
If using ImageGrab
Verify that your image is not a 0x0 area due to an incorrect bbox
.
Solution 14 - Python
If you are using byte object instead of reading from file you can convert your image to numpy array like this
image = numpy.array(Image.open(io.BytesIO(image_bytes)))
Solution 15 - Python
Verify the application root folder is the same as the file you are attempting to run.