OpenCV TypeError: Expected cv::UMat for argument [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
2 min

banner

# OpenCV TypeError: Expected cv::UMat for argument [Solved]

To solve the OpenCV "TypeError: Expected cv::UMat for argument", make sure your image is of the correct form.

The first thing you should try is to pass the cv2.UMat object directly to cv2.cvtColor().

main.py
import cv2 img = cv2.imread('thumbnail.webp') img_UMat = cv2.UMat(img) gray_UMat = cv2.cvtColor(img_UMat, cv2.COLOR_RGB2GRAY) print(gray_UMat)

pass umat object directly to cv2 cvt color

The first argument the cv2.cvtColor() method takes is the image src.

# Convert the image to a NumPy array

If that doesn't work, try to convert the image to a NumPy array by passing it to numpy.array().

First, make sure you have the numpy module installed.

shell
pip install numpy # or with pip3 pip3 install numpy

Now, import the numpy module and convert the image to a NumPy array.

main.py
import cv2 import numpy as np img = cv2.imread('thumbnail.webp') # ✅ Convert image to NumPy array img = np.array(img) img_UMat = cv2.UMat(img) gray_UMat = cv2.cvtColor(img_UMat, cv2.COLOR_RGB2GRAY) print(gray_UMat)

convert image to numpy array

I only added this line to the code sample.

main.py
# ✅ Convert an image to a NumPy array img = np.array(img)

If that doesn't work, try to use the numpy.ascontiguousarray() method instead.

main.py
import cv2 import numpy as np img = cv2.imread('thumbnail.webp') # ✅ using numpy.ascontiguousarray() img = np.ascontiguousarray(img) img_UMat = cv2.UMat(img) gray_UMat = cv2.cvtColor( img_UMat, cv2.COLOR_RGB2GRAY ) print(gray_UMat)

using numpy ascontiguousarray method instead

# Try to create a copy of the image

If the error persists, try to reassign the image to a copy of itself by calling img.copy().

main.py
import cv2 img = cv2.imread('thumbnail.webp') # ✅ Create a copy of the image img = img.copy() img_UMat = cv2.UMat(img) gray_UMat = cv2.cvtColor(img_UMat, cv2.COLOR_RGB2GRAY) print(gray_UMat)

create copy of the image

Notice that we reassigned the value of the img variable to the result of calling img.copy().

main.py
# ✅ Create a copy of the image img = img.copy()

# Try to pass the cv2.UMat object to the cv2.UMat class

If the error persists, try to pass the cv2.UMat object to the cv2.UMat class when calling cv2.cvtColor().

main.py
import cv2 img = cv2.imread('thumbnail.webp') img_UMat = cv2.UMat(img) gray_UMat = cv2.cvtColor( cv2.UMat(img_UMat), cv2.COLOR_RGB2GRAY ) print(gray_UMat)

pass cv2 umat object to cv2 umat class

The only line I changed is the following.

main.py
gray_UMat = cv2.cvtColor( cv2.UMat(img_UMat), cv2.COLOR_RGB2GRAY )

I passed the already-created cv2.UMat object to the cv2.UMat class when calling cv2.cvtColor().

If you get one of the following OpenCV errors, click on the link and follow the instructions:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.