12/05/2013

OpenCV Cuda Example source code

#include < stdio.h >  
#include < opencv2\opencv.hpp >
#include < opencv2\gpu\gpu.hpp >
 
#ifdef _DEBUG  
#pragma comment(lib, "opencv_core246d.lib")   
#pragma comment(lib, "opencv_imgproc246d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_objdetect246d.lib")   
#pragma comment(lib, "opencv_gpu246d.lib")  
//#pragma comment(lib, "opencv_features2d246d.lib")  
#pragma comment(lib, "opencv_highgui246d.lib")   
//#pragma comment(lib, "opencv_ml246d.lib")  
#else  
#pragma comment(lib, "opencv_core246.lib")  
#pragma comment(lib, "opencv_imgproc246.lib")  
//#pragma comment(lib, "opencv_objdetect246.lib")  
#pragma comment(lib, "opencv_gpu246.lib")  
//#pragma comment(lib, "opencv_features2d246.lib")  
#pragma comment(lib, "opencv_highgui246.lib")  
//#pragma comment(lib, "opencv_ml246.lib")  
#endif  

using namespace cv;  


void main()  
{  
 unsigned long AAtime=0, BBtime=0;
 
 Mat img;
 Mat outimg, outimg2;
 img = imread("Tulips.jpg",0); 
 gpu::GpuMat d_src, d_dst;
 
 d_src.upload(img);
 AAtime = getTickCount(); 
 gpu::Canny(d_src, d_dst, 35, 200, 3); 
 BBtime = getTickCount();

 printf("cuda %.5lf \n",  (BBtime - AAtime)/getTickFrequency() );
 d_dst.download(outimg);


 AAtime = getTickCount();
 Canny(img, outimg2, 35, 200, 3);
 BBtime = getTickCount();
 printf("cpu %.5lf \n",  (BBtime - AAtime)/getTickFrequency() );


 

 namedWindow("t");  
 imshow("t",outimg2); 
 namedWindow("t2");  
 imshow("t2",outimg);  
 
 cvWaitKey(0);  
 

} 


////

cuda processing takes 0.00578 sec.
and non-cuda processing takes 0.01707 sec.

In this case, cuda is faster 2times than non-cuda, but cuda will be seen higher speed.

CUDA + OpenCV Setting(install) method, OpenCV programing using CUDA

For using CUDA-OpenCV, you have to made dll, lib newly.
Because download dll, lib is often not included cuda function.

Firstly, you have to download theses files. (prepare these files)

1 .OpenCV 2.26
-http://opencv.org/downloads.html

2. CUDA development toolkit
-https://developer.nvidia.com/cuda-downloads
If your OS is window 7 64bit and desktop, download  cuda-winvista-win7-win8-general-64 version

3. CMake
-http://www.cmake.org/cmake/resources/software.html

and unzip or install 1, 2, 3 file.

and Excute CMake program.

'Making_GPUOpenCV_forPosting' folder is made newly in the C:/opencv folder.
click configuration and select your compile version, in my case I use vs 2012, so I selecte visual studio 11.


Then, you will see following picture.



Now, select cuda releated options.
In my case, I have selected 3 options, 'CUDA_FAST_MATH, WITH_CUBLAS, WITH_CUDA'.
And you must check the cuda toolkit path.



And click configuration again.
check!, cuda option is included or not, you can see the contents at the below window.




Next, click generate.


Files is made in the folder.
And open the "OpenCV.sin" file as compiler.



Next, Compile and prayer~
If you have success to compile, you can get Lib and Dll files of CUDA version.


Make debug and release version dlls and libs.
And gather files to the one folder to use comfortable.




  
Test cuda version opencv libs.
This is exaple source code.

caution!!
Do not cuda programming on the remote mode.
And gpu graphics card is required.