⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 video.cpp

📁 基于粒子滤波原理
💻 CPP
字号:

#include "video_header.h"


int main( int argc, char** argv )
{
	IplImage* pFrame = NULL; 
	IplImage* pGrey = NULL;
	CvCapture* pCapture = NULL;
	IplImage* dstimage=NULL;

	//直方图参数定义
	IplImage* histimage=NULL;
	CvHistogram* dsthist=NULL;
	CvHistogram* prediction_hist = NULL;
	CvHistogram* particlehist=NULL;
	int histnum = 50;
	float hranges_arr[] = {1,255};
	float* histranges = hranges_arr;

	cvNamedWindow("video", 1);
	cvNamedWindow("grey",1);
	cvNamedWindow("histogram", 1);


	//粒子滤波的参数定义
	float exptect_dist = 50;
	// How many condensentation steps per "tracking" step
	int steps=1;
	int samplenum = 64;
	// setup our variance on measurement noise global so we can get to them in  callbacks
	int measurement_noise = 5;
	// we want a 2 dim model, we track x,y   (not phi, delta_phi)
	int Dim=2;
	int MDim=histnum;
	//CvMat* measurement = cvCreateMat(MDim, 1, CV_32FC1);

	//CvMat* state = NULL;
	CvConDensation *ConDens = cvCreateConDensation(Dim, MDim, samplenum);
	CvMat* LB = cvCreateMat(Dim,1,CV_32FC1);
	CvMat* UB = cvCreateMat(Dim,1,CV_32FC1);
	CvPoint state_prediction = cvPoint(153, 180);
	
	//CvMat* noise = cvCreateMat( samplenum, 1, CV_32FC1 );
	CvRandState rng;
	cvRandInit(&rng, 0, 1, -1, 0);
	//cvRandSetRange(rng, 0, 5, 0);
	//rng->disttype = CV_RAND_NORMAL;
	//cvRand(rng, noise);

	//目标区域大小
	int a=16,b=8;

	
	
	//视频读取
	int nFrmNum = 0;
	if(!(pCapture = cvCaptureFromFile("shortvideo.avi")))
	{
		std::cout<<"cannot open video:"<<argv[1]<<"\n";
		return -2;
	}
	
	while(pFrame = cvQueryFrame(pCapture))
	{
		nFrmNum++;
		//if it is the first frame. then apply for the needed space
		if(nFrmNum==1)
		{
			pGrey = cvCreateImage( cvGetSize(pFrame), IPL_DEPTH_8U, 1 );
			//转化成单通道图像再处理
			cvCvtColor(pFrame, pGrey, CV_BGR2GRAY);
			histimage = cvCreateImage( cvSize(320,200), IPL_DEPTH_8U, 3 );
			dstimage = cvCreateImage(cvSize(2*a-1, 2*b-1), IPL_DEPTH_8U, 1);
			//先创建目标图像
			dstimage = patchimage_getting( pGrey, dstimage, state_prediction, cvSize(a, b));
			//计算目标图像直方图
			dsthist = hist_calculation(dstimage, dsthist, histnum, histranges);
			//画直方图
			cvZero( histimage );
			histimage = histogram_drawing(dsthist, histimage, histnum);

			pGrey = rectangle_drawing(pGrey, state_prediction, cvSize(a, b));
			cvNamedWindow("first frame", 1);
			cvShowImage("first frame", pGrey);
			cvShowImage("histogram", histimage);

			
			//根据目标初始位置初始化粒子组
			LB->data.fl[0] = 150.0f; // lb on data
			UB->data.fl[0] = 160.0f; // ub on data
			LB->data.fl[1] = 175.0f; // lb on data
			UB->data.fl[1] = 185.0f; // ub on data
			cvConDensInitSampleSet(ConDens, LB, UB);
 
			//for(int i=0;i<ConDens->SamplesNu m;i++)
			//{
			//	//std::cout<<ConDens->flSamples[i][0]<<"    "<<ConDens->flSamples[i][1]<<"\n";
			//	std::cout<<ConDens->flConfidence[i]<<"\n";
			//}
			
			cvWaitKey(); 

		}
		else 
		{
			//if(nFrmNum<6)   
			//	continue;
			//nFrmNum=2;

			//change to grey image
			cvCvtColor(pFrame, pGrey, CV_BGR2GRAY);

			std::cout<<"1state_prediction = "<<state_prediction.x<<"    "<<state_prediction.y<<"\n";

			state_prediction = PF_result(pGrey, ConDens, state_prediction, dsthist, cvSize(a, b), histnum, histranges, exptect_dist, steps, rng);

			std::cout<<"2state_prediction = "<<state_prediction.x<<"    "<<state_prediction.y<<"\n";
			//draw a rectangle in a position
			pFrame = rectangle_drawing(pFrame, state_prediction, cvSize(a, b));

			dstimage = patchimage_getting( pGrey, dstimage, state_prediction, cvSize(a, b));
			//计算目标图像直方图
			prediction_hist = hist_calculation(dstimage, dsthist, histnum, histranges);
			//画直方图
			cvZero( histimage );
			histimage = histogram_drawing(prediction_hist, histimage, histnum);


			cvShowImage("video", pFrame);
			cvShowImage("grey",pGrey);
			cvShowImage("histogram", histimage);
			//如果有按键事件,则跳出循环
			//此等待也为cvShowImage函数提供时间完成显示
			//等待时间可以根据CPU速度调整
			if( cvWaitKey(1) >= 0 )
				break;
			//cvShowImage("video", pFrame);
		}
	} 
	//销毁窗口
	cvDestroyWindow("video");
	cvDestroyWindow("grey");
	cvDestroyWindow("histogram");
	cvDestroyWindow("first frame");
	//释放图像指针
	cvReleaseImage(&pFrame);
	cvReleaseCapture(&pCapture);
	cvReleaseImage(&pGrey);
	cvReleaseImage(&dstimage);
	cvReleaseImage(&histimage);
	
	return 0;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -