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?
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.
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)
Change your image to be numpy array
np.array(image)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With