#include<opencv2\opencv.hpp>
#include<cv.h>
#include<highgui.h>
using namespace cv;
using namespace std;
int main() {
CvCapture * avi = cvCreateFileCapture("D:\\视频\\1.avi");
if (!avi)
return 0;
IplImage *img = cvQueryFrame(avi);
double fps = cvGetCaptureProperty(avi, CV_CAP_PROP_FPS);
CvSize size = cvSize((int)cvGetCaptureProperty(avi, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(avi, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter * writer = cvCreateVideoWriter("2.avi", CV_FOURCC('M','J','P','G'), fps, size); //创建视频文件读入器
IplImage *tem_img = cvCreateImage(size, IPL_DEPTH_8U, 1);
while ((img = cvQueryFrame(avi)) != NULL) {
cvCvtColor(img, tem_img, CV_RGB2GRAY); //彩色转灰度
cvWriteFrame(writer, tem_img);
cvNamedWindow("data");
cvShowImage("data", tem_img);
cvWaitKey(24);
}
cvReleaseVideoWriter(&writer);
cvReleaseImage(&img);
cvReleaseImage(&tem_img);
cvReleaseCapture(&avi);
return 0;
}
运行结果: