1/28/2015

Canny edge detector, example source code in opencv

Canny edge processing example



cpu version result



gpu version result






...
cpu version code.
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
//#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

void ProccTimePrint( unsigned long Atime , string msg);

int main()  
{  

 //video input
 VideoCapture cap("C:\\videoSample\\tracking\\rouen_video.avi");

 //variable
 Mat o_frame;  
 Mat showMat_r;  
 Mat showMat_r2;  
 

 //first frame
 cap >> o_frame;  
 if( o_frame.empty() )  
  return 0;   


 unsigned long AAtime=0;
 namedWindow("origin",0);
 namedWindow("canny",0);

 while(1)  
 {  
  /////////////////////////////////////////////////////////////////////////  
  AAtime = getTickCount();  

  //frame
  cap >> o_frame;  
  if( o_frame.empty() )  
   return 0;  

  resize(o_frame, showMat_r, Size(RWIDTH, RHEIGHT) );  
  Canny(showMat_r, showMat_r2, 50, 100);

  imshow("origin", showMat_r);  
  imshow("canny", showMat_r2);  

  //processing time
  ProccTimePrint(AAtime , "Total");     

  if( waitKey(5) > 0)  
   break;  

 }

 return 0;
}


void ProccTimePrint( unsigned long Atime , string msg)     
{     
 unsigned long Btime=0;     
 float sec, fps;     
 Btime = getTickCount();     
 sec = (Btime - Atime)/getTickFrequency();     
 fps = 1/sec;     
 printf("%s %.4lf(sec) / %.4lf(fps) \n", msg.c_str(),  sec, fps );     
}

///

gpu version code
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

void ProccTimePrint( unsigned long Atime , string msg);

int main()  
{  

 //input
 VideoCapture cap("C:\\videoSample\\tracking\\rouen_video.avi");

 //variable
 Mat o_frame;  
 Mat showMat_r;  
 Mat showMat_r2;  
 gpu::GpuMat o_frame_gpu;
 gpu::GpuMat r_frame_gpu;
 gpu::GpuMat rg_frame_gpu;
 gpu::GpuMat r_frame_gpu2;

 //first frame
 cap >> o_frame;  
 if( o_frame.empty() )  
  return 0;   


 unsigned long AAtime=0;

 while(1)  
 {  
  /////////////////////////////////////////////////////////////////////////  
  AAtime = getTickCount();  

  //frame
  cap >> o_frame;  
  if( o_frame.empty() )  
   return 0;  

  //upload to gpumat
  o_frame_gpu.upload(o_frame);  
  gpu::resize(o_frame_gpu, r_frame_gpu, Size(RWIDTH, RHEIGHT) );  
  gpu::cvtColor(r_frame_gpu, rg_frame_gpu, CV_BGR2GRAY);
  gpu::Canny(rg_frame_gpu, r_frame_gpu2, 50, 100); //gray only

  //download to mat
  r_frame_gpu.download(showMat_r);  
  r_frame_gpu2.download(showMat_r2);  

  
  //show image
  imshow("origin", showMat_r);  
  imshow("canny", showMat_r2);  

  //processing time
  ProccTimePrint(AAtime , "Total");     

  if( waitKey(10) > 0)  
   break;  

 }

 return 0;
}


void ProccTimePrint( unsigned long Atime , string msg)     
{     
 unsigned long Btime=0;     
 float sec, fps;     
 Btime = getTickCount();     
 sec = (Btime - Atime)/getTickFrequency();     
 fps = 1/sec;     
 printf("%s %.4lf(sec) / %.4lf(fps) \n", msg.c_str(),  sec, fps );     
}



No comments:

Post a Comment