mans
mans

Reputation: 18178

How to read UMat from a file in opencv 3.0 Beta?

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?

Upvotes: 13

Views: 17229

Answers (5)

JoeManiaci
JoeManiaci

Reputation: 475

What works for me is...

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

This is using the latest opencv 3.2

Upvotes: 3

Antonio
Antonio

Reputation: 20296

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.

Upvotes: 0

lexxai
lexxai

Reputation: 85

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();
     }
}

Upvotes: 1

kiranpradeep
kiranpradeep

Reputation: 11211

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.

Upvotes: 18

y434y
y434y

Reputation: 642

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;
    }
}

Upvotes: -2

Related Questions