视频灰度化写入(opencv)

platform:opencv 3.4.2 & vs 2017

#include<highgui.h>
#include<cv.h>
#include<stdio.h>
using namespace std;
int main() {
	/*视频播放*/
	cvNamedWindow("kris_in", CV_WINDOW_AUTOSIZE);
	CvCapture * capture = cvCreateFileCapture("ym.avi");
	int n=0;
	
	IplImage * frame;
	
	while (1) {
		frame = cvQueryFrame(capture);
		if (!frame) {
			//scanf("%d", &n);
			break;
		}
		cvShowImage("kris_in", frame);
		char c = cvWaitKey(33);
		if (c == 27)
			break;
	}
	cvReleaseImage(&frame);
	cvDestroyWindow("kris_in");
	cvReleaseCapture(&capture);
	
    CvCapture * capture1 = cvCreateFileCapture("ym.avi");
	cvNamedWindow("grayVedio", CV_WINDOW_AUTOSIZE);
	printf("now vedio to gray!!!\n");
	IplImage * bgr_frame = cvQueryFrame(capture1);

	/*获取原视频的fps与size*/
	int fps = (int)cvGetCaptureProperty(capture1, CV_CAP_PROP_FPS);
	printf("fps=%d\n", fps);
	CvSize size = cvSize(
		(int)cvGetCaptureProperty(capture1, CV_CAP_PROP_FRAME_WIDTH),
		(int)cvGetCaptureProperty(capture1, CV_CAP_PROP_FRAME_HEIGHT)
	);

	/*通过cvCreateVedioWriter设定输出视频的文件名字,压缩编码格式,fps,帧的大小*/
	CvVideoWriter *writer = cvCreateVideoWriter(
		"ym_out_gray.avi",
		CV_FOURCC('x', 'v', 'i', 'd'),
		fps,
		size
	);
	/*输出帧*/
	IplImage * logpolar_frame = cvCreateImage(size, IPL_DEPTH_8U, 1);

	while ((bgr_frame=cvQueryFrame(capture1))!=NULL)
	{   
		n++;
		/*
		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);
		
		/*写入帧图*/
		cvWriteFrame(writer, logpolar_frame);

		/*显示图像*/
		cvShowImage("grayVedio", logpolar_frame);
		char c = cvWaitKey(fps);
		if (c == 27)break;
	}
    
	cvReleaseVideoWriter(&writer);
	cvReleaseCapture(&capture1);
	cvReleaseImage(&logpolar_frame);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38448815/article/details/82725603