Question

I am trying to write a program which stores each frame of a video file as an image using OPENCV with CPP. I have achieved that with ffmpeg and mplayer. I want to get it using OPENCV. Can anyone help? Is there any other function to store an image to a file other than IMWRITE()?

Is there any function in OPENCV to get the live streaming of video which is available at a particular port of a device and process it in real time ? i.e to store each frame of stream as an image? Can anyone help me out?

thanks in advance..:)

Was it helpful?

Solution

saving all images with increasing numbered filenames, this works for me:

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);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top