opencv / opencv-python

Automated CI toolchain to produce precompiled opencv-python, opencv-python-headless, opencv-contrib-python and opencv-contrib-python-headless packages.
https://pypi.org/project/opencv-python/
MIT License
4.47k stars 842 forks source link

How to convert cv2.Mat() to cv2.UMat() #1030

Open JV-X opened 2 weeks ago

JV-X commented 2 weeks ago

Hello, I am trying to write some glue code for a third-party library. In the code, I need to return a data of type cv2.UMat. Now I can use cv2.Mat(descs) to get a Mat, but I can't find a way to convert cv2.Mat to cv2.UMat. I tried umat = cv.UMat(mat), mat.copyTo(umat), umat = cv.UMat(descs.shape[0], descs.shape[1], descs.dtype, cv.USAGE_DEFAULT), but none of them worked. I found on the Internet that there seems to be such a method in C to convert Mat to UMat, but I can't seem to find it in Python. Can you give some suggestions?

wuyiulin commented 2 weeks ago

use

umat = cv2.UMat(mat)

it works for me in OpcnCV 3.4.18.

JV-X commented 2 weeks ago

use

umat = cv2.UMat(mat)

it works for me in OpcnCV 3.4.18.

For project reasons, it seems I can't use another version of opencv, but still thanks so much for your reply.

my environment is as follows:

The environment is Windows 11 version

  1. The latest opencv-python version installed is "4.10.0.84"
  2. Opencv should be the opencv_videoio_ffmpeg4100_64.dll that comes with the opencv-python package Lib\site-packages\cv2. Opencv is not installed separately on this machine In this environment, UMat (mat) conversion is not successful
advait-zx commented 2 weeks ago

You can use umat = mat.getUMat check if it works?

JV-X commented 1 week ago

You can use umat = mat.getUMat check if it works?

I tried it, but mat doesn't have the method getUMat, the error message as below:

AttributeError (note: full exception trace is shown but execution is paused at: _run_module_as_main)
'Mat' object has no attribute 'getUMat'