风言枫语  

 

code:

#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv\cxcore.h>
#include <stdio.h>

 
#include <fstream>
#include <string>
#include <opencv\ml.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include <iostream>
#include <string.h>
#include <ctype.h>

using namespace cv;
using namespace std;

void detectAndDraw( Mat& img,
                   CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
                   double scale);
String cascadeName =
"D:\\Opencv\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
String nestedCascadeName =
"D:\\Opencv\\opencv\\data\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml";

int main()
{


IplImage* aplace = 0;
IplImage* colorlaplace = 0;


IplImage* planes[3] = {0,0,0};
CvCapture* capture = 0;
Mat frame, frameCopy, image;
double scale = 1;
CascadeClassifier cascade, nestedCascade;
capture = cvCaptureFromCAM(0);

if(!cascade.load(cascadeName))
{
cout<<"Load 1 is false!"<<endl;
}
//if(!nestedCascade.load(nestedCascadeName))
//{
// cout<<"Load 2 is false!"<<endl;
//}

cvNamedWindow("Example",0);
for(;;)
{
IplImage* frame0 = 0;
int i;

frame0 = cvQueryFrame(capture);
frame = frame0;
if(!frame0)
{
break;
}
if( frame0->origin == IPL_ORIGIN_TL )
            frame.copyTo( frameCopy );
        else
            flip( frame, frameCopy, 0 );


        detectAndDraw( frameCopy, cascade, nestedCascade, scale );
//cvShowImage("Example",frame);
cvWaitKey(1);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Example");
return 0;
}
void detectAndDraw( Mat& img,
                   CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
                   double scale)
{
    int i = 0;
    double t = 0;
    vector<Rect> faces;
    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
    cvtColor( img, gray, CV_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );
    t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
        //|CV_HAAR_FIND_BIGGEST_OBJECT
        //|CV_HAAR_DO_ROUGH_SEARCH
        |CV_HAAR_SCALE_IMAGE
        ,
        Size(30, 30) );
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
    for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector<Rect> nestedObjects;
        Point center;
       // Scalar color = colors[i%8];
Scalar color = colors[3];
        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);
        circle( img, center, radius, color, 3, 8, 0 );
        if( nestedCascade.empty() )
            continue;
        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            //|CV_HAAR_FIND_BIGGEST_OBJECT
            //|CV_HAAR_DO_ROUGH_SEARCH
            //|CV_HAAR_DO_CANNY_PRUNING
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, color, 3, 8, 0 );
        }
    }  
    cv::imshow( "Example", img );    
}


 

 

 

posted on 2013-08-18 21:17  风言枫语  阅读(412)  评论(0编辑  收藏  举报