I am trying to understand the potential of OpenCL module of OpenCV 3.0.0.
This is related to a previous question of mine, where I understood that there might not exist many answers about this topic just yet. So I decided to ask you guys to help me benchmarking a piece of code.
I wrote an easy to compile simple code snippet that uses a webcam stream and finds Sobel edges and then blurs image some dozens of times. I'm blurring the image multiple times to create a strain on the processor, but feel free to change that part of the code, what is important is the comparison between OpenCL and non-OpenCL versions.
CODE:
#include <iostream>
#include <ctime>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/ocl.hpp>
using namespace std;
int main()
{
cout << "Have OpenCL?: " << cv::ocl::haveOpenCL() << endl;
// SWITCH OPENCL ON/OFF BY CHANGING THE INPUT OF THE FUNCTION BELLOW
cv::ocl::setUseOpenCL(true);
int nBlurs = 50;
cv::VideoCapture cam;
if (!cam.open(0))
std::cout << "Problem connecting to cam " << std::endl;
else
std::cout << "Successfuly connected to camera " << std::endl;
double timerSec = 0;
unsigned short frameCounter = 0;
while (cv::waitKey(1))
{
std::clock_t start;
double time;
start = std::clock();
cv::UMat frame;
cv::UMat frameGray;
cam.read(frame);
cv::cvtColor(frame, frameGray, cv::COLOR_BGR2GRAY);
cv::UMat frameSobelx;
cv::UMat frameSobely;
cv::Sobel(frameGray, frameSobelx, frameGray.depth(), 1, 0, 3);
cv::Sobel(frameGray, frameSobely, frameGray.depth(), 0, 1, 3);
cv::UMat frameSobel;
cv::bitwise_or(frameSobelx, frameSobely, frameSobel);
cv::UMat blurredSobel;
for (int n = 0; n < nBlurs; n++)
cv::blur(frameSobel, blurredSobel, cv::Size(3,3));
cv::imshow("Sobel blurred Frame", blurredSobel);
frameCounter++;
time = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
timerSec += time;
if (timerSec >= 1)
{
timerSec = 0;
cout << "Frames per second: " << frameCounter << endl;
frameCounter = 0;
}
}
return 0;
}
My System
Processor: Intel Core 2 duo 3GHZ
Grahics Card: Nvidia GeForce GT 220
OS: Linux Gentoo
My Results
With OpenCL: 15 FPS, 150% CPU usage and 88% GPU usage
Without OpenCL: 27 FPS, 98% CPU usage and 5% GPU usage
This result is extremely strange.. how come that with OpenCL the GPU is working at 88% capacity and the CPU is at 150%? And, on top of that? It is slower than the non-GPU version?
I'm trying to figure out if this is a hardware problem. Could anyone please compile and make this test?
OPENCL
NO-OPENCL
Best regards!