I'm using OpenCV to capture images from a camera frame by frame. When I get a new image frame(IplImage), I want to push it into a vector, then delete the beginning element of this vector.
The reason I do this is: I need to store and keep the newest 30 frames from the camera.
I did it like this:
1 2 3 4 5 6 7 8 9 10 11 12
IplImage Image_cap;
vector<IplImage> vec_image;
while(1)
{
///////////////////
//here is the code which will read in one frame from camera and store it in Image_cap
///////////////////
//push the current frame into the image and delete the beginning element
vec_image.push_back(Image_cap);
vec_image.erase(vec_image.begin());
}
But when I run it, I cannot get the right result.
After some experiments, I found out that the color data in IplImage is stored in the memory with a pointer named "imageData". So when I get a new frame from the camera, Image_cap->imageData will be replaced by the new color data. And when I do push_back(), only the pointer "imageData" will be pushed into the vector, but not the data.
So is there any solution to meet my requirement(to store and keep the newest 30 frames from the camera)?
It appears that IplImage class does not have an appropriate copy constructor that does a deep copy. You need to either write a copy-constructor, or if you already have written one, review it.
And prefer using a std::deque<> instead of a std::vector<> for this.
Thanks again for you suggestion~ IplImage indeed does not have a deep copy method.
I've decided to write a list class by my self to solve this problem~
Just a quick look at the OpenCV documentation, it looks like they're treating IplImage* as a handle to a resource, so you probably should also be using pointers and treating them the same way. Perhaps cvCloneImage for a 'deep' copy, if you find that's necessary.