Skip to content

Instantly share code, notes, and snippets.

@pi-null-mezon
Forked from LaurentBerger/testOCL_OCV32.cpp
Last active December 30, 2016 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pi-null-mezon/89d51f2e119d8032c91b87239d8505b0 to your computer and use it in GitHub Desktop.
Save pi-null-mezon/89d51f2e119d8032c91b87239d8505b0 to your computer and use it in GitHub Desktop.
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/core/ocl.hpp>
#include <iostream>
using namespace cv;
using namespace std;
string imagePath="C:/Testdata/Image/Lighthouse.jpg";
int TestOpenCL(int nbTest);
int main(int argc, char **argv)
{
if (!cv::ocl::haveOpenCL())
{
cout << "OpenCL is not avaiable..." << endl;
return 0;
}
cv::ocl::Context context;
std::vector<cv::ocl::PlatformInfo> platforms;
cv::ocl::getPlatfomsInfo(platforms);
//OpenCL Platforms
for (size_t i = 0; i < platforms.size(); i++)
{
//Access to Platform
const cv::ocl::PlatformInfo* platform = &platforms[i];
//Platform Name
std::cout << "Platform Name: " << platform->name().c_str() << "\n";
//Access Device within Platform
cv::ocl::Device current_device;
for (int j = 0; j < platform->deviceNumber(); j++)
{
//Access Device
platform->getDevice(current_device, j);
//Device Type
int deviceType = current_device.type();
switch (deviceType) {
case (1 << 1):
cout<<" CPU device\n";
break;
case (1 << 2) :
cout << " GPU device\n";
if (context.create(deviceType))
TestOpenCL(50);
break;
}
}
}
return 0;
}
int TestOpenCL(int nbTest)
{
Mat gray, grayres, imageres;
cout << "CPU info: getNumberOfCPUs =" << getNumberOfCPUs() << "\t getNumThreads = " << getNumThreads() << "\n";
Mat image = imread(imagePath.c_str(), CV_LOAD_IMAGE_UNCHANGED);
double totalTime = 0;
UMat uimage, uimageres;
UMat ugray, ugrayres;
uimage = image.getUMat(cv::ACCESS_FAST);
vector<double> tps;
tps.resize(nbTest);
TickMeter myChrono;
double m,v;
// ********************** CVTCOLOR ************
cout << "****************************\ncvtColor\n****************************";
// 1 UMAT ocl false
ocl::setUseOpenCL(false);
cout << "\nUMat without opencl: ";
cvtColor(uimage, ugray, COLOR_BGR2GRAY);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
cvtColor(uimage, ugray, COLOR_BGR2GRAY);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m=0;v=0;
for (int nTest = 0; nTest<nbTest; nTest++)
m+= tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m-tps[nTest],2.0);
v = sqrt( v / (nbTest - 1) );;
cout << "\t" << m << " +/-" <<v;
// 2 UMAT ocl true
ocl::setUseOpenCL(true);
cout << "\nUMat+opencl: ";
cvtColor(uimage, ugray, COLOR_BGR2GRAY);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
cvtColor(uimage, ugray, COLOR_BGR2GRAY);
myChrono.stop();
tps[nTest]=myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t\t" << m << " +/-" << v;
// 3 MAT ocl false
ocl::setUseOpenCL(false);
cout << "\nMat without opencl: ";
cvtColor(image, gray, COLOR_BGR2GRAY);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
cvtColor(image, gray, COLOR_BGR2GRAY);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t" << m << " +/-" << v;
// 4 MAT ocl true
ocl::setUseOpenCL(true);
cout << "\nMat+opencl: ";
cvtColor(image, gray, COLOR_BGR2GRAY);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
cvtColor(image, gray, COLOR_BGR2GRAY);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t\t" << m << " +/-" << v;
// ********************** GAUSSIANBLUR ************
cout << "\n\n****************************\nGaussianBlur\n****************************";
ocl::setUseOpenCL(false);
cout << "\nUMat without opencl: ";
GaussianBlur(ugray, ugrayres, Size(101,101), 1.5);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
GaussianBlur(ugray, ugrayres, Size(101,101), 1.5);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t" << m << " +/-" << v << " ms";
ocl::setUseOpenCL(true);
cout << "\nUMat+opencl: ";
GaussianBlur(ugray, ugrayres, Size(101,101), 1.5);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
GaussianBlur(ugray, ugrayres, Size(101,101), 1.5);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t\t" << m << " +/-" << v << " ms";
// 3 MAT ocl false
ocl::setUseOpenCL(false);
cout << "\nMat without opencl: ";
GaussianBlur(gray, grayres, Size(101,101), 1.5);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
GaussianBlur(gray, grayres, Size(101,101), 1.5);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t" << m << " +/-" << v << " ms";
// 4 MAT ocl true
ocl::setUseOpenCL(true);
cout << "\nMat+opencl: ";
GaussianBlur(gray, grayres, Size(101,101), 1.5);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
GaussianBlur(gray, grayres, Size(101,101), 1.5);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t\t" << m << " +/-" << v << " ms";
// ********************** Canny ************
cout << "\n\n****************************\nCanny\n****************************";
ocl::setUseOpenCL(false);
cout << "\nUMat without opencl: ";
Canny(ugray,ugrayres, 0.5, 0.9, 7);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
Canny(ugray,ugrayres, 0.5, 0.9, 7);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t" << m << " +/-" << v << " ms";
ocl::setUseOpenCL(true);
cout << "\nUMat+opencl: ";
Canny(ugray,ugrayres, 0.5, 0.9, 7);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
Canny(ugray,ugrayres, 0.5, 0.9, 7);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t\t" << m << " +/-" << v << " ms";
// 3 MAT ocl false
ocl::setUseOpenCL(false);
cout << "\nMat without opencl: ";
Canny(gray,grayres, 0.5, 0.9, 7);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
Canny(gray,grayres, 0.5, 0.9, 7);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t" << m << " +/-" << v << " ms";
// 4 MAT ocl true
ocl::setUseOpenCL(true);
cout << "\nMat+opencl: ";
Canny(gray,grayres, 0.5, 0.9, 7);
for (int nTest = 0; nTest<nbTest; nTest++)
{
myChrono.reset();
myChrono.start();
Canny(gray,grayres, 0.5, 0.9, 7);
myChrono.stop();
tps[nTest] = myChrono.getTimeMilli();
}
m = 0.0; v = 0.0;
for (int nTest = 0; nTest<nbTest; nTest++)
m += tps[nTest];
m /= nbTest;
for (int nTest = 0; nTest<nbTest; nTest++)
v += pow(m - tps[nTest], 2.0);
v = sqrt( v / (nbTest - 1) );
cout << "\t\t" << m << " +/-" << v << " ms";
cout<<"\n\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment