学习OpenCV3----读写视频

在OpenCV中,提供了视频读写类VideoCapture,该类可以方便的连接计算机的摄像头,并将摄像头的视频按照帧写入单幅图像,以便进行图像处理。下面的例子中,OpenCV打开摄像头,并将图像输出到Frame中。

int main( int argc, char** argv ) {

   

    if (argc != 2) {

        help(argv);

        return 0;

    }

  cv::namedWindow( "Example 2-3",cv::WINDOW_AUTOSIZE );

 

  cv::VideoCapture cap;

 

  cap.open( string(argv[1]) );

      cout <<"Opened file: "<<argv[1] <<endl;

 

  cv::Mat frame;

 

  for(;;) {

 

    cap >> frame;

 

    if( frame.empty() ) break; // Ran out of film

 

    cv::imshow( "Example2-3", frame );

 

    if( (char)cv::waitKey(33) >= 0 ) break;

 

  }

  return 0;

 

}

对读入的视频可以经过一定的处理再合成为视频写入硬盘进行保存,对视频的写入借助于OpenCV的视频写类VideoWriter,以下的示例展示了读取视频并输出帧,经过图像处理之再合成为视频。

int main( int argc, char** argv ) {

   

cv::namedWindow( "Example2-11", cv::WINDOW_AUTOSIZE );

  cv::namedWindow( "Log_Polar",cv::WINDOW_AUTOSIZE );

 

  // ( Note: could capture from a camera by giving a cameraid as an int.)

  cv::VideoCapturecapture( argv[1] );

  double fps = capture.get(CV_CAP_PROP_FPS );

  cv::Size size(

    (int)capture.get(CV_CAP_PROP_FRAME_WIDTH ),

    (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT )

  );

  cv::VideoWriterwriter;

  writer.open(argv[2], CV_FOURCC('M','J','P','G'), fps, size );

  cv::Matlogpolar_frame, bgr_frame;

 

  for(;;) {

    capture>> bgr_frame;

    if( bgr_frame.empty() ) break; // end if done

    cv::imshow( "Example 2-11",bgr_frame );

    cv::logPolar(

      bgr_frame, // Input color frame

     logpolar_frame, // Output log-polar frame

      cv::Point2f( // Centerpoint for log-polar transformation

       bgr_frame.cols/2, // x

       bgr_frame.rows/2 // y

      ),

      40, // Magnitude (scale parameter)

     CV_WARP_FILL_OUTLIERS // Fill outliers with 'zero'

    );

    cv::imshow( "Log_Polar",logpolar_frame );

    writer <<logpolar_frame;

   char c = cv::waitKey(10);

    if( c == 27 ) break; // allow the user to break out

  }

  writer.release();

 capture.release();

}

 


openCV源码下载

没有更多推荐了,返回首页