1. 程式人生 > >opencv兩視訊疊加(控制檯版)

opencv兩視訊疊加(控制檯版)

//#include "stdafx.h"  
#include <iostream>  
#include <cv.h>  
//#include <cxcore.h>  
#include <cxcore.h>  
#include <highgui.h>  
using namespace cv;
//#include <cvaux.h>
// 使用標準名稱空間  
using namespace std;  

// 初始化進度條的位置  
int g_slider_position1 = 0;  
int g_slider_position2 = 0;  

CvCapture* g_capture1 = NULL;  
CvCapture* g_capture2 = NULL;  
//Mat ss;
// 定義回撥函式用於播放進度的控制   
void onTrackbarSlide1( int pos1 )  
{  
	cvSetCaptureProperty( g_capture1, CV_CAP_PROP_POS_FRAMES, pos1 );  
}  
void onTrackbarSlide2( int pos2 )  
{  
	cvSetCaptureProperty( g_capture2, CV_CAP_PROP_POS_FRAMES, pos2 );  
}  

void cvText(IplImage* img, const char* text, int x, int y)
{
	CvFont font;

	double hscale = 1.0;
	double vscale = 1.0;
	int linewidth = 2;
	cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX |CV_FONT_ITALIC,hscale,vscale,0,linewidth);

	CvScalar textColor =cvScalar(0,255,255);
	CvPoint textPos =cvPoint(x, y);

	cvPutText(img, text, textPos, &font,textColor);
}

int main(int argc, char** argv )  
{  
	// 建立播放視窗  
	cvNamedWindow( "Video Test 1", CV_WINDOW_AUTOSIZE );  
	cvNamedWindow( "Video Test 2", CV_WINDOW_AUTOSIZE );  

	// 捕捉視訊檔案  
	argv[1]="1.avi";
	argv[2]="2.avi";
	argv[3]="3.avi";
	argv[4]="4.avi";
	g_capture1 = cvCreateFileCapture( argv[1] );  
	g_capture2 = cvCreateFileCapture( argv[2] );  

	// 讀取、顯示視訊檔案的幀數  
	int frames1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FRAME_COUNT );  
	cout << "frames1 = " << frames1 << endl;  
	// 建立進度條  
	if( frames1 != 0 )  
		cvCreateTrackbar(   
		"Position",   
		"Video Test 1",   
		&g_slider_position1,   
		frames1,   
		onTrackbarSlide1  
		);  

	int frames2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FRAME_COUNT );  
	cout << "frames2 = " << frames2 << endl;  
	if( frames2 != 0 )  
		cvCreateTrackbar(   
		"Position",   
		"Video Test 2",   
		&g_slider_position2,   
		frames2,   
		onTrackbarSlide2   
		);  

	// 讀取視訊檔案資訊  
	double fps1 = (int) cvGetCaptureProperty( g_capture1, CV_CAP_PROP_FPS );  
	double fps2 = (int) cvGetCaptureProperty( g_capture2, CV_CAP_PROP_FPS );  
	CvSize size1 = cvSize(   
		(int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_WIDTH),  
		(int)cvGetCaptureProperty(g_capture1, CV_CAP_PROP_FRAME_HEIGHT));  
	CvSize size2 = cvSize(   
		(int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_WIDTH),  
		(int)cvGetCaptureProperty(g_capture2, CV_CAP_PROP_FRAME_HEIGHT));  

	// 建立 VideoWriter   
	CvVideoWriter* wrVideo1 = cvCreateVideoWriter(argv[3], CV_FOURCC('M','J','P','G'), fps1, size1);  
	CvVideoWriter* wrVideo2 = cvCreateVideoWriter(argv[4], CV_FOURCC('M','J','P','G'), fps2, size2);  

	int frs = 0;  

	// 開始播放並儲存視訊  
	IplImage* frame1;  
	IplImage* frame2;  

	while( frs < frames1 && frs < frames2 )  
	{  

		// 獲取、顯示原始檔的幀畫面  
		frame1 = cvQueryFrame( g_capture1 );  
		Mat mtx1(frame1);
		cvText(frame1, "NEWTOUCH",10,30);
		if( !frame1 ) break;  
		cvShowImage( "Video Test 1", frame1 );  

		frame2 = cvQueryFrame( g_capture2 );
		Mat mtx2(frame2);
		if( !frame2 ) break;  
		cvShowImage( "Video Test 2", frame2 );  

		// 儲存:將當前幀寫入到目標視訊檔案  
		Mat dst;
		addWeighted( mtx1, 1, mtx2, 0.5, 0.5, dst);  
		imshow( "add", dst ); 
		//cvWriteFrame( wrVideo1, frame1 );  
		//cvWriteFrame( wrVideo2, frame2 );  

		// 若按下 ESC 鍵,則退出程式  
		char c = cvWaitKey(33);  
		if( c==27 ) break;  
	}  
	// 釋放記憶體,關閉視窗  
	cvReleaseCapture( &g_capture1 );  
	cvReleaseCapture( &g_capture2 );  
	cvReleaseVideoWriter( &wrVideo1 );  
	cvReleaseVideoWriter( &wrVideo2 );  
	cvDestroyWindow( "Video Test 1" );  
	cvDestroyWindow( "Video Test 2" );  
	cvDestroyWindow( "add" );  
	system("pause");
	return 0;  
}