2013년 11월 22일 금요일

reading and writing

OpenCV is a useful library comprised of many functions for computer vision and other related image processing fields. Here, I present a sample using the video processing capabilities of the latest OpenCV 2.2 library.
#include "opencv2/opencv.hpp"

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

 cvNamedWindow( "Canny", CV_WINDOW_AUTOSIZE );

 // GET video
 CvCapture* capture = cvCreateFileCapture( "sample.avi" );
 if (!capture ) {
  printf( "Unable to read input video." );
  return 0;
 }

  double fps = cvGetCaptureProperty(
  capture,
  CV_CAP_PROP_FPS
  );

 // Read frame
 IplImage* frame = cvQueryFrame( capture );
 
 // INIT the video writer
 CvVideoWriter *writer = cvCreateVideoWriter(
  "out.avi",
  CV_FOURCC('M', 'J', 'P', 'G'),
  fps,
  cvGetSize(frame)
  );

 // DECLARE greyscale image to hold the result
 IplImage* edges  = cvCreateImage(
  cvGetSize(frame)
  , IPL_DEPTH_8U
  , 1
  );

 while(1) {

  // PERFORM canny edge detection
  cvCvtColor( frame, edges, CV_BGR2GRAY );        
        cvCanny( edges, edges, 1.0, 1.0, 3 );

  // WRITE the result
  cvCvtColor( edges, frame, CV_GRAY2BGR );
  cvWriteFrame( writer, frame );

  cvShowImage( "Canny", frame );

  // READ next frame
  frame = cvQueryFrame( capture );
  if( !frame ) break;

  char c = cvWaitKey(33);
  if( c == 27 ) break;
 }

 // CLEAN everything
 cvReleaseImage( &edges );
 cvReleaseImage( &frame );
 cvReleaseCapture( &capture );
 cvReleaseVideoWriter( &writer );

 cvDestroyWindow( "Canny" );
 return 0;
}

댓글 없음:

댓글 쓰기