Opencv Typeerror: Expected Cv::Umat for Argument 'Src' - What Is This

Expected cv::UMat for argument 'src'

When I just run the code, it also complains Expected cv::UMat for argument 'src'. Because there is no lane.jpg in my PC, so cv2.imread return NoneType, and np.copy return np.array(None, dtype=object). If you just pass such a variable to cv2, it will complain Expected cv::UMat for argument 'xxx'.

Sample Image

Yes, you should check your image exists and loaded successfully!


And notice, another bad practice is: your variable name canny is the same with your function name canny(). So when you call canny = canny(img), the function canny() object is replaced by variable canny. If you call canny() next time, it will fail like this:TypeError: 'numpy.ndarray' object is not callable.
Sample Image

Then use different names.



>>> src = cv2.imread("noexist.png")
>>> img = np.copy(src)
>>>
>>> type(src)
<class 'NoneType'>
>>> cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.0.1) d:\build\opencv\opencv-4.0.1\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

>>>
>>> img
array(None, dtype=object)
>>> cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Expected cv::UMat for argument 'src'
>>>
>>> img()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.ndarray' object is not callable

TypeError: Expected Ptr<cv::UMat> for argument 'm'

You need to use ImageTk.getimage(img) before converting to NumPy array.

You also need to convert the color format from RGBA to BGR before saving as JPEG.

Code sample:

img = ImageTk.PhotoImage(img)  # Type of img is PIL.ImageTk.PhotoImage

# https://stackoverflow.com/questions/58389742/how-to-convert-imagetk-to-image
img = np.array(ImageTk.getimage(img)) # Get the image, and then convert it to NumPy array

img_bgr = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR) # Convent from RGBA to BGR color space

cv2.imwrite("img.jpg", img_bgr) # Save to JPEG.

A more efficient solution is not using OpenCV, and not using ImageTk.PhotoImage.

You can simply use pillow for saving the image using img.save:

img.thumbnail((350,350))
img.save("img.jpg")

passing PIL image to OpenCV causes TypeError: Expected cv::UMat for argument 'src'

PIL and OpenCV both have different formats of image objects but they can be interchangeable, e.g. OpenCV accepts ndarray and you can change PIL image into array using NumPy. Similarly, you can get PIL image object from numpy array using PIL.Image.fromarray() method. I've modified your code and I think it will resolve your issue.

from PIL import Image
import cv2 as cv
import numpy as np

# Method to process the green band of the image
def normalizeGreen(intensity):
minI, maxI = 90, 225
minO, maxO = 0, 255
iO = (intensity-minI)*(((maxO-minO)/(maxI-minI))+minO)
return iO

# Create an image object
# imageObject = Image.open("<path-to-image>")
imageObject = Image.open("images/13/img-r1-000.jpg")

# Split the red, green and blue bands from the Image
r, g, b = imageObject.split()

# Apply point operations that does contrast stretching on each color band
normalizedGreenBand = g.point(normalizeGreen)

# convert the Pil Image object to OpenCv image object
normalizedGreenBand = np.array(normalizedGreenBand)

# create a CLAHE object (Arguments are optional)
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
normalizedGreenBand = clahe.apply(normalizedGreenBand)

# Display the image before contrast stretching
imageObject.show()

# convert the OpenCv image object to Pil Image object
normalizedGreenBand = Image.fromarray(normalizedGreenBand)
# Display the image after contrast stretching
normalizedGreenBand.show()

I first applied the point() method on the green channel and then converted the normalized green channel into a NumPy array in order to apply the cv.createCLAHE() operation. Finally, I reconverted the NumPy array into PIL image object for displaying (we can use cv2.imshow() without converting to PIL object).



Related Topics



Leave a reply



Submit