Solved Issue with storing Mat objects in QList
-
Hello All,
I have created QList of Mat (Memory Allocator for Image in Opencv) object and i am storing the camera frame in it.
My problem is that after storing the camera frames when i am trying to save the content of list.
All the images are replaced by the last image.
Please find the code below for more clarification:Code:
QList<Mat> images; //Declaration of list of mat object VideoCapture camera(0); //Appending the image to list for(int m = 0; m < 12; m++){ // Move the controller to the desired location camera >> controllerFrame; // Capture the frame images.append(controllerFrame); // Appending the image frames qDebug()<<"Size of the image List:"<<images.size(); //Saving the current frame which have been captured String saveImage; saveImage = "C:\\Users\\demo\\ImageAppendedToList"+to_string(m)+".jpeg"; qDebug()<<saveImage.c_str(); imwrite(saveImage.c_str(), controllerFrame); } // Display the image frames capture in the list for (int xy = 0 ; xy<images.size(); xy++) { String saveImageList; saveImageList="C:\\Users\\demo\\ImagesFrom the list"+to_string(xy)+".jpeg"; qDebug()<<saveImageList.c_str(); imwrite(saveImageList.c_str(), (images.at(xy))); }
When i am trying to display the image stored in the list. I always get the last frame.
Could not identify the probable reason of the issue. -
replace
images.append(controllerFrame);
withimages.append(controllerFrame.clone());
The reason is that the copy constructor of
cv::Mat
does not perform any copy at all, all the images in the list point to the same data that you overwrite each time -
Hi,
You are appending always the same object to the list so you likely have 12 copies of it that share their underlying data.
As an experiment, move the instantiation of
controllerFrame
in yourfor
loop. -
@VRonin : Thanks for the help it works. You always highlight this thing :)
-
@SGaist : Nope i am taking snaps of different location which i have mentioned in the comment.
-
I wasn't referring to what you were taking pictures of but the objects that contain it. Basically the same thing that @VRonin described.
-
@SGaist : Ohh k. Thanks for your help. :)