0

Hi guys i am trying to add an IplImage into a vector every time i extract a frame from the camera, but somehow the IplImage stored in my vector always got over-written by the latest frame.

The variable that i want to store is image.get() with image defined as

boost::scoped_ptr<IplImage> image(cvCreateImage(size, IPL_DEPTH_32F, 1));

i have created a vector to store my image as followed

cv::vector< const IplImage*> buffer;

i use the function push_back to add in the image into buffer

buffer.push_back(image.get());

after that i try to save the image as png format for each variable in the buffer and i discover that the images are the same.

i think this should be a pointer issue as i have no problem trying to save integers into another vector.

any suggestion how i can store different images into a vector? thanks in advance

megabyte1024
  • 7,840
  • 4
  • 25
  • 41
lkleung1
  • 57
  • 7

1 Answers1

0

Wait a second. Do you know what scoped_ptr<> does?

IplImage const* store_this_somewhere; // e.g. in your vector
{
    boost::scoped_ptr<IplImage> image(cvCreateImage(size, IPL_DEPTH_32F, 1));

     store_this_somewhere = image.get();

 } // HERE `image` is destructed and **deletes** the image

 // store_this_somewhere is **dangling** here and dereferencing it is 
 // UB

I think you're having Undefined Behaviour because the image gets deleted. Next time you grab a screenshot, the memory allocated might happen to be in the same spot which causes the "old", dangling, pointers to seem to point to the latest screen grab.

Either don't use pointers, or use smart pointers:

cv::vector< boost::scoped_ptr<const IplImage*> > buffer;

See also:

Very general:

Community
  • 1
  • 1
sehe
  • 328,274
  • 43
  • 416
  • 565