人脸识别检测头文件:
#ifndef _OBJDETECT_H_ #define _OBJDETECT_H_ extern "C" { #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <sys/mman.h> #include <sys/time.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <linux/fb.h> #include <linux/ti81xxfb.h> #include <linux/ti81xxvin.h> #include<linux/videodev2.h> } #include "opencv/highgui.h" #include "opencv/cv.h" #include "opencv/cxcore.h" #include "opencv/cvaux.h" class CObjdetect { public: CObjdetect(){ scale_input = 2.2; scale_img_size = 30; factor_k = 2.0; rangle_r = 50.0; if_alloct_for_detect=0; } ~CObjdetect(); public: int Initdetect(char * cascade_name); void detect_and_draw( IplImage* img, double scale, double k, double m ); public: double scale_input ; int scale_img_size; double factor_k ; double rangle_r; double dong_k; char send_data[50]; private: CvMemStorage* storage ; CvHaarClassifierCascade* cascade; IplImage* gray; IplImage* small_img; int if_alloct_for_detect; }; #endif /*_OBJDETECT_H_*/
源文件如下:
#include "objdetect.h" CObjdetect::~CObjdetect() { printf("exit the Objdetect \n"); } int CObjdetect::Initdetect(char * cascade_name) { cascade = (CvHaarClassifierCascade*) cvLoad(cascade_name, 0, 0, 0); if (!cascade) { fprintf(stderr, "ERROR: Could not load classifier cascade\n"); return -1; } storage = cvCreateMemStorage(0); return 0; } void CObjdetect::detect_and_draw( IplImage* img, double scale, double k, double m ) { CvPoint pt1; CvPoint pt2; CvScalar color2 = { 0, 255, 0 }; static CvScalar colors[] = { {{0,0,255}}, {{0,128,255}}, {{0,255,255}}, {{0,255,0}}, {{255,128,0}}, {{255,255,0}}, {{255,0,0}}, {{255,0,255}} }; //double scale = 3.3; if (0 == if_alloct_for_detect) { gray = cvCreateImage(cvSize(img->width, img->height), 8, 1); small_img = cvCreateImage(cvSize(cvRound(img->width / scale), cvRound( img->height / scale)), 8, 1); if_alloct_for_detect = 1; } int i; cvCvtColor( img, gray, CV_BGR2GRAY ); cvResize( gray, small_img, CV_INTER_LINEAR ); cvEqualizeHist( small_img, small_img ); cvClearMemStorage( storage ); if( cascade ) { double t = (double)cvGetTickCount(); CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage, k, m, 0/*CV_HAAR_DO_CANNY_PRUNING*/, cvSize(30, 30) ); t = (double)cvGetTickCount() - t; printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) ); for( i = 0; i < (faces ? faces->total : 0); i++ ) { CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); CvPoint center; int radius; center.x = cvRound((r->x + r->width*0.5)*scale); center.y = cvRound((r->y + r->height*0.5)*scale); //radius = cvRound((r->width + r->height)*0.25*scale); //cvCircle( img, center, radius, colors[i%8], 3, 8, 0 ); pt1.x = cvRound((r->x) * scale); pt1.y = cvRound((r->y) * scale); pt2.x = cvRound((r->x + r->width) * scale); pt2.y = cvRound((r->y + r->height) * scale); cvRectangle(img, pt2, pt1, color2, 3, 8); } } // cvReleaseImage( &gray ); // cvReleaseImage( &small_img ); }