Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "TypeError: Expected Ptr<cv::UMat> for argument '%s'"

I'm writing codes to accelerate my program using CUDA, but I got a tricky error. I have no idea about it.My environment is OpenCV 4.1.1, python 3.6. Here is my code.

I define a function to rotate the img,

def rotate(img, angle):
    '''
    '''    
    if len(img.shape) == 3:
        (rows, cols, channels) = img.shape
        out_size = (cols, rows, channels)
    else:
        (rows, cols) = img.shape
        out_size = (cols, rows)

    if angle == 0:
        dst = img

    else:
        # img_gpu = cv2.cuda_GpuMat()
        img_gpu = cv2.cuda_GpuMat()
        out_gpu = cv2.cuda_GpuMat()
#         M_gpu = cv2.cuda_GpuMat()
#         out_size_gpu = cv2.cuda_GpuMat()
#         border_value_gpu = cv2.cuda_GpuMat()

        m = cv2.getRotationMatrix2D((cols/2, rows/2), angle, 1) 
        img_gpu.upload(img)
#         M_gpu.upload(M)
#         out_size_gpu.upload((12000, 6000))
#         border_value_gpu.upload((0, 0, 0))
        cols_gpu = cv2.cuda_GpuMat()
        rows_gpu = cv2.cuda_GpuMat()
        cols_gpu.upload(cols)
        rows_gpu.upload(rows)

        print(type(img))
        print(img.shape)
        (row,col) = img.shape
        print([img_gpu.size()[0],img_gpu.size()[1]])
#         M = np.float32([[1,0,100],[0,1,50]])
#         out=cv2.UMat(out_gpu,(284,284))
        out_gpu = cv2.cuda.warpAffine(img_gpu, m, (col,row))

        dst = out_gpu.download()

    return dst

then I call it.

img = cv2.imread('../FengZhan/temp.png',0)
img_rotate = rotate(img, -10)

it cannot work and has the following error:

<ipython-input-48-41cd06952793> in rotate(img, angle)
     36 #         M = np.float32([[1,0,100],[0,1,50]])
     37 #         out=cv2.UMat(out_gpu,(284,284))
---> 38         out_gpu = cv2.cuda.warpAffine(img_gpu, m, (col,row))
     39 
     40         dst = out_gpu.download()

TypeError: Expected Ptr<cv::UMat> for argument '%s'

I tried to replace img_gpu with cv2.UMat(img_gpu), but it still cannot work. Anybody help me?

like image 740
crazycao Avatar asked Aug 06 '19 03:08

crazycao


3 Answers

I got this error: TypeError: Expected Ptr<cv::UMat> for argument '%s'

code:

image = cv2.imread("image_path")
image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)

Simply because the image path was incorrect and I was loading an image which wasn't there. I realize it might not be your case however I hope it may help others who encounter this error.

like image 189
Melron Avatar answered Oct 25 '22 11:10

Melron


I don't understand why do you define in the beginning out_gpu = cv2.cuda_GpuMat(). And in the end you set it again to: out_gpu = cv2.cuda.warpAffine(img_gpu, m, (col,row)).

The reason why you get this error may be because of your code out_gpu = cv2.cuda.warpAffine(img_gpu, m, (col,row)) argument img_gpu is supposed to be a string link to an image, yet it is defined previously as img_gpu = cv2.cuda_GpuMat(). Try to correct this by replacing the line img_gpu.upload(img) by uploaded_img = img_gpu.upload(img)

like image 39
Kornee Avatar answered Oct 25 '22 11:10

Kornee


Change your image to be numpy array

np.array(image)
like image 23
Tamer Awad Avatar answered Oct 25 '22 09:10

Tamer Awad