13

I want to use UMat so my code can be run on both GPU and CPU using OpenCL (OpenCV 3.0.0 Beta).

but I can not find a way to read an image file into a UMat or convert a Mat to UMat.

How can I read an image into a UMat?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
mans
  • 17,104
  • 45
  • 172
  • 321

5 Answers5

17

Sample for Mat to UMat conversion is below. Coudlnt' find documentation for this. So only option was to read the source.

UMat img = imread( "lena.jpg", IMREAD_COLOR  ).getUMat( ACCESS_READ );

Different access flags as in source are

ACCESS_READ, ACCESS_WRITE, ACCESS_RW, ACCESS_FAST

For undestanding UMat usage, a complete sample for face detection is available here. Also note that documentation still refers to older flags as imread second parameter. You might need to use newer flags as in your OpenCV header file.

kiranpradeep
  • 10,859
  • 4
  • 50
  • 82
  • There's some documentation [here](http://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#ad3b86ff70d06f7e567e51d8d215c85cd), but it's indeed still pretty cryptic... – Antonio Jan 13 '16 at 15:50
3

What works for me is...

cv::UMat image;
cv::imread("image.jpeg", 1).copyTo(image);

This is using the latest opencv 3.2

JoeManiaci
  • 435
  • 3
  • 15
1

For covert I use Uimage.copyTo(image) or image=Uimage.getMat(cv::ACCESS_READ) but last in some cases generate problem with reassign data.

If you want ti use UMat globally for all, may attend that present some limitation with use UMat. And may be better have reserve method with Mat if operation with UMat have access violation error.

clearReadyResultImage();
qDebug()<<"vpOpenCV::blurImage, try GPU cv::blur";
try{
   cv::blur(Uimage, UresultImage, cv::Size(ksize, ksize));
   UresultImageReady=!UresultImage.empty();
}
catch (cv::Exception& ex) {
     qWarning()<<"vpOpenCV::blurImage error"<<ex.what();
}
if (!UresultImageReady) {
     checkReadyImage();
     qDebug()<<"vpOpenCV::blurImage, try CPU cv::blur";
     try{
         cv::blur(image, resultImage, cv::Size(ksize, ksize));
         resultImageReady=!resultImage.empty();
     }
     catch (cv::Exception& ex) {
         qWarning()<<"vpOpenCV::blurImage error"<<ex.what();
     }
}

...

void checkReadyImage(){
     if (!imageReady && UimageReady){
         Uimage.copyTo(image);
         imageReady=!image.empty();
     }
}
lexxai
  • 75
  • 9
0

Note that what I am describing here is NOT a correct answer!

According to this presentation (apparently an official one from itseez) the correct way to load an image from file directly into a cv::UMat should be the following:

UMat img;
imread( "lena.jpg", -1, img); // -1 => CV_LOAD_IMAGE_UNCHANGED (colourwise)

However, this is not part of OpenCV 3 interface, so you should really use Kiran's implementation.

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
-2

You can convert (copy the underlying data) cv::Mat to cv::UMat and vice versa by using copyTo method.

Here is the sample code, that uses cv::UMat:

//VideoCapture tCapturer("2.avi"); // source is set to be the clip
VideoCapture tCapturer(0); // source is set to be the camera
UMat tUFrame;

if (!tCapturer.isOpened())
{
    cout << "Could not open video file!" << endl;
    getchar();
    return -1;
}
else
{
    cout << "Video file has been opened successfully!" << endl;
    getchar();
}

namedWindow("video", 1);

while (true)
{
    if (!tClip.read(tUFrame)) // get a new frame from camera
    {
        break;
    }

    Mat tRegularFrame;
    tUFrame.copyTo(tRegularFrame);

    imshow("video", tRegularFrame);

    if (waitKey(30) >= 0)
    {
        break;
    }
}
y434y
  • 633
  • 1
  • 8
  • 27