开发者

Save IplImage (OpenCV) on a vector in C++

I try to save some frames of webcam in OpenCV:

CvCapture* capture = cvCaptureFromCAM(0);
IplImage *img = new IplImage(); 
vector<IplImage *> imgs;

while (true){
    cvGrabFrame(capture));
    img=cvRetrieveFrame(capture);
    imgs.push_back(img);
}

And then read from vector:

cvNamedWindow("Caption",CV_WINDOW_AUTOSIZE);

for(int i=0 ; i < imgs.size() ; i++){
  开发者_如何学Go  IplImage *img_col = new IplImage();
    img_col=imgs[i];
    cvShowImage("Caption" , img_col);
}

But echo this error:

OpenCV Error: Null pointer (The image has NULL data pointer) in cvGetMat


I think you need to learn how memory works in c++

In the first bit of code you allocate only a single image pointer, but then retrieve all your images into and save copies of the same value.

In the second part you allocate a new image for each pointer and then overwrite this memory with the value in the array

You should also read about how openCV manages memory, it will do some of this for you automatically.


Just change:

vector<IplImage> imgs;
...
img=cvRetrieveFrame(capture);
textmgs.push_back(img); // A copy of the full matrix will be executed

But as Martin said you should learn C++ before learning OpenCV, or better, learn python and forget about C++ :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜