c++ - 用opencv获取帧视频

标签 c++ opencv video-processing

这是我从视频中获取帧的代码。我想在带有循环和条件的视频中显示 2 帧。

    int main( int argc, char** argv )
    {    

    string fileName = "E:\\Tugas Akhir\\Video Master\\city_1.avi";
    Mat image1;
    Mat image2;
    Mat frame;
    cv::Mat result;
    VideoCapture cap(fileName); 
    if(!cap.isOpened())  
        return -1;

    Mat edges;
    for(int loop=0;;loop++)
    {
        //std::cout<<loop<<endl;
        cap >> frame; // get a new frame from camera
        if(loop>0 && (loop%20)==0){             
            //std::cout<<"frame 2"<<endl;
            image2=frame;                               
            **imshow("image2",image2);**
            break;      
        }else if(loop==0){          
            image1=frame;
            **imshow("image1",image1);**
            //std::cout<<"frame 1"<<endl;

        }
        //loop++;


        if(waitKey(30) >= 0) break;
    }


 waitKey(0);
 return 0;
 }

这是结果,2 个窗口有 2 个不同的图像

enter image description here

但是当我改变 imshow("image1",image1) 方法位置时......

           if(loop>0 && (loop%20)==0){              
            //std::cout<<"frame 2"<<endl;
            image2=frame;

            **imshow("image1",image1);**
            **imshow("image2",image2);**
            break;      
        }else if(loop==0){          
            image1=frame;

            //std::cout<<"frame 1"<<endl;

        }

image1 窗口显示与 image2 相同的图片, enter image description here

我不知道为什么会显示奇怪的结果,请告诉我如何解决,谢谢

最佳答案

这样的作业:

image1 = frame;

只进行复制。 Mat 结构被复制,像素被共享

因此,在您的第二个示例中,您将用当前帧覆盖 image1。如果你想“缓存”Mats,使用:

image1 = frame.clone(); // deep copy

关于c++ - 用opencv获取帧视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23190704/

相关文章:

ffmpeg : how to determine frame rate automatically?

c++ - 如何在使用合成时使用默认构造函数

c++ - 当 auto 遇到多态和虚函数时,正确的行为是什么?

c++ - JsonCpp : Undefined reference to `Json::Value::operator[](int) const'

java - 使用 JAVA 代码从视频文件 (.mp4) 创建视频预览图像,如 YouTube 或 Whatsapp

shell - 使用 ffmpeg 提取特定帧的列表

c++ - 在 C++ 中手动排序 vector<int>

python - 来自响应请求python的图像中的标记框

image - imwrite 合并图像 : writing image after adding alpha channel to it opencv python

numpy - Tensorflow 和 OpenCV 中的双线性插值实现