Previous Mat becomes zero
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?
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 juststd::stringstream ss; ss << count; cout << ss.str()
?@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.