Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty code to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

using namespace std;

int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
        std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    if(use_opencl) {
        std::cout << "OPENCL VERSION" << std::endl;
        cv::VideoCapture cam;

        if (!cam.open(0))
            std::cout << "Problem connecting to cam " << std::endl;
        else
            std::cout << "Successfuly connected to camera " << std::endl;

        long frameCounter = 0;

        std::time_t timeBegin = std::time(0);
        int tick = 0;

        cv::Mat frame;
        cv::Mat blurredSobel;

        char c;
        do
        {
            cam.read(frame);

            double t = (double) cv::getTickCount();
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;

            t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
            cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

            cv::imshow("Sobel blurred Frame", blurredSobel);

            frameCounter++;

            std::time_t timeNow = std::time(0) - timeBegin;

            c = cv::waitKey(30);

            if (timeNow - tick >= 1)
            {
                tick++;
                //cout << "Frames per second: " << frameCounter << endl;
                frameCounter = 0;
            }
        } while(c != 27);
    } else {
        std::cout << "CPU VERSION" << std::endl;
        cv::VideoCapture cam;

        if (!cam.open(0))
            std::cout << "Problem connecting to cam " << std::endl;
        else
            std::cout << "Successfuly connected to camera " << std::endl;

        long frameCounter = 0;

        std::time_t timeBegin = std::time(0);
        int tick = 0;

        cv::Mat frame;
        cv::Mat frameGray;
        cv::Mat frameSobelx;
        cv::Mat frameSobely;
        cv::Mat blurredSobel;

        char c;
        do
        {
            cam.read(frame);

            double t = (double) cv::getTickCount();

            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
            t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
            cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

            cv::imshow("Sobel blurred Frame", blurredSobel);

            frameCounter++;

            std::time_t timeNow = std::time(0) - timeBegin;

            c = cv::waitKey(30);

            if (timeNow - tick >= 1)
            {
                tick++;
                //cout << "Frames per second: " << frameCounter << endl;
                frameCounter = 0;
            }
        } while(c != 27);
    }

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPS
  • GPU version=~90 FPS

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty code to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

using namespace std;

int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
        std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    if(use_opencl) {
        std::cout << "OPENCL VERSION" << std::endl;
        cv::VideoCapture cam;

        if (!cam.open(0))
            std::cout << "Problem connecting to cam " << std::endl;
        else
            std::cout << "Successfuly connected to camera " << std::endl;

        long frameCounter = 0;

        std::time_t timeBegin = std::time(0);
        int tick = 0;

        cv::Mat frame;
        cv::Mat blurredSobel;

        char c;
        do
        {
            cam.read(frame);

            double t = (double) cv::getTickCount();
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;

            t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
            cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

            cv::imshow("Sobel blurred Frame", blurredSobel);

            frameCounter++;

            std::time_t timeNow = std::time(0) - timeBegin;

            c = cv::waitKey(30);

            if (timeNow - tick >= 1)
            {
                tick++;
                //cout << "Frames per second: " << frameCounter << endl;
                frameCounter = 0;
            }
        } while(c != 27);
    } else {
        std::cout << "CPU VERSION" << std::endl;
        cv::VideoCapture cam;

        if (!cam.open(0))
            std::cout << "Problem connecting to cam " << std::endl;
        else
            std::cout << "Successfuly connected to camera " << std::endl;

        long frameCounter = 0;

        std::time_t timeBegin = std::time(0);
        int tick = 0;

        cv::Mat frame;
        cv::Mat frameGray;
        cv::Mat frameSobelx;
        cv::Mat frameSobely;
        cv::Mat blurredSobel;

        char c;
        do
        {
            cam.read(frame);

            double t = (double) cv::getTickCount();

            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
            t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
            cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

            cv::imshow("Sobel blurred Frame", blurredSobel);

            frameCounter++;

            std::time_t timeNow = std::time(0) - timeBegin;

            c = cv::waitKey(30);

            if (timeNow - tick >= 1)
            {
                tick++;
                //cout << "Frames per second: " << frameCounter << endl;
                frameCounter = 0;
            }
        } while(c != 27);
    }

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPS
  • GPU version=~90 FPS

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

Edit: Additionnal information:

  • Windows platform + Visual Studio 2010 + Release mode
  • to set the GPU device: add the environment variable OPENCV_OPENCL_DEVICE with for example: :GPU:2

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty code to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

using namespace std;

int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
        std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    if(use_opencl) {
        std::cout << "OPENCL VERSION" << std::endl;
     cv::VideoCapture cam;

     if (!cam.open(0))
    (!cam.open(0)) {
        std::cout << "Problem connecting to cam " << std::endl;
        else
    }
    else {
        std::cout << "Successfuly connected to camera " << std::endl;
     }

    long frameCounter = 0;

     std::time_t timeBegin = std::time(0);
     int tick = 0;

     cv::Mat frame;
    cv::Mat frameGray;
    cv::Mat frameSobelx;
    cv::Mat frameSobely;
    cv::Mat blurredSobel;

     char c;
     do
     {
         cam.read(frame);

         double t = (double) cv::getTickCount();
        if(use_opencl) {
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;
         } else {
            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
        }

        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
         cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

         cv::imshow("Sobel blurred Frame", blurredSobel);

         frameCounter++;

         std::time_t timeNow = std::time(0) - timeBegin;

         c = cv::waitKey(30);

         if (timeNow - tick >= 1)
         {
             tick++;
             //cout << "Frames per second: " << frameCounter << endl;
             frameCounter = 0;
         }
     } while(c != 27);
    } else {
        std::cout << "CPU VERSION" << std::endl;
        cv::VideoCapture cam;

        if (!cam.open(0))
            std::cout << "Problem connecting to cam " << std::endl;
        else
            std::cout << "Successfuly connected to camera " << std::endl;

        long frameCounter = 0;

        std::time_t timeBegin = std::time(0);
        int tick = 0;

        cv::Mat frame;
        cv::Mat frameGray;
        cv::Mat frameSobelx;
        cv::Mat frameSobely;
        cv::Mat blurredSobel;

        char c;
        do
        {
            cam.read(frame);

            double t = (double) cv::getTickCount();

            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
            t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
            cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

            cv::imshow("Sobel blurred Frame", blurredSobel);

            frameCounter++;

            std::time_t timeNow = std::time(0) - timeBegin;

            c = cv::waitKey(30);

            if (timeNow - tick >= 1)
            {
                tick++;
                //cout << "Frames per second: " << frameCounter << endl;
                frameCounter = 0;
            }
        } while(c != 27);
    }

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPS
  • GPU version=~90 version=~70 FPS

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

Edit: Additionnal information:

  • Windows platform + Visual Studio 2010 + Release mode
  • to set the GPU device: add the environment variable OPENCV_OPENCL_DEVICE with for example: :GPU:2

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty code to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
        std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    std::cout << "OPENCL VERSION" << std::endl;
    cv::VideoCapture cam;

    if (!cam.open(0)) {
        std::cout << "Problem connecting to cam " << std::endl;
    }
    else {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    long frameCounter = 0;

    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;
    cv::Mat frameGray;
    cv::Mat frameSobelx;
    cv::Mat frameSobely;
    cv::Mat blurredSobel;

    char c;
    do
    {
        cam.read(frame);

        double t = (double) cv::getTickCount();
        if(use_opencl) {
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;
        } else {
            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
        }

        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

        cv::imshow("Sobel blurred Frame", blurredSobel);

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        c = cv::waitKey(30);

        if (timeNow - tick >= 1)
        {
            tick++;
            //cout << "Frames per second: " << frameCounter << endl;
            frameCounter = 0;
        }
    } while(c != 27);

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPS
  • GPU version=~70 FPS

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

Edit: Additionnal information:

  • Windows platform + Visual Studio 2010 + Release mode
  • to set the GPU device: add the environment variable OPENCV_OPENCL_DEVICE with for example: :GPU:2

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty code to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
        std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    cv::VideoCapture cam;

    if (!cam.open(0)) {
        std::cout << "Problem connecting to cam " << std::endl;
    }
    else {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    long frameCounter = 0;

    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;
    cv::Mat frameGray;
    cv::Mat frameSobelx;
    cv::Mat frameSobely;
    cv::Mat blurredSobel;

    char c;
    do
    {
        cam.read(frame);

        double t = (double) cv::getTickCount();
        if(use_opencl) {
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;
        } else {
            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
        }

        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

        cv::imshow("Sobel blurred Frame", blurredSobel);

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        c = cv::waitKey(30);

        if (timeNow - tick >= 1)
        {
            tick++;
            //cout << "Frames per second: " << frameCounter << endl;
            frameCounter = 0;
        }
    } while(c != 27);

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPSFPS ; CPU load=92%
  • GPU version=~70 FPSFPS ; CPU load=8% ; GPU load=7%

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

Edit: Additionnal information:

  • Windows platform + Visual Studio 2010 + Release mode
  • to set the GPU device: add the environment variable OPENCV_OPENCL_DEVICE with for example: :GPU:2

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty code to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
        std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    cv::VideoCapture cam;

    if (!cam.open(0)) {
        std::cout << "Problem connecting to cam " << std::endl;
    }
    else {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    long frameCounter = 0;

    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;
    cv::Mat frameGray;
    cv::Mat frameSobelx;
    cv::Mat frameSobely;
    cv::Mat blurredSobel;

    char c;
    do
    {
        cam.read(frame);

        double t = (double) cv::getTickCount();
        if(use_opencl) {
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;
        } else {
            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
        }

        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

        cv::imshow("Sobel blurred Frame", blurredSobel);

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        c = cv::waitKey(30);

        if (timeNow - tick >= 1)
        {
            tick++;
            //cout << "Frames per second: " << frameCounter << endl;
            frameCounter = 0;
        }
    } while(c != 27);

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPS ; CPU load=92%load=~92%
  • GPU version=~70 FPS ; CPU load=8% load=~8% ; GPU load=7%load=~7%

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

Edit: Additionnal information:

  • Windows platform + Visual Studio 2010 + Release mode
  • to set the GPU device: add the environment variable OPENCV_OPENCL_DEVICE with for example: :GPU:2

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty code to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
        std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    cv::VideoCapture cam;

    if (!cam.open(0)) {
        std::cout << "Problem connecting to cam " << std::endl;
    }
    else {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    long frameCounter = 0;

    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;
    cv::Mat frameGray;
    cv::Mat frameSobelx;
    cv::Mat frameSobely;
    cv::Mat blurredSobel;

    char c;
    do
    {
        cam.read(frame);

        double t = (double) cv::getTickCount();
        if(use_opencl) {
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;
        } else {
            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
        }

        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

        cv::imshow("Sobel blurred Frame", blurredSobel);

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        c = cv::waitKey(30);

        if (timeNow - tick >= 1)
        {
            tick++;
            //cout << "Frames per second: " << frameCounter << endl;
            frameCounter = 0;
        }
    } while(c != 27);

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPS ; CPU load=~92%
  • GPU version=~70 FPS ; CPU load=~8% ; GPU load=~7%

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

Edit: Additionnal information:

  • Windows platform + Visual Studio 2010 + Release mode
  • to set the GPU device: add the environment variable OPENCV_OPENCL_DEVICE with for example: :GPU:2:GPU:1

Edit2:

I retried with this time OpenCV 3.0.0 from master (as of 26/03/2015) and I built it with VS2010, 64 bits, release mode and without IPP. I manage to have with this configuration coherent results:

  • setUseOpenCL(false): ~24 FPS ; ~8% for CPU load
  • setUseOpenCL(true): ~200 FPS ; ~15% for CPU load ; ~< 10% for GPU load

To calculate the FPS, I measure only the time to process the image (without the time to acquire the image, without the time to display the image). I still have to set the correct device as I have 2 GPU (Intel and NVidia).

It seems when I look to the code that if OpenCV is built with IPP and OpenCL, IPP is prioritized against OpenCL: https://github.com/Itseez/opencv/blob/master/modules/imgproc/src/deriv.cpp#L575.

I think it explains why even if we enable OpenCL, we have bad result.

One last thing, it seems that on my computer the version with IPP is slower than the version without.

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty code to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
        std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    cv::VideoCapture cam;

    if (!cam.open(0)) {
        std::cout << "Problem connecting to cam " << std::endl;
    }
    else {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    long frameCounter = 0;

    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;
    cv::Mat frameGray;
    cv::Mat frameSobelx;
    cv::Mat frameSobely;
    cv::Mat blurredSobel;

    char c;
    do
    {
        cam.read(frame);

        double t = (double) cv::getTickCount();
        if(use_opencl) {
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;
        } else {
            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
        }

        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;

        cv::imshow("Sobel blurred Frame", blurredSobel);

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        c = cv::waitKey(30);

        if (timeNow - tick >= 1)
        {
            tick++;
            //cout << "Frames per second: " << frameCounter << endl;
            frameCounter = 0;
        }
    } while(c != 27);

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPS ; CPU load=~92%
  • GPU version=~70 FPS ; CPU load=~8% ; GPU load=~7%

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

Edit: Additionnal information:

  • Windows platform + Visual Studio 2010 + Release mode
  • to set the GPU device: add the environment variable OPENCV_OPENCL_DEVICE with for example: :GPU:1

Edit2:

I retried with this time OpenCV 3.0.0 from master (as of 26/03/2015) and I built it with VS2010, 64 bits, release mode and without IPP. I manage to have with this configuration coherent results:

  • setUseOpenCL(false): ~24 FPS ; ~8% for CPU load
  • setUseOpenCL(true): ~200 FPS ; ~15% for CPU load ; ~< 10% for GPU load

To calculate the FPS, I measure only the time to process the image (without the time to acquire the image, without the time to display the image). I still have to set the correct device as I have 2 GPU (Intel and NVidia).

It seems when I look to the code that if OpenCV is built with IPP and OpenCL, IPP is prioritized against OpenCL: https://github.com/Itseez/opencv/blob/master/modules/imgproc/src/deriv.cpp#L575.

I think it explains why even if we enable OpenCL, we have bad result.

One last thing, it seems that on my computer the version with IPP is slower than the version without.

Edit 3:

I have good results (24 FPS for CPU, 200 FPS for GPU) with the pre-built OpenCV 3.0.0 beta just by adding the correct environment variable to set the GPU device. Without it, I have the same result between OpenCL true/false.

In fact, I have so many OpenCV versions on my computer that I'm not sure what is the main reason of the issue (bug in OpenCV 3.0.0 beta/master ?, problem with IPP ?, problem with 2 graphics chipset ?)... :)

I did a quick test with OpenCV 3.0.0 beta and I realised that the OpenCL version used my Intel HD Graphics instead of my NVidia GPU (see my comments).

I tested also with OpenCV 2.4.10 and it is possible to set the device for OpenCL: http://docs.opencv.org/modules/ocl/doc/introduction.html.

This is a dirty the code I use to test OpenCL with OpenCV 2.4.10:

#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>

 int main(int argc, char**argv)
{
    int nBlurs = 50;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";
    }

    std::cout << "use_opencl=" << use_opencl << std::endl;
    }

    cv::VideoCapture cam;

    if (!cam.open(0)) {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    long frameCounter = 0;

    std::time_t timeBegin = std::time(0);
    int tick = 0;

    cv::Mat frame;
    cv::Mat frameGray;
    cv::Mat frameSobelx;
    cv::Mat frameSobely;
    cv::Mat blurredSobel;

    char c;
    double total_time = 0.0;
    int cpt = 0;
    do
    {
        cam.read(frame);

        double t = (double) cv::getTickCount();
        if(use_opencl) {
            cv::ocl::oclMat frame_ocl, frameGray_ocl, frameSobelx_ocl, frameSobely_ocl, blurredSobel_ocl;
            frame_ocl.upload(frame);

            cv::ocl::cvtColor(frame_ocl, frameGray_ocl, cv::COLOR_BGR2GRAY);

            cv::ocl::Sobel(frameGray_ocl, frameSobelx_ocl, frameGray_ocl.depth(), 1, 0, 3);
            cv::ocl::Sobel(frameGray_ocl, frameSobely_ocl, frameGray_ocl.depth(), 0, 1, 3);

            cv::ocl::bitwise_or(frameSobelx_ocl, frameSobely_ocl, frameGray_ocl);

            for (int n = 0; n < nBlurs; n++) {
                cv::ocl::blur(frameGray_ocl, blurredSobel_ocl, cv::Size(3,3));
            }

            blurredSobel = blurredSobel_ocl;
        } else {
            cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

            cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
            cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

            cv::bitwise_or(frameSobelx, frameSobely, frameGray);

            for (int n = 0; n < nBlurs; n++) {
                cv::blur(frameGray, blurredSobel, cv::Size(3,3));
            }
        }

        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        cout total_time += t;
        cpt++;
        std::cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << endl;
" ; Average FPS=" << (cpt/total_time) << std::endl;

        cv::imshow("Sobel blurred Frame", blurredSobel);

        frameCounter++;

        std::time_t timeNow = std::time(0) - timeBegin;

        c = cv::waitKey(30);

        if (timeNow - tick >= 1)
        {
            tick++;
            //cout << "Frames per second: " << frameCounter << endl;
            frameCounter = 0;
        }
    } while(c != 27);

    return 0;
}

My result (pass as an argument 1 to use the OpenCL version, otherwise it is the CPU version):

  • CPU version=~24 FPS ; CPU load=~92%
  • GPU version=~70 FPS ; CPU load=~8% ; GPU load=~7%

Also, you can use a program to monitor the GPU load (e.g. GPU-Z). Mine is about 7% when the program is running.

Edit: Additionnal information:

  • Windows platform + Visual Studio 2010 + Release mode
  • to set the GPU device: add the environment variable OPENCV_OPENCL_DEVICE with for example: :GPU:1

Edit2:

I retried with this time OpenCV 3.0.0 from master (as of 26/03/2015) and I built it with VS2010, 64 bits, release mode and without IPP. I manage to have with this configuration coherent results:

  • setUseOpenCL(false): ~24 FPS ; ~8% for CPU load
  • setUseOpenCL(true): ~200 FPS ; ~15% for CPU load ; ~< 10% for GPU load

To calculate the FPS, I measure only the time to process the image (without the time to acquire the image, without the time to display the image). I still have to set the correct device as I have 2 GPU (Intel and NVidia).

It seems when I look to the code that if OpenCV is built with IPP and OpenCL, IPP is prioritized against OpenCL: https://github.com/Itseez/opencv/blob/master/modules/imgproc/src/deriv.cpp#L575.

I think it explains why even if we enable OpenCL, we have bad result.

One last thing, it seems that on my computer the version with IPP is slower than the version without.

Edit 3:

I have good results (24 FPS for CPU, 200 FPS for GPU) with the pre-built OpenCV 3.0.0 beta just by adding the correct environment variable to set the GPU device. Without it, I have the same result between OpenCL true/false.

In fact, I have so many OpenCV versions on my computer that I'm not sure what is the main reason of the issue (bug in OpenCV 3.0.0 beta/master ?, problem with IPP ?, problem with 2 graphics chipset ?)... :)

Edit 4: The code I used with OpenCV 3.0.0:

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/ocl.hpp>


int main(int argc, char** argv)
{
    std::cout << "Have OpenCL ?: " << cv::ocl::haveOpenCL() << std::endl;

    bool use_opencl = false;
    if(argc > 1) {
        use_opencl = std::string(argv[1]) == "1";    
    }

    std::cout << "use_opencl=" << use_opencl << std::endl;

/*  SWITCH OPENCL ON/OFF IN LINE BELLOW */
    cv::ocl::setUseOpenCL(use_opencl);
/*                                      */
    int nBlurs = 50;

    cv::VideoCapture cam;

    if (!cam.open(0)) {
        std::cout << "Problem connecting to cam " << std::endl;
        return -1;
    }
    else {
        std::cout << "Successfuly connected to camera " << std::endl;
    }

    cv::UMat frame;
    cv::UMat frameGray;
    cv::UMat frameSobelx;
    cv::UMat frameSobely;

    cv::UMat frameSobel;
    cv::UMat blurredSobel;

    char c;
    double total_time = 0.0;
    int cpt = 0;
    do
    {
        cam.read(frame);

        double t = (double) cv::getTickCount();
        cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);

        cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
        cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);

        cv::bitwise_or(frameSobelx, frameSobely, frameSobel);

        for (int n = 0; n < nBlurs; n++) {
            cv::blur(frameSobel, blurredSobel, cv::Size(3,3));
        }

        t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
        total_time += t;
        cpt++;
        std::cout << "Times passed in seconds: " << t << " ; FPS: " << (1/t) << " ; average FPS=" << (cpt/total_time) << std::endl;

        cv::imshow("Sobel blurred Frame", blurredSobel);

        c = cv::waitKey(30);
    } while(c != 27);

    return 0;
}