C.J Blue
C.J Blue

Reputation: 21

Haar detection - save Mat of image in order to get and show previous frame

I am using Haar detection in my hobby project, the detection is done on a video stream. Once Haar detects something I imshow it, this is how it looks like: Mat faceROI = frame_gray(faces[i]); imshow( "Detection", faceROI);

While the video is running I am getting detections and the Mat is getting updated/overwritten with a new image of the object. What I want to do now is to save Mat so when a new detections occure I get both the previous and current frame. I'll guess I have to save the Mat in some way and then update it so current -> previous and so on.

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

In case you want to see the whole code, I am doing this: http://docs.opencv.org/2.4/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

Help is much appreciated!

Upvotes: 2

Views: 182

Answers (1)

BHawk
BHawk

Reputation: 2462

You might have and easier time if you don't split the detect/display into a separate function. I've modified the OpenCV documentation code below. Keep in mind I haven't compiled or run this so there may be some errors, but it should give you an idea of a different way to address the issue.

/** @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;
}

Upvotes: 1

Related Questions