Reputation: 162
I am trying to write a programm that captures n frames from the video and places them into the container for further work (I'm making a collage afterwards). But i've encountered an issue, when all images in the container are the same(it is filled only with last image captured). I've checked that images are captured correctly, as I'm saving them as well and can cleary see that they are different.
Here is my code:
std::vector<cv::Mat> MY_framelist; //container for captured frames
cv::VideoCapture myvid;
cv::Mat MY_frame; //will capture frames here
myvid.open(pass_filename); //open video file(char* pass_filename=12.mp4)
if (!myvid.isOpened()) {
printf("Capture not open \n");
}
double x_length = myvid.get(CV_CAP_PROP_FRAME_COUNT); //get maxlength of the video
uint each_frame = uint(x_length) / 16; //capture every 16 frames
for (uint j = 0, current_frame = 1; (current_frame < x_length) && (j < 16); current_frame += each_frame, j++)
{
myvid.set(CV_CAP_PROP_POS_FRAMES, current_frame); //set frame
myvid.read(MY_frame); // then capture the next one
MY_framelist.push_back(MY_frame); //place it into the container
std::stringstream frameNum; //generating name for saved images
frameNum << j + 1;
if (j + 1 <= 9)
my_filename += "0";
my_filename += frameNum.str();
my_filename += ".jpg";
cv::imwrite(my_filename.c_str(), MY_frame); //saving images to prove that they are captured correctly
my_filename = "test";
printf(" and Image # ");
printf("%d", j + 1);
printf(" saved \n");
}
As a result MY_framelist will contain 16 same images that were captured last. What am I doing wrong here? I've seen some workarround here , but I'm not really eager to do this as it will lead to not so accurate results. Thanks in advance!
Upvotes: 1
Views: 186
Reputation: 41765
OpenCV Mat
copy is a shallow copy, i.e. only the header is copied, not the data. So here:
MY_framelist.push_back(MY_frame); //place it into the container
you'll get a container with always the same image.
You need to do a deep copy copying the data, too:
MY_framelist.push_back(MY_frame.clone()); //place it into the container
Upvotes: 1