Q:

Storing images in mat vector

I am using opencv face detector to locate faces inside an image. I am trying to store the multiple different faces in an vector. It seems that I store the same multiple times. 
Here is my code:
    face_cascade.detectMultiScale( gray_image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE );
    Rect faceRect, eyeRect, temp;
    //draw a rectangle for all found faces in the vector array on the original
    image
    cout<< faces.size()<<endl;
    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(image, pt1, pt2, cvScalar(0, 255, 0, 0), 3, 8, 0);
        //Create the cropped image
        if(!faces.empty()){
            faceRect = cvRect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
            croppedFaceImage = gray_image(faceRect).clone();

      resize(croppedFaceImage,resizedcroppedFaceImage,Size(60,60),0,0,INTER_LINEAR);

            char k;
            imshow("new", resizedcroppedFaceImage);
            k = waitKey(0);
            if (k== 'ESC')  {destroyAllWindows();}
            diFaces.push_back(resizedcroppedFaceImage);
        }
        else if(faces.empty()) 
       {resize(gray_image,resizedcroppedFaceImage,Size(60,60),0,0,INTER_LINEAR); break;}
       }

When I imshow resizedcroppedFaceImage everything is ok, but when I am trying to imshow diFaces it shows me the same image. Is there any explanation about push_back and storing the same image?? 

A:

since your definition of resizedcroppedFaceImage is outside the loop, you indeed reuse the same Mat structure(including the data pointer) for all your images, so they're all the same (pushing them into a vector results a shallow copy only).
so, either push a clone():
diFaces.push_back( resizedcroppedFaceImage.clone() );

or make resizedcroppedFaceImage a local var inside the loop, so it's a fresh Mat for every pass.

