Search code examples
c++opencvimage-processingopenclimread

OpenCL copyto() consumes more time


I am comparing OpenCL UMat with that of OpenCL Mat, I run the cvtColor() function for 500 times.

int OpenCL_UMat()
{
    Mat img;
    UMat uimage, U1;
    img = imread("image_path");
    for (int i = 1; i < 500; i++)
    {
        img.copyTo(uimage); // Here it takes 1 sec
        cvtColor(uimage, U1, CV_BGR2GRAY);
    }
    imshow("image1", U1);
    return 0;
}

int OpenCL_Mat()
{
    Mat img, img1;
    img = imread("image_path");
    for (int i = 1; i < 500; i++)
    {
        cvtColor(img, img1, CV_BGR2GRAY);
    }
    imshow("image1", img);
    return 0;
}

The output for the code in microseconds is
_________________________________
| OpenCL UMat= 2368720 microseconds-|
|_________________________________|
| OpenCL Mat = 312200 microseconds----|
|_________________________________|

From the above code i found that copyto() function needs time to load the image from RAM on to GPU, so when the copyto() function runs, it consumes 1 sec to execute.

Is there any option, where i can directly make use of UMat.
Something like this
UMat img = imread("image_path");


Solution

  • You can use

    UMat img = imread("image_path").getUMat(ACCESS_READ);