First time here? Check out the FAQ!

Ask Your Question
0

How do I save an image from a video stream?

asked Aug 22 '16

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;
}
Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Aug 22 '16

berak gravatar image

updated Aug 22 '16

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 
    }
Preview: (hide)

Comments

1

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

j0h gravatar imagej0h (Aug 22 '16)edit
1

aww, sorry, sloppy person here, too ;)

berak gravatar imageberak (Aug 22 '16)edit

Question Tools

1 follower

Stats

Asked: Aug 22 '16

Seen: 6,469 times

Last updated: Aug 22 '16