📄 video2.cpp
字号:
// video2.cpp : Defines the entry point for the console application.
//
#include "stdafx.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;
//纹理特征参数定义
CvMat* FuzhiMat=NULL;
CvMat* FangxiangMat=NULL;
CvMat* WenLi = cvCreateMat(36, 1, CV_32FC1);
CvMat* Model_WenLi = cvCreateMat(36, 1, CV_32FC1);
//std::cout<<WenLi->rows<<" "<<Model_WenLi->rows;
//创建窗口
cvNamedWindow("video", 1);
cvNamedWindow("grey",1);
cvNamedWindow("histogram", 1);
//粒子滤波框架的参数定义及初始化
float exptect_dist = 50;
int steps= 5;// How many condensentation steps per "tracking" step
int samplenum = 64;
int measurement_noise = 5;// setup our variance on measurement noise global so we can get to them in callbacks
int Dim=2;// we want a 2 dim model, we track x,y (not phi, delta_phi)
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=12,b=6;
//视频读取
int nFrmNum = 0;
if(!(pCapture = cvCaptureFromFile("shortvideo.avi")))
{
std::cout<<"cannot open video:"<<argv[1]<<"\n";
cvDestroyWindow("video");
cvDestroyWindow("grey");
cvDestroyWindow("histogram");
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);
//cvZero(dstimage);fatal error!!!!!!!!!!!!!!
dstimage = cvCreateImage(cvSize(2*a-1, 2*b-1), IPL_DEPTH_8U, 1);
//颜色直方图参数初始化
histimage = cvCreateImage( cvSize(320,200), IPL_DEPTH_8U, 3 );
//纹理特征参数初始 化
FuzhiMat = cvCreateMat(2*b-1, 2*a-1, CV_32FC1);
FangxiangMat = cvCreateMat(2*b-1, 2*a-1, CV_32FC1);
//先创建目标图像
dstimage = patchimage_getting( pGrey, dstimage, state_prediction, cvSize(a, b));
//cvNamedWindow("dstimage", 1);
//cvShowImage("histogram", dstimage);
//计算目标图像直方图
dsthist = hist_calculation(dstimage, dsthist, histnum, histranges);
//get直方图
histimage = histogram_drawing(dsthist, histimage, histnum);
//计算目标图像纹理特征
grads_calculation(dstimage, FuzhiMat, FangxiangMat);
//for(int i=1;i<FuzhiMat->cols-1;i++)
// for(int j=1;j<FuzhiMat->rows-1;j++)
// {
// std::cout<<FuzhiMat->data.fl[j * FuzhiMat->cols + i]<<" "<<FangxiangMat->data.fl[j * FangxiangMat->cols + i]<<"\n";
// }
wen_li_statistic(FuzhiMat, FangxiangMat, Model_WenLi);
//for(int i=0;i<36;i++)
//{
// std::cout<<Model_WenLi->data.fl[i]<<" ";
//}
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);
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, FuzhiMat, FangxiangMat, Model_WenLi, WenLi, \
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(100) >= 0 )
break;
//cvShowImage("video", pFrame);
}
}
//销毁窗口
cvDestroyWindow("video");
cvDestroyWindow("grey");
cvDestroyWindow("histogram");
cvDestroyWindow("first frame");
//cvDestroyWindow("dstimage");
//释放图像指针
cvReleaseImage(&pFrame);
cvReleaseCapture(&pCapture);
cvReleaseImage(&pGrey);
cvReleaseImage(&dstimage);
cvReleaseImage(&histimage);
//释放矩阵
cvReleaseMat( &FuzhiMat );
cvReleaseMat( &FangxiangMat );
cvReleaseMat( &WenLi );
cvReleaseMat( &Model_WenLi );
cvReleaseMat( &LB );
cvReleaseMat( &UB );
//释放直方图结构体
cvReleaseHist( &dsthist );
cvReleaseHist( &prediction_hist );
cvReleaseHist( &particlehist );
//释放粒子结构体
cvReleaseConDensation( &ConDens );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -