来自视频文件的数组 - opencv

时间:2014-10-16 19:23:06

标签: c++ arrays opencv mat

我在Linux上使用C ++和opencv进行编码。我发现了this类似的问题;虽然,我无法让它发挥作用。

我想要做的是在视频文件中读取并在阵列中存储一定数量的帧。超过这个数字,我想删除第一帧,并将最新的帧添加到数组的末尾。

到目前为止,这是我的代码。

VideoCapture cap("Video.mp4");
int width = 2;
int height = 2;
Rect roi = Rect(100, 100, width, height);

 vector<Mat> matArray;
int numberFrames = 6;
int currentFrameNumber = 0;

for (;;){

    cap >> cameraInput;
    cameraInput(roi).copyTo(finalOutputImage);

    if(currentFrameNumber < numberFrames){
        matArray.push_back(finalOutputImage);
    }else if(currentFrameNumber <= numberFrames){
        for(int i=0;i<matArray.size()-1; i++){
            swap(matArray[i], matArray[i+1]);
        }
        matArray.pop_back();
        matArray.push_back(finalOutputImage);
    }

    currentFrameNumber++;
 }

我对垫子的理解说这可能是指针的问题;我只是不确定如何解决它。当我看到垫子阵列时,每个元素都是相同的帧。谢谢。

1 个答案:

答案 0 :(得分:3)

如果您要使用C ++非常有用的STL,则不需要所有这些复杂功能。

if( currentFrameNumber >= numberFrames )
    matArray.remove( matArray.begin() );
matArray.push_back( finalOutputImage.clone() );     //check out @berak's comment

应该这样做。