Ask Your Question
0

How do I save an image from a video stream?

asked 2016-08-22 11:36:59 -0600

j0h gravatar image

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;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-08-22 11:46:26 -0600

berak gravatar image

updated 2016-08-22 12:08:28 -0600

you just need to change the name, so it does not overwrite the previous with the current frame.

   int counter = 0; // NEW !
   for(;;){ //forever
          Mat frame;
          cap >> frame;
          if( frame.empty() ) break; // end of video stream
          imshow("this is you, smile! :)", frame);
          String name = format("img%04d.png", counter++); // NEW !
          imwrite(name, frame); 
          // btw, you sloppily 
          // a: swapped img & name above,
          // b: also, c++ does use "" for strings, your original code would *never* compile ...
          if( waitKey(1) == 27 ) break; // stop capturing by pressing ESC 
    }
edit flag offensive delete link more

Comments

1

can you put ";" at the end of the increment line? (I lack reputation) Also, that works!

j0h gravatar imagej0h ( 2016-08-22 11:55:02 -0600 )edit
1

aww, sorry, sloppy person here, too ;)

berak gravatar imageberak ( 2016-08-22 12:08:06 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-22 11:36:59 -0600

Seen: 6,010 times

Last updated: Aug 22 '16