Ask Your Question
0

Previous Mat becomes zero

asked 2015-02-28 12:15:17 -0600

Tiras gravatar image

Currently, I am trying to grab a Mat and do a simple comparison. But now I am stuck at grabbing previous frame. Here is my code.

for(;;){
        cap >> frame;
        // Clone the current frame:
        Mat original = frame.clone();
        // Convert the current frame to grayscale:
        Mat gray;
        cvtColor(original, gray, CV_BGR2GRAY);
         vector< Rect_<int> > faces;
         Mat face_tmp;
         haar_cascade.detectMultiScale(gray, faces,1.2);    //scaleFactor = 1.2 for stability
         for(int i = 0; i < faces.size(); i++) {
            Rect face_i = faces[i];
            Mat face = gray(face_i);
            Mat face_resized;
            cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
            Mat Diff = face_resized - face_tmp;
            Scalar sumOfface_resized = sum(face_resized);
            Scalar sumOfface_tmp = sum(face_tmp);
            Scalar sumOfDiff = sum(Diff);
            face_tmp = face_resized.clone();

            string String_count = static_cast<ostringstream*>( &(ostringstream() << count) )->str();
            string String_face_i = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
            string filename = String_count + "_" + String_face_i;
            cout << "FileName is : " << filename << endl;
            cout << "Sum of face_resized is : " << sumOfface_resized << endl;
            cout << "Sum of face_tmp is : "     << sumOfface_tmp << endl;
            cout << "Sum of element differentiation is : " << sumOfDiff << endl;
            cout << "xxxxx"  << sum(face_tmp) << endl;
    }

Here is my output:

    FileName is : 0_0
    Sum of face_resized is : [5.3287e+06, 0, 0, 0]
    Sum of face_tmp is : [0, 0, 0, 0]
    Sum of element differentiation is : [5.3287e+06, 0, 0, 0]
    xxxxx[5.3287e+06, 0, 0, 0]
    FileName is : 1_0
    Sum of face_resized is : [5.3287e+06, 0, 0, 0]
    Sum of face_tmp is : [0, 0, 0, 0]
    Sum of element differentiation is : [5.3287e+06, 0, 0, 0]
    xxxxx[5.3287e+06, 0, 0, 0]
    FileName is : 2_0
    Sum of face_resized is : [5.3287e+06, 0, 0, 0]
    Sum of face_tmp is : [0, 0, 0, 0]
    Sum of element differentiation is : [5.3287e+06, 0, 0, 0]
    xxxxx[5.3287e+06, 0, 0, 0]
    FileName is : 3_0
    Sum of face_resized is : [5.3287e+06, 0, 0, 0]
    Sum of face_tmp is : [0, 0, 0, 0]
    Sum of element differentiation is : [5.3287e+06, 0, 0, 0]
    xxxxx[5.3287e+06, 0, 0, 0]

Why my previous Mat always zero? I had defined face_tmp outside for loop already. Where am I missing?

edit retag flag offensive close merge delete

Comments

1

You declare face_tmp, but I don't see that you set it to a value. What are you actually doing or what do you want to do? Btw this: string String_count = static_cast<ostringstream*>( &(ostringstream() << count) )->str(); look very strange, why not just std::stringstream ss; ss << count; cout << ss.str() ?

Guanta gravatar imageGuanta ( 2015-02-28 13:03:08 -0600 )edit

@Guanta : face_tmp = face_resized.clone(); I set to be a temp Mat for next time by clone method. I want to do a comparison between current frame and previous frame. Then the frame is significantly different. Program will decide what to do next with current frame. For example take a shot for being a training set. For string conversion I don't know your version before. It is good way to try. My ultimate goal is : Let a simple program gather a picture for training set.

Tiras gravatar imageTiras ( 2015-03-02 22:36:20 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-03-03 01:40:31 -0600

Tiras gravatar image

I put static in front of Mat face_tmp;

static Mat face_tmp;

And then Matrix face_tmp remains.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-02-28 12:15:17 -0600

Seen: 273 times

Last updated: Mar 03 '15