9/02/2013

OpenCV Video Writer example source code (VideoWriter function)

This is video writer example source code.
This source code was used from video load & display.

...
int main()
{
    VideoCapture capture("rhinos.avi");
    Mat frame;

    //Set properties
    int askFileTypeBox = 0; //-1 is show box of codec
    int Color = 1;
    Size S = Size((int)capture.get(CV_CAP_PROP_FRAME_WIDTH), (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT));

    //make output video file
    VideoWriter outVideo;
    //save video file by origin paramers
    outVideo.open(".\\outVideo.avi", askFileTypeBox, capture.get(CV_CAP_PROP_FPS), S, Color);

    double fps = capture.get(CV_CAP_PROP_FPS);
    //check
    if (!capture.isOpened())
    {
        printf("AVI file can not open.\n");
        return 0;
    }

    //create window
    namedWindow("w");

    while (1)
    {
        //grab frame from file & throw to Mat
        capture >> frame;
        if (frame.empty()) //Is video end?
            break;

        //processing example
        //Sobel(frame, frame, frame.depth(), 1, 0);

        ////////////////////
        //outVideo.write(frame);
        outVideo << frame;
        //display and delay
        imshow("w", frame);

        //delay by origin fps
        if (waitKey((1 / fps) * 1000) > 0)
            break;

    }

    return 0;
}
...

OpenCV, Video file load and display example source code (using VideoCapture function)

This is example source code to load video file and display

...
//file load
 VideoCapture capture(".\\video.avi");
 Mat frame;

 //check
 if( !capture.isOpened() )
 {
  printf("AVI file can not open.\n");
  return;
 }
 
 //create window
 namedWindow("w");
 
 while(1)
 {
  //grab frame from file & throw to Mat
  capture >> frame;
  if(frame.empty() ) //Is video end?
   break;
  
  //processing example
  Sobel(frame,frame,frame.depth(),1,0);
  ////////////////////

  //display and delay
  imshow("w", frame);
  if(waitKey(10) > 0)
   break;
 }
...

Thank you.