Haar检测 - 保存图像的Mat以获取并显示前一帧

时间:2016-05-11 19:08:12

标签: c++ opencv

我在我的爱好项目中使用Haar检测,检测是在视频流上完成的。一旦Haar检测到我现在所看到的内容,就会出现这种情况:Mat faceROI = frame_gray(faces[i]); imshow( "Detection", faceROI);

当视频正在运行时,我正在检测到Mat正在更新/覆盖对象的新图像。我现在要做的是保存Mat所以当新的检测发生时,我得到之前的和当前帧。我想我必须以某种方式保存Mat然后更新它,以便当前 - >之前的等等。

imshow( "Previous detection", previousROI);` <- want to be able to do this

如果您想查看整个代码,我这样做:http://docs.opencv.org/2.4/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

如果不将检测/显示拆分为单独的功能,您可能会有更轻松的时间。我已经修改了下面的OpenCV文档代码。请记住,我没有编译或运行此代码,因此可能存在一些错误,但它应该让您了解解决该问题的不同方法。

/** @function main */
int main( int argc, const char** argv )
{
    CvCapture* capture;
    Mat frame;

    //-- 1. Load the cascades
    if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
    if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };

    //-- 2. Read the video stream
    capture = cvCaptureFromCAM( -1 );
    if( capture )
    {
        //store faces here
        std::vector<Mat> prev_faces;
        std::vector<Mat> current_faces;

        while( true )
        {
            frame = cvQueryFrame( capture );

            //-- 3. Apply the classifier to the frame
            if( !frame.empty() )
            {
                std::vector<Rect> faces;
                Mat frame_gray;

                cvtColor( frame, frame_gray, CV_BGR2GRAY );
                equalizeHist( frame_gray, frame_gray );

                //-- Detect faces
                face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

                for( size_t i = 0; i < faces.size(); i++ )
                {
                    Mat faceROI = frame_gray( faces[i] );
                    current_faces.push_back(faceROI); //adds all of the current detections to a vector
                }

                if (prev_faces.size() > 0 && current_faces.size() > 0)
                {
                    //  do stuff with prev_faces and current_faces
                    //  for(int i = 0; i < prev_faces.size(); i++){
                    //      imshow("previous", prev_faces[i])
                    //  }
                    //  for(int i = 0; i < prev_faces.size(); i++){
                    //      imshow("current",  current_faces[i])
                    //  }
                    //  imshow("stuff", other_cool_Mats_I_made_by_analysing_and_comparing_prev_and_current)
                }
                prev_faces = current_faces;
                current_faces.erase(current_faces.begin(), current_faces.end());

            else
            { printf(" --(!) No captured frame -- Break!"); break; }

            int c = waitKey(10);
            if( (char)c == 'c' ) { break; }
        }
    }
    return 0;
}