利用OpenCV读入一个彩色视频文件并以灰度格式输出

#include "highgui.h" #include "cv.h" /* * 读入一个彩色视频文件并以灰度格式输出这个视频文件 * "学习OpenCV", P32, 例2-10 * 2010-05-16 */ int main( int argc, char** argv ) { CvCapture* capture = 0; capture = cvCreateFileCapture( argv[1] ); if( !capture ) return -1; IplImage* bgr_frame = cvQueryFrame( capture );//Init the video read double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );//获取fps CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) ); CvVideoWriter *writer = cvCreateVideoWriter( argv[2], CV_FOURCC('X','V','I','D'), fps, size ); IplImage* logpolar_frame = cvCreateImage( size, IPL_DEPTH_8U, 1   ); while( (bgr_frame = cvQueryFrame(capture)) != NULL ) { // cvLogPolar( bgr_frame, // logpolar_frame, // cvPoint2D32f(bgr_frame->width/2, bgr_frame->height/2), // 40, // CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS // ); cvCvtColor( bgr_frame, logpolar_frame, CV_RGB2GRAY );//彩色转换为灰度图像 cvFlip( logpolar_frame, NULL, 0 );//翻转图像 cvWriteFrame( writer, logpolar_frame ); } cvReleaseVideoWriter( &writer ); cvReleaseImage( &logpolar_frame ); cvReleaseCapture( &capture ); return(0); }

 

代码出处:《学习OpenCV(中文版)》, Gary Bradski & Adrian Kaehler著,于仕琪、刘瑞祯译

P32, 例2-10

 

修改:

1. cvCvtColor( bgr_frame, logpolar_frame, CV_RGB2GRAY );//彩色转换为灰度图像
    cvFlip( logpolar_frame, NULL, 0 );//翻转图像 

将彩色转换为灰度图像后,图像会翻转,不知为什么。所以用cvFlip再翻转一次图像。

2. 将MJPG编码格式改为XVID格式。

3.  将新图像logpolar_frame的位深度改为1.
 

你可能感兴趣的:(video,null,include,2010)