1. 程式人生 > >視訊灰度化寫入(opencv)

視訊灰度化寫入(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;
	}
    printf("%d\n", n);
	scanf("%d", &n);
	cvReleaseVideoWriter(&writer);
	cvReleaseCapture(&capture1);
	cvReleaseImage(&logpolar_frame);
	return 0;
}