简体   繁体   English

C ++(和openCV):在向量中累积许多Mat <Mat>

[英]C++ (and openCV): Accumulating a number of Mat in vector<Mat>

I'm grabbing frame from webcam am displaying them on a window. 我正在从摄像头抓取帧,将其显示在窗口上。 To do a temporal analysis, I keeping the frames on a vector as I grab them. 为了进行时间分析,我在抓取帧时将其保持在矢量上。 In order to test it, when the size of my vector reaches 100, I attempt to visualize all the frames that I stored so far. 为了对其进行测试,当向量的大小达到100时,我尝试可视化到目前为止存储的所有帧。

The strange thing is that, indeed the vector has 100 frames inside, but they are all the same and correspond to the last frame that was captured. 奇怪的是,向量的内部确实有100帧,但是它们都是相同的,并且对应于捕获的最后一个帧。

Code: 码:

Mat frame;       

    _cap.start(0);  //VideoCapture object   

    vector<Mat> seq;
    while(1)
    {
        _cap.getFrame(frame);           

        seq.push_back(frame);

        imshow("a", seq[0]);

        if (seq.size() == 100)
        {
            for (int n = 0; n < seq.size(); n++)
            {
                cout << "I'm in" << endl;
                imshow("b", seq[n]);
                waitKey(0);
            }
        }

            waitKey(30);   
    }

the Mat's you get from the VideoCapture all point to the same driver memory. 从VideoCapture获得的Mat都指向相同的驱动程序内存。

Mat a=b; 垫a = b; does a shallow copy ( same pixel pointer ) 进行浅拷贝(相同的像素指针)

so you need : 所以你需要:

 seq.push_back(frame.clone());

here ( ie a deep copy ) 在这里(即深层副本)

Frame will still point to the same data field. 框架仍将指向相同的数据字段。 Hence whenever you grab a new image all previous captured images will also update. 因此,每当您获取新图像时,所有先前捕获的图像也会更新。

Solution is to release the frame after you have put it into the vector by calling frame.release() 解决方法是在将框架放入向量中后,通过调用frame.release()释放框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM