opencv 3.0 cascade_gpu example source code

opencv 2.49 example is here
http://study.marearts.com/2014/09/opencv-face-detection-using-adaboost.html



#include < iostream>    
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\cudaobjdetect.hpp"
#include "opencv2\cudaimgproc.hpp"

#ifdef _DEBUG               
#pragma comment(lib, "opencv_core300d.lib")       
#pragma comment(lib, "opencv_highgui300d.lib")    
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_objdetect300d.lib")  
#pragma comment(lib, "opencv_imgproc300d.lib")  
#pragma comment(lib, "opencv_cudaobjdetect300d.lib")  
#else       
#pragma comment(lib, "opencv_core300.lib")       
#pragma comment(lib, "opencv_highgui300.lib")    
#pragma comment(lib, "opencv_imgcodecs300.lib")    
#pragma comment(lib, "opencv_objdetect300.lib")  
#pragma comment(lib, "opencv_imgproc300.lib")  
#pragma comment(lib, "opencv_cudaobjdetect300.lib")  
#endif        

using namespace std;
using namespace cv;


void main()
{
 //for time measure  
 float TakeTime;
 unsigned long Atime, Btime;

 //window  
 namedWindow("origin");

 //load image  
 Mat img = imread("sh.jpg");
 Mat grayImg; //adaboost detection is gray input only.  
 cvtColor(img, grayImg, CV_BGR2GRAY);

 //load xml file  
 string trainface = ".\\haarcascade_frontalface_alt.xml";

 //declaration  
 Ptr< cuda::CascadeClassifier> cascade_gpu = cuda::CascadeClassifier::create(trainface);

 
 /////////////////////////////////////////////  
 
 //gpu case face detection code  
 cuda::GpuMat faceBuf_gpu;
 cuda::GpuMat GpuImg;
 vector< Rect> faces;

 GpuImg.upload(grayImg);
 Atime = getTickCount();

 cascade_gpu->detectMultiScale(GpuImg, faceBuf_gpu);
 cascade_gpu->convert(faceBuf_gpu, faces);
 Btime = getTickCount();
 TakeTime = (Btime - Atime) / getTickFrequency();
 printf("detected face(gpu version) =%d / %lf sec take.\n", faces.size(), TakeTime);
 Mat faces_downloaded;
 if (faces.size() >= 1)
 {
  for (size_t i = 0; i < faces.size(); ++i)
   rectangle(img, faces[i], Scalar(255));
 } 

 /////////////////////////////////////////////////  
 //result display  
 imshow("origin", img);
 waitKey(0);
}


Comments

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)