关闭

OpenCV将彩色视频转换为灰度视频并保存

709人阅读 评论(0) 收藏 举报
分类:
#include "cxcore.h"
#include "cvcam.h"
#include "windows.h"
#include "cv.h"
#include "highgui.h"
#include "stdio.h"

#pragma comment(lib, "cv.lib")
#pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "highgui.lib")

 
int main()
{
	CvCapture* capture = NULL;
	IplImage* bgr_frame = NULL;

	capture = cvCreateFileCapture( "tree.avi" );//函数给指定文件中的视频流分配和初始化CvCapture结构。
	if ( !capture )
	{
		return -1;
	}

//	cvNamedWindow("111",CV_WINDOW_AUTOSIZE);
//	cvNamedWindow("222",CV_WINDOW_AUTOSIZE);

	bgr_frame = cvQueryFrame(capture);// Init the video read 从摄像头或者文件中抓取并返回一帧 
	double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
	CvSize size = cvSize( ( int ) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH ),
	                      ( int ) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT ) );
/*
		用cvCreateVideoWriter创建写AVI文件的容器时,当它的第二个参数是-1时,弹出对话框,
	    选择压缩编码方式,然后写avi视频正确,并且能打开。
	
		CV_FOURCC('P','I','M','1') = MPEG-1 codec 
		CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
		CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
	  CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
	  CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec  失败
    	CV_FOURCC('X', 'V', 'I', 'D') = MPEG-4 codec  成功
	  CV_FOURCC('U', '2', '6', '3') = H263 codec
	  CV_FOURCC('I', '2', '6', '3') = H263I codec
	  CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec	
*/

	CvVideoWriter *writer = cvCreateVideoWriter( "myvide.avi",	CV_FOURCC('X', 'V', 'I', 'D'), fps, size);
	IplImage* logpolar_frame = cvCreateImage( size, IPL_DEPTH_8U, 1);

	while( (bgr_frame = cvQueryFrame( capture )) != NULL )
	{
	//	cvShowImage("111", bgr_frame );
	    
	//  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_BGR2GRAY );
		logpolar_frame->origin=1;//视频与原视频方向一致
		cvWriteFrame( writer, logpolar_frame );

	//	cvShowImage("222", logpolar_frame);

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

	//销毁窗口
//	cvDestroyWindow("111");
//  cvDestroyWindow("222");
//	cvDestroyAllWindows();

    //释放图像
	cvReleaseVideoWriter( &writer );
	cvReleaseImage( &logpolar_frame );
	cvReleaseCapture(&capture);

	return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:57251次
    • 积分:1214
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:40篇
    • 译文:0篇
    • 评论:0条