Discussions>TypeError: Expected cv::UMat for argument 'mat'>

TypeError: Expected cv::UMat for argument 'mat'

Why I'm getting this error, when I want to just screen shot by python script?

import cv2
from PIL import ImageGrab 

img = ImageGrab.grab(bbox=(0, 0, 512, 512))
cv2.imshow('image', img)
TypeError: Expected cv::UMat for argument 'mat'
3 votesJW270.00
2 Answers
UG3.00
0

 I am trying to run this code. I took the blurred image and apllied sobels kernel on it. kernel applied successfully like i am getting the vertical and horizontal components on the output but when i try to show the gradient it is giving an error. I have tried many ways but i am not getting a solution.   

    import numpy as np

    import cv2 as cv

    from scipy import ndimage

    img=cv.imread("scarlet_guassian_blur.jpg",cv.IMREAD_UNCHANGED)

    Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32)

    Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32)

    

    Ix = ndimage.filters.convolve(img, Kx)

    Iy = ndimage.filters.convolve(img, Ky)

    

    print("SobelX_Vertical\n",Ix)

    print("SobelY_Horizontal\n",Iy)

    

    cv.imshow("Gray_Scale",img)

    cv.imshow("SobelX_Vertical",Ix)

    cv.imshow("SobelY_Horizontal",Iy)

    

    hyp=(Ix*Ix) + (Iy*Iy)

    G=np.sqrt([hyp])

    

    gradient = G / G.max() * 255

    print("Gradient\n",gradient)

    phase = np.arctan2(Iy, Ix)

    print("Phase\n",phase)

    cv.imshow("Gradient",gradient)

    cv.waitKey()

    cv.destroyAllWindows()

It is giving the following output in my terminal:

SobelX_Vertical
 [[  0   0   0 ...   0   0   0]
 [  0   0   0 ...   0   0   0]
 [  0   0   0 ...   0   0   0]
 ...
 [  2   2 254 ... 254 255   0]
 [  3   4 254 ... 250 253   0]
 [  1   4 255 ... 248 252   0]]
SobelY_Horizontal
 [[ 0  0  0 ...  0  0  0]
 [ 0  0  0 ...  0  0  0]
 [ 0  0  0 ...  0  0  0]
 ...
 [ 6  2  0 ... 12 15 16]
 [ 1  2  2 ...  8 11 12]
 [ 1  2  1 ...  4  4  4]]
Gradient
 [[[  0.     0.     0.   ...   0.     0.     0.  ]
  [  0.     0.     0.   ...   0.     0.     0.  ]
  [  0.     0.     0.   ...   0.     0.     0.  ]
  ...
  [102.    45.6   32.25 ... 196.1  242.4    0.  ]
  [ 51.    72.1   45.6  ... 161.2  183.8  193.5 ]
  [ 22.8   72.1   22.8  ... 144.2   91.2   64.5 ]]]
Phase
 [[0.      0.      0.      ... 0.      0.      0.     ]
 [0.      0.      0.      ... 0.      0.      0.     ]
 [0.      0.      0.      ... 0.      0.      0.     ]
 ...
 [1.249   0.785   0.      ... 0.0472  0.05875 1.57   ]
 [0.3218  0.4636  0.00787 ... 0.03198 0.04346 1.57   ]
 [0.785   0.4636  0.00392 ... 0.01613 0.01587 1.57   ]]
 
---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)
<ipython-input-3-84576290db15> in <module>()     23 phase = np.arctan2(Iy, Ix)     24 print("Phase\n",phase)---> 25 cv.imshow("Gradient",gradient)     26 cv.waitKey()     27 cv.destroyAllWindows()TypeError: Expected Ptr<cv::UMat> for argument 'mat'
Reply
JW270.00
0

Duplicate. Take a look at This Discussion.

JO242.00
3

You get the error, because OpenCV needs a numpy array to show as an image, but ImageGrab returns a Pillow object. Just add a convertion script before it.

import cv2
import numpy as np
from PIL import ImageGrab 

img = ImageGrab.grab(bbox=(0, 0, 512, 512))
img2 = np.array(img)
cv2.imshow('image', img2)
Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?