Fixing OpenCV TypeError: Expected cv::UMat for argument ‘src’ in Python

OpenCV TypeError: Expected cv::UMat for argument ‘src’ in Python is an error which occurs when you load a faulty image or it is of a faulty format.

I will explain why this error takes place and how to fix it, while also trying to add other solutions that could solve the error.

Exploring the OpenCV TypeError: Expected cv::UMat for argument ‘src’ in Python

This is an error which occurs when you load a faulty image or it is of a faulty format.

Please double check so you can avoid mixing between errors. The error message should look like the error message bellow.

                                                                       #
TypeError: Expected cv::UMat for argument 'src'
                                                                       #

Bellow I will present multiple solutions some have worked for me and others have worked for other developers.

The best Step by Step method to solve the issue : use np.float32() or cv2.UMat(). Alternatively use np.array() or np.ascontiguousarray()

This is going to be a step by step troubleshooting method, we will try all the possible options we have until you solve this issue.

This is an example that will reproduce the error, solving the error in your case and in this one should be pretty similar.

                                                                       #
var = cv2.cvtColor(myimg, cv2.colorconversion)
                                                                       #

The cv2.cvtColor() method is used to convert an image from one color space to another

The first solution is to use cv2.UMat() like in the example bellow.

                                                                       #
var = cv2.cvtColor(cv2.UMat(myimg), cv2.colorconversion)
                                                                       #

Keep in mind that myimg is an image that I load using the line bellow

                                                                       #
myimg = cv2.imread("image.jpg")
                                                                       #

Alternatively you can try using np.float32() if using cv2.UMat() did not solve the issue.

                                                                       #
var = cv2.cvtColor(np.float32(myimg), cv2.colorconversion)
                                                                       #

If this does not work, you will probably get the following error message

                                                                       #
UMat() missing required argument 'ranges'
                                                                       #

In this case, it is clear that myimg is a None and not a valid numpy ndarray. This happens when you either make a mistake when naming the image in your code for example you load image1 which does not exist while the real image is image0.

Sometimes this happens when the name of the image is right but you have provided the wrong path to the image. The solution is to use the correct path.

If all of that did not work, you can try using np.array like in the example bellow. alternatively you can try using np.ascontiguousarray()

                                                                       #
import numpy as np 
myimg = np.array(myimg)
# alternatively you can try using np.ascontiguousarray()
np.ascontiguousarray(myimg)
                                                                       #

I hope you guys found my article helpful, I hope the solution provided above has solved your problem and the error OpenCV TypeError Expected cv UMat for argument src in Python is gone.

Summing-up : 

If you like the effort I put in this post please consider donating to our Kofi account using the red button at the top of this page. Keep learning guys, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/