I’m not much of a C++ programmer, being more used to C# and Java where one doesn’t need to worry about pointers. I thought I understood what I was doing here, but the results aren’t what I was expecting and I’m not sure is it that I’m doing something silly here, or if somewhere else in the programme is causing problems.
Anyway, I’m working with OpenCV, and I have two images, which I initialise like this:
IplImage *current_frame = NULL;
IplImage *previous_frame = NULL;
Then I have this block of code:
if (current_frame != NULL)
{
previous_frame = new IplImage(*current_frame);
current_frame = cvQueryFrame( capture );
}
else
{
current_frame = cvQueryFrame( capture );
previous_frame = cvQueryFrame( capture );
}
The idea being that the first time the code executes, both current and previous frames will use the freshly captured image, but that for subsequent frames previous_frame will take on the previous value of current_frame, and current_frame capture a fresh image (I’ve stepped through the code and it is going into the correct branches of the if statement).
What’s actually happening is that I’m outputting both frames and they are identical, rather than previous_frame lagging by one as I want it to.
Am I misusing the pointers? If so, how should I go about getting the behaviour I want? Or does this look like it should do what I want it to?
Thanks.
I suspect you are using the raw C API: here
IplImageis defined as a POD structAs you can see,
new IplImage(*current_frame)copies only pointers (especially,imageData), not the actual data. So you are false-sharing the data in both images.I suggest you read here for the C++ wrappers, and especially how to use them for memory management: http://opencv.willowgarage.com/documentation/cpp/memory_management.html
EDIT If you want to use the C API: