使用OpenCV错误的视频中的人脸检测关闭程序

时间:2014-12-11 14:57:15

标签: c++ opencv

我使用opencv

在c ++中使用面部检测项目

当程序确定面部时 退出计划

为什么?

并且视频显示了问题:

http://youtu.be/xYS7udYKiSI

图片:

http://i.stack.imgur.com/VdzDX.png

代码:

#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;


int main(int argc, const char *argv[]){
     if (argc != 4) {
        cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
        cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
        cout << "\t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl;
        cout << "\t <device id> -- The webcam device id to grab frames from." << endl;
      //  exit(1);
    }
    CascadeClassifier face_cascade;
    String fn="C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml";
    face_cascade.load(fn);


    VideoCapture input(0);
    if(!input.isOpened()){return -1;}

    namedWindow("Mezo",1);
    Mat f2;
    Mat frame;

    while(true){

        input>>frame;
        waitKey(10);
        cvtColor(frame, f2, CV_BGR2GRAY);

        equalizeHist(f2, f2);


        std::vector<Rect> faces;
        face_cascade.detectMultiScale(f2, faces, 1.1, 10, CV_HAAR_SCALE_IMAGE | CV_HAAR_DO_CANNY_PRUNING,Size(150,0),Size(300,300));

        //draw a rectangle for all found faces in the vector array on the original image
        for(int i = 0; i < faces.size(); i++)
        {
            Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
            Point pt2(faces[i].x, faces[i].y);

            rectangle(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 2, 8, 0);
        }


        imshow("Mezo",frame);

    waitKey(3);
    char c=waitKey(3);
    if(c==27){break;}

    }

    return 0;
}

有什么想法吗?

全部谢谢

0 个答案:

没有答案