zoukankan      html  css  js  c++  java
  • opencv 通过摄像头捕捉头部

    code:

    #include <opencvcv.h>
    #include <opencvhighgui.h>
    #include <opencvcxcore.h>
    #include <stdio.h>
    
     
    #include <fstream>
    #include <string>
    #include <opencvml.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
    ", 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 );    
    }


     

  • 相关阅读:
    spyder学习记录---如何调试
    Pycharm学习记录---同一目录下无法import明明已经存在的.py文件
    python库之matplotlib学习---图无法显示中文
    python之字典遍历方法
    python库之matplotlib学习---关于坐标轴
    将博客搬至CSDN
    C代码实现栈
    Android进程间通讯
    Android进程间通讯之messenger
    C代码实现非循环单链表
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3266474.html
Copyright © 2011-2022 走看看