访问每个视频帧并进行处理

时间:2014-01-10 05:44:33

标签: c++ opencv live-streaming

我正在尝试编写一个程序,使用OPENCV和CPP将视频文件的每个帧存储为图像。我已通过ffmpegmplayer实现了这一目标。我希望使用OPENCV来获取它。有人可以帮忙吗?是否还有其他功能可以将图像存储到IMWRITE()以外的文件中?

OPENCV中是否有任何功能可以获取设备特定端口可用的实时视频流并实时处理?即将每帧流存储为图像? 任何人都可以帮助我吗?

提前感谢.. :))

1 个答案:

答案 0 :(得分:3)

使用增加的编号文件名保存所有图像,这对我有用:

const char* videofilename = "StopMoti2001.mpeg";
cv::VideoCapture cap(videofilename); // open a video file
if(!cap.isOpened())  // check if succeeded
{
    std::cout << "file " << videofilename << " not found or could not be opened" << std::endl;
    return;
}

cv::namedWindow("output");

unsigned long counter = 0;

cv::Mat frame;
// read frames until end of video:
while(cap.read(frame))
{

    // display frame
    cv::imshow("output", frame); cv::waitKey(25);   // remove this line if you don't need the live output


    // adjust the filename by incrementing a counter
    std::stringstream filename (std::stringstream::in | std::stringstream::out);
    filename << "image" <<  counter++ << ".jpg";

    std::cout << "writing " << filename.str().c_str() << " to disk" << std::endl;

    // save frame to file: image0.jpg, image1.jpg, and so on...
    cv::imwrite(filename.str().c_str(),frame);

}