// Tracker.cpp: implementation of the Tracker class.
//
//////////////////////////////////////////////////////////////////////
#include "Tracker.h"
#include "adaboostDetect.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Tracker::Tracker()
{
nTrajectories=0;
frameNo=0;
}
Tracker::~Tracker()
{
delete[] trajectories;
}
void Tracker::initTracker(IplImage* frame, CvRect* regions, int nRegions, int particlesPerObject)
{
IplImage* frameHSV=0;
colorFeatures cf;
p_perObject=particlesPerObject;
nTrajectories=nRegions;
frameHSV=cf.bgr2hsv(frame);
trajectories = (trajectory*)malloc( nRegions * sizeof( trajectory ) );
for (int i=0; i<nTrajectories; i++)
{
particleFilter* pf;
pf=new particleFilter;
pf->initParticles(regions[i], particlesPerObject);
trajectories[i].object = pf;
trajectories[i].object->objectID=i;
trajectories[i].object->weight=1;
trajectories[i].stratFrame=0;
trajectories[i].histo=computeHistogram( frameHSV, regions[i] );
trajectories[i].points = (CvPoint*) malloc ( sizeof(CvPoint));
trajectories[i].points[frameNo]=trajectories[i].object->getParticleCentre();
}
frameNo++;
}
void Tracker::next(IplImage* frame, adaboostDetect* adabosst, char *fileName)
{
IplImage* frameHSV=0;
colorFeatures cf;
int w,h;
frameHSV=cf.bgr2hsv(frame);
w = frame->width;
h = frame->height;
CvRect *rects=0;
int ntrack=0;
for (int i=0; i<nTrajectories; i++)
{
trajectories[i].object->transition(w,h);
trajectories[i].object->updateWeight(frameHSV, trajectories[i].histo);
trajectories[i].object->normalizeWeights();
trajectories[i].object->resample();
trajectories[i].points = (CvPoint*) realloc (trajectories[i].points, (frameNo+1) * sizeof(CvPoint));
trajectories[i].points[frameNo]=trajectories[i].object->getParticleCentre();
}
mergeTrack();
cvReleaseImage( &frameHSV );
frameNo++;
}
histogram* Tracker::computeHistogram(IplImage* frame, CvRect region)
{
colorFeatures cf;
IplImage* tmp;
histogram* hist;
cvSetImageROI( frame, region );
tmp = cvCreateImage( cvGetSize( frame ), IPL_DEPTH_32F, 3 );
cvCopy( frame, tmp, NULL );
cvResetImageROI( frame );
hist = cf.comHistogramHSV( &tmp, 1 );
cf.normalizeHistogram( hist );
cvReleaseImage( &tmp );
return hist;
}
void Tracker::showResults(IplImage* frame, int param)
{
for (int i=0; i<nTrajectories; i++)
{
CvScalar color;
color = CV_RGB(255,0,0);
if (trajectories[i].object->weight>0.5)
{
int p=SHOW_SELECTED;
trajectories[i].object->displayParticles( frame, CV_RGB(0,0,255), color , p);
CvFont font;
cvInitFont(&font,CV_FONT_HERSHEY_PLAIN|CV_FONT_ITALIC,1,1,0,1);
char buffer [4];
sprintf (buffer, "%d",trajectories[i].object->objectID );
cvPutText(frame,buffer, cvPoint( cvRound(trajectories[i].object->particles[0].x)+5, cvRound(trajectories[i].object->particles[0].y)+5 ), &font, cvScalar(255,255,255));
}
}
}
IplImage* Tracker::subtractObjects(IplImage* frame)
{
IplImage* tmp;
tmp = cvCreateImage( cvSize(frame->width,frame->height),
IPL_DEPTH_8U, frame->nChannels );
cvCopy( frame, tmp);
int w,h;
w = frame->width;
h = frame->height;
for (int i=0; i<nTrajectories; i++)
{
if (trajectories[i].object->weight<0.5)
continue;
CvRect r;
r= trajectories[i].object->getParticleRect();
for (int k=r.y; k<r.y+r.height;k++)
for (int j=r.x; j<r.x+r.width;j++)
{
if ((k>=0)&&(k<h)&&(j>=0)&&(j<w))
{
//cvSet2D(tmp,k,j,cvScalar(0,0,0));
((uchar *)(tmp->imageData + k*tmp->widthStep))[j*tmp->nChannels + 0]=0;
((uchar *)(tmp->imageData + k*tmp->widthStep))[j*tmp->nChannels + 1]=0;
((uchar *)(tmp->imageData + k*tmp->widthStep))[j*tmp->nChannels + 2]=0;
}
}
}
return tmp;
}
void Tracker::addObjects(IplImage *frame, CvRect *regions, int nRegions)
{
IplImage* frameHSV=0;
colorFeatures cf;
int rs=0, nw=0,tr=0, i;
float d=0, x1,y1, dm=0, x, y;
int trFound[200];
for (i=0; i<nTrajectories; i++)
trFound[i]=0;
if (nRegions<1)
return;
frameHSV=cf.bgr2hsv(frame);
for (i=nTrajectories; i<nTrajectories+nRegions; i++)
{
//////////////////////////////////////////////////
x = (float)regions[i-nTrajectories].x+regions[i-nTrajectories].width/2;
y = (float)regions[i-nTrajectories].y+regions[i-nTrajectories].height/2;
for (int j=0; j<nTrajectories; j++)
{
x1 = trajectories[j].object->particles[0].x;
y1 = trajectories[j].object->particles[0].y;
d = sqrtf( (x1 - x) * (x1 - x) + (y1 - y) * (y1 - y));
if (j==0)
dm=d;
if (d<dm)
{
tr=j;
dm=d;
}
}
////////////////////////////////////////////////////
if (dm<20)
{
////////////////////////////////////////////////
trajectories[tr].object->resetParticles( regions[i-nTrajectories]);
frameHSV=cf.bgr2hsv(frame);
trajectories[tr].histo=computeHistogram( frameHSV, regions[i-nTrajectories] );
///////////////////////////////////////////////////////////////////
trajectories[tr].object->weight=1;
trFound[tr]=1;
rs++;
}
else
{
trajectories= (trajectory*) realloc (trajectories, (nTrajectories+nw+1) * sizeof(trajectory));
particleFilter* pf;
pf=new particleFilter;
pf->initParticles(regions[i-nTrajectories], p_perObject);
trajectories[nw+nTrajectories].stratFrame=frameNo;
trajectories[nw+nTrajectories].object = pf;
trajectories[nw+nTrajectories].object->objectID=nw+nTrajectories;
trajectories[nw+nTrajectories].object->weight=0.5;
trajectories[nw+nTrajectories].histo=computeHistogram( frameHSV, regions[i-nTrajectories] );
trajectories[nw+nTrajectories].points = (CvPoint*) malloc ( frameNo*sizeof(CvPoint));
trajectories[nw+nTrajectories].points[frameNo]=trajectories[nw+nTrajectories].object->getParticleCentre();
nw++;
}
}
for (int j=0; j<nTrajectories; j++)
{
if (trFound[j]==1)
{
if (trajectories[j].object->weight<1)
trajectories[j].object->weight+=0.5;
}
else
{
trajectories[j].object->weight-=0.5;//0.35
}
}
nTrajectories=nTrajectories+nw;
for (i=0; i<nTrajectories; i++)
if (trajectories[i].object->weight<-3)
removeTrack(i);
}
void Tracker::updateObjectWeights(IplImage *frame, adaboostDetect* adabosst)
{
IplImage* frameHSV=0, *temp=0;
colorFeatures cf;
int ff=0;
for (int i=0; i<nTrajectories; i++)
{
colorFeatures cf;
int x, y, w, h;
w=cvRound(trajectories[i].object->particles[0].width);
h=cvRound(trajectories[i].object->particles[0].height);
x=cvRound(trajectories[i].object->particles[0].x)-w/2;
y=cvRound(trajectories[i].object->particles[0].y)-h/2;
cvSetImageROI( frame, cvRect(x-5,y-5,w+10,h+10) );
temp = cvCreateImage( cvGetSize( frame ),
IPL_DEPTH_8U, frame->nChannels );
cvCopy( frame, temp, NULL );
int found=0,n=0;
CvRect* rgs;
n=adabosst->detectObject(temp, &rgs);
if (n>0) found=1;
if (found==1)
{
if (trajectories[i].object->weight<1)
trajectories[i].object->weight+=0.5;
ff++;
}
else
{
trajectories[i].object->weight-=0.5;//0.35
if (trajectories[i].object->weight<-4)
removeTrack(i);
}
cvReleaseImage( &temp );
cvResetImageROI( frame );
}
}
void Tracker::removeTrack(int No)
{
for (int i=No;i<nTrajectories-1;i++)
{
trajectories[i]=trajectories[i+1];
}
nTrajectories--;
}
void Tracker::mergeTrack()
{
float d=0, x1,y1, x, y;
for (int i=0;i<nTrajectories-1;i++)
{
x = trajectories[i].object->particles[0].x;
y = trajectories[i].object->particles[0
headCountCode.zip_opencv head_opencv 训练_opencv人头分类_人头训练_人群计数 ope
版权申诉
128 浏览量
2022-07-14
01:29:36
上传
评论
收藏 53KB ZIP 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- stm32 usb接口通信
- Chessmate是一款完全免费的国际象棋学习软件,支持引擎分析,学开局、残局、棋书解读、大数据分析等功能
- 总结整理的Android面试Java基础知识点面试资料精编汇总文档资料合集.zip
- .android_lq
- FDN5632N-VB一款SOT23封装N-Channel场效应MOS管
- 毛老板-2404250902.amr
- Java类加载流程(双亲委派)流程图.zip
- FDN5632-NL-VB一款SOT23封装N-Channel场效应MOS管
- 新目标大学英语(第二版)视听说教程 第1册 Unit 4 TOP课件.zip
- 自动驾驶-状态估计和定位之Error State EKF.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0