简体   繁体   English

在opencv c ++中从相机抓取并保存特定的帧

[英]grab and save specific frame from camera in opencv c++

I have program in opencv with c++ I want to save specific frame from sequence of frames but just last frame is saved! 我在c ++中的opencv中有程序,我想从帧序列中保存特定的帧,但只保存最后一个帧! let me introduce better I have the following code : 让我更好地介绍一下,我有以下代码:

   Mat frame,img1,img2;
   for(int i = 0; i < 100; i++)
        {
            cout<<i<<endl;
            waitKey(1);
            video >> frame;

            if (i==10)
                {
                    img2=frame;

                }
            namedWindow("img");
            imshow("img",frame);
        }

namedWindow("pic");
imshow("pic",img2);
waitKey(30);
video.release();
waitKey(0);  

as you can see 100 frames are grabbed and I want to save tenth frame in img2 and show it out of for loop when loop is finished but the problem here is that it does not show tenth frame! 如您所见,抓取了100帧,我想将第十帧保存在img2中,并在循环结束时将其显示为for循环,但是这里的问题是它不显示第十帧! indeed it shows last frame! 确实显示了最后一帧!

img2 = frame; will perform a copy of the cv::Mat . 将执行cv::Mat的副本。 However, copying a Mat will copy only the header of the Mat , but both objects will refer to the same data. 然而,复制Mat只能复制的头Mat ,但是这两个对象将引用相同的数据。 So, if you modify the data of frame , you're also modifying img2 . 因此,如果您修改frame的数据,那么您也在修改img2

What you need is deep copy , ie you want to copy not only the header, but also the data. 您需要的是深度复制 ,即您不仅要复制标题,还要复制数据。 Doing so, modifying frame won't affect img2 . 这样做,修改frame不会影响img2

You can perform a deep copy simply with: 您可以使用以下方法执行深层复制:

img2 = frame.clone();

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

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