0
votes

I'm programming in C++ using OpenCV in an object oriented approach. Basically I have an array of object called People[8]. For each array, I want to allocate an image to it by taking a picture using webcam. I did something like this:

for (int i=0; i<8; i++){
    cvWaitKey(0);                         //wait for input then take picture
    Mat grabbed = cam1.CamCapture();
    People[i].setImage(grabbed);
    imshow("picture", grabbed);
    cvWaitKey(1);
}

I face 2 problems here: 1) The imshow does not display the 'latest' image captured, it display the image previously taken i.e (i-1) instead of i. 2) When I display all the images together, 8 windows appear and all of them are displaying the last image captured on the camera.

I do not have any clue what is wrong, could anyone please advice? Thank you in advance.

2

2 Answers

0
votes

"all of them are displaying the last image captured on the camera."

the images you get from the capture point to driver memory. so the former image gets overwritten by the latter.

you need to store a clone() of the mat you get, like:

People[i].setImage( grabbed.clone() );
0
votes

I have not worked with OpenCV for a while but I would move around cvWaitKey( 1 ), I also would not have 2 calls to it, from what I remember it is similar to glFlush(). Also I would change 1 to 10. For some reason I remember 1 not working.