我在 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++;
}
我对垫子的理解表明这可能是指针的问题;我只是不确定如何解决它。当我查看垫子阵列时,每个元素都是相同的框架。谢谢。
最佳答案
如果您要使用 C++ 非常有用的 STL,则无需所有这些复杂的操作。
if( currentFrameNumber >= numberFrames )
matArray.remove( matArray.begin() );
matArray.push_back( finalOutputImage.clone() ); //check out @berak's comment
应该这样做。
关于c++ - 来自视频文件的垫子数组 - opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26412268/