#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// Convert a video to grayscale
// argv[1]: input video file
// argv[2]: name of new output file
//
IplImage* doresize(IplImage* in){
IplImage* out1 = cvCreateImage(
cvSize( in->width/2, in->height/2 ),
in->depth,
in->nChannels
);
return( out1 );
};
IplImage* doPyrDown(
IplImage* in,
int filter = IPL_GAUSSIAN_5x5)
{
IplImage* out = cvCreateImage(
cvSize( in->width/2, in->height/2 ),
in->depth,
in->nChannels
);
cvPyrDown( in, out );
return( out );
};
int main( int argc, char* argv[] ) {
cvNamedWindow( "Example2_10", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Log_Polar", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( "D:\\1.wmv" );
if (!capture){
return -1;
}
IplImage* bgr_frame;IplImage* img_pyr;
double fps = cvGetCaptureProperty (
capture,
CV_CAP_PROP_FPS
);
printf("fps=%d\n",(int)fps);
CvSize size = cvSize(
(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
);
printf("frame (w, h) = (%d, %d)\n",size.width,size.height);
CvVideoWriter* writer = cvCreateVideoWriter( // On linux Will only work if you've installed ffmpeg development files correctly,
"D:\\33.avi", // otherwise segmentation fault. Windows probably better.
CV_FOURCC('D','X','5','0'),
fps,
size
);
IplImage* logpolar_frame = cvCreateImage(
size,
IPL_DEPTH_8U,
3
);
IplImage* gray_frame = cvCreateImage(
size,
IPL_DEPTH_8U,
1
);
while( (bgr_frame=cvQueryFrame(capture)) != NULL ) {
cvShowImage( "Example2_10", bgr_frame );
//cvLogPolar( bgr_frame, logpolar_frame, //This is just a fun conversion the mimic's the human visual system
// cvPoint2D32f(bgr_frame->width/2,
// bgr_frame->height/2),
// 40,
// CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS ); //此语句正确
cvCvtColor(bgr_frame, gray_frame,CV_BGR2GRAY); //将色彩转换为灰度,必须与上面的IplImage* gray_frame搭配使用才行
//用上语句转换灰度,则需要将gray_frame通道初始化为1,若为3则发生错误,下句可将gray_frame通道初始化为1为3,因为未指定转换格式CV_BGR2GRAY
//cvConvertImage(bgr_frame, gray_frame,CV_CVTIMG_FLIP);//图像180度旋转
/****以上两句程序都是讲图像转换为灰度****/
img_pyr=doresize(gray_frame); cvResize(gray_frame,img_pyr);
//使用cvResize需要先将目标图像初始化好为目标图像的一般即doresize所做的,然后才好将原图像转换为目标图像的一般
//img_pyr =doPyrDown(gray_frame,IPL_GAUSSIAN_5x5); //gray_frame =doPyrDown(gray_frame,IPL_GAUSSIAN_5x5);这里必须重新定义指针,直接用这语句不对
/****以上两句程序都是讲图像缩放为原来的一般****/
cvShowImage( "Log_Polar", img_pyr );
//Sigh, on linux, depending on your ffmpeg, this often won't work ...
cvWriteToAVI( writer, img_pyr );
char c = cvWaitKey(10);
if( c == 27 ) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseImage( &img_pyr );
cvReleaseCapture( &capture );
}