Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How do I save an image from a video stream?

Im brand new to digital image processing, and opencv, and Ive been struggling, to save an image, from a video stream. I am trying to use imwrite, to write a new image, from a frame. but it doesn't work that way. (In python, (not that this is a python question) I had to create a blank image, then use imwrite, to save the frame to the blank with imwrite) It felt pretty hackerish, Im looking for the best way to save an image from a video stream.

imwrite() doesn't apparently create a new image if one doesn't already exist. (Or I don't know how to make it do it)

What is the best practice for saving an image from a video stream?

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;
int main(int argc, char** argv){
    VideoCapture cap;
     // Create mat with alpha channel
    Mat mat(480, 640, CV_8UC4);       

    for(;;){ //forever
          Mat frame;
          cap >> frame;
          if( frame.empty() ) break; // end of video stream
          imshow("this is you, smile! :)", frame);
          imwrite(frame, 'n.png' );
          if( waitKey(1) == 27 ) break; // stop capturing by pressing ESC 
    }
    // the camera will be closed automatically upon exit
    // cap.close();
    return 0;
}