📄 blobtrackanalysishist.cpp
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "_cvaux.h"
#define MAX_FV_SIZE 5
#define BLOB_NUM 5
typedef struct DefBlobFVN
{
CvBlob blob;
CvBlob BlobSeq[BLOB_NUM];
int state;
int LastFrame;
int FrameNum;
}DefBlobFVN;
class CvBlobTrackFVGenN: public CvBlobTrackFVGen
{
private:
CvBlobSeq m_BlobList;
CvMemStorage* m_pMem;
CvSeq* m_pFVSeq;
float m_FVMax[MAX_FV_SIZE];
float m_FVMin[MAX_FV_SIZE];
float m_FVVar[MAX_FV_SIZE];
int m_Dim;
CvBlob m_BlobSeq[BLOB_NUM];
int m_Frame;
int m_State;
int m_LastFrame;
int m_ClearFlag;
void Clear()
{
if(m_pMem)
{
cvClearMemStorage(m_pMem);
m_pFVSeq = cvCreateSeq(0,sizeof(CvSeq),sizeof(float)*(m_Dim+1), m_pMem);
m_ClearFlag = 1;
}
}
public:
CvBlobTrackFVGenN(int dim = 2 ):m_BlobList(sizeof(DefBlobFVN))
{
int i;
assert(dim <= MAX_FV_SIZE);
m_Dim = dim;
for(i=0;i<m_Dim;++i)
{
m_FVVar[i] = 0.01f;
m_FVMax[i] = 1;
m_FVMin[i] = 0;
}
m_Frame = 0;
m_State = 0;
m_pMem = cvCreateMemStorage();
m_pFVSeq = NULL;
Clear();
};
~CvBlobTrackFVGenN()
{
if(m_pMem)cvReleaseMemStorage(&m_pMem);
};
void AddBlob(CvBlob* pBlob)
{
float FV[MAX_FV_SIZE+1];
int i;
DefBlobFVN* pFVBlob = (DefBlobFVN*)m_BlobList.GetBlobByID(CV_BLOB_ID(pBlob));
if(!m_ClearFlag) Clear();
if(pFVBlob==NULL)
{
DefBlobFVN BlobNew;
BlobNew.blob = pBlob[0];
BlobNew.LastFrame = m_Frame;
BlobNew.state = 0;;
BlobNew.FrameNum = 0;
m_BlobList.AddBlob((CvBlob*)&BlobNew);
pFVBlob = (DefBlobFVN*)m_BlobList.GetBlobByID(CV_BLOB_ID(pBlob));
}/* add new record if need */
pFVBlob->blob = pBlob[0];
/* shift */
for(i=(BLOB_NUM-1);i>0;--i)
{
pFVBlob->BlobSeq[i] = pFVBlob->BlobSeq[i-1];
}
pFVBlob->BlobSeq[0] = pBlob[0];
if(m_Dim>0)
{/* calc FV position */
FV[0] = CV_BLOB_X(pBlob);
FV[1] = CV_BLOB_Y(pBlob);
}
if(m_Dim<=2)
{ /* add new FV if position is enought */
*(int*)(FV+m_Dim) = CV_BLOB_ID(pBlob);
cvSeqPush( m_pFVSeq, FV );
}
else if(pFVBlob->FrameNum > BLOB_NUM)
{ /* calc velocity for more complex FV */
float AverVx = 0;
float AverVy = 0;
{ /* average velocity */
CvBlob* pBlobSeq = pFVBlob->BlobSeq;
int i;
for(i=1;i<BLOB_NUM;++i)
{
AverVx += CV_BLOB_X(pBlobSeq+i-1)-CV_BLOB_X(pBlobSeq+i);
AverVy += CV_BLOB_Y(pBlobSeq+i-1)-CV_BLOB_Y(pBlobSeq+i);
}
AverVx /= BLOB_NUM-1;
AverVy /= BLOB_NUM-1;
FV[2] = AverVx;
FV[3] = AverVy;
}
if(m_Dim>4)
{ /* state duration */
float T = (CV_BLOB_WX(pBlob)+CV_BLOB_WY(pBlob))*0.01f;
if( fabs(AverVx) < T && fabs(AverVy) < T)
pFVBlob->state++;
else
pFVBlob->state=0;
FV[4] = (float)pFVBlob->state;
}/* state duration */
/* add new FV */
*(int*)(FV+m_Dim) = CV_BLOB_ID(pBlob);
cvSeqPush( m_pFVSeq, FV );
}/* if velocity is calculated */
pFVBlob->FrameNum++;
pFVBlob->LastFrame = m_Frame;
}; /* AddBlob */
void Process(IplImage* pImg, IplImage* /*pFG*/)
{
int i;
if(!m_ClearFlag) Clear();
for(i=m_BlobList.GetBlobNum();i>0;--i)
{ /* delete unused blob */
DefBlobFVN* pFVBlob = (DefBlobFVN*)m_BlobList.GetBlob(i-1);
if(pFVBlob->LastFrame < m_Frame)
{
m_BlobList.DelBlob(i-1);
}
}/* check next blob in list */
m_FVMin[0] = 0;
m_FVMin[1] = 0;
m_FVMax[0] = (float)(pImg->width-1);
m_FVMax[1] = (float)(pImg->height-1);
m_FVVar[0] = m_FVMax[0]*0.01f;
m_FVVar[1] = m_FVMax[1]*0.01f;
m_FVVar[2] = (float)(pImg->width-1)/1440.0f;
m_FVMax[2] = (float)(pImg->width-1)*0.02f;
m_FVMin[2] = -m_FVMax[2];
m_FVVar[3] = (float)(pImg->width-1)/1440.0f;
m_FVMax[3] = (float)(pImg->height-1)*0.02f;
m_FVMin[3] = -m_FVMax[3];
m_FVMax[4] = 25*32.0f; /* max state is 32 sec */
m_FVMin[4] = 0;
m_FVVar[4] = 10;
m_Frame++;
m_ClearFlag = 0;
};
virtual void Release(){delete this;};
virtual int GetFVSize(){return m_Dim;};
virtual int GetFVNum()
{
return m_pFVSeq->total;
};
virtual float* GetFV(int index, int* pFVID)
{
float* pFV = (float*)cvGetSeqElem( m_pFVSeq, index );
if(pFVID)pFVID[0] = *(int*)(pFV+m_Dim);
return pFV;
};
virtual float* GetFVMin(){return m_FVMin;}; /* returned pointer to array of minimal values of FV, if return 0 then FVrange is not exist */
virtual float* GetFVMax(){return m_FVMax;}; /* returned pointer to array of maximal values of FV, if return 0 then FVrange is not exist */
virtual float* GetFVVar(){return m_FVVar;}; /* returned pointer to array of maximal values of FV, if return 0 then FVrange is not exist */
};/* CvBlobTrackFVGenN */
CvBlobTrackFVGen* cvCreateFVGenP(){return (CvBlobTrackFVGen*)new CvBlobTrackFVGenN(2);}
CvBlobTrackFVGen* cvCreateFVGenPV(){return (CvBlobTrackFVGen*)new CvBlobTrackFVGenN(4);}
CvBlobTrackFVGen* cvCreateFVGenPVS(){return (CvBlobTrackFVGen*)new CvBlobTrackFVGenN(5);}
#undef MAX_FV_SIZE
#define MAX_FV_SIZE 4
class CvBlobTrackFVGenSS: public CvBlobTrackFVGen
{
private:
CvBlobSeq m_BlobList;
CvMemStorage* m_pMem;
CvSeq* m_pFVSeq;
float m_FVMax[MAX_FV_SIZE];
float m_FVMin[MAX_FV_SIZE];
float m_FVVar[MAX_FV_SIZE];
int m_Dim;
CvBlob m_BlobSeq[BLOB_NUM];
int m_Frame;
int m_State;
int m_LastFrame;
int m_ClearFlag;
void Clear()
{
cvClearMemStorage(m_pMem);
m_pFVSeq = cvCreateSeq(0,sizeof(CvSeq),sizeof(float)*(m_Dim+1), m_pMem);
m_ClearFlag = 1;
}
public:
CvBlobTrackFVGenSS(int dim = 2 ):m_BlobList(sizeof(DefBlobFVN))
{
int i;
assert(dim <= MAX_FV_SIZE);
m_Dim = dim;
for(i=0;i<m_Dim;++i)
{
m_FVVar[i] = 0.01f;
m_FVMax[i] = 1;
m_FVMin[i] = 0;
}
m_Frame = 0;
m_State = 0;
m_pMem = cvCreateMemStorage();
m_pFVSeq = NULL;
};
~CvBlobTrackFVGenSS()
{
if(m_pMem)cvReleaseMemStorage(&m_pMem);
};
void AddBlob(CvBlob* pBlob)
{
//float FV[MAX_FV_SIZE+1];
int i;
DefBlobFVN* pFVBlob = (DefBlobFVN*)m_BlobList.GetBlobByID(CV_BLOB_ID(pBlob));
if(!m_ClearFlag) Clear();
if(pFVBlob==NULL)
{
DefBlobFVN BlobNew;
BlobNew.blob = pBlob[0];
BlobNew.LastFrame = m_Frame;
BlobNew.state = 0;;
BlobNew.FrameNum = 0;
m_BlobList.AddBlob((CvBlob*)&BlobNew);
pFVBlob = (DefBlobFVN*)m_BlobList.GetBlobByID(CV_BLOB_ID(pBlob));
}/* add new record if need */
/* shift */
for(i=(BLOB_NUM-1);i>0;--i)
{
pFVBlob->BlobSeq[i] = pFVBlob->BlobSeq[i-1];
}
pFVBlob->BlobSeq[0] = pBlob[0];
if(pFVBlob->FrameNum > BLOB_NUM)
{/* average velocity */
CvBlob* pBlobSeq = pFVBlob->BlobSeq;
float T = (CV_BLOB_WX(pBlob)+CV_BLOB_WY(pBlob))*0.01f;
float AverVx = 0;
float AverVy = 0;
int i;
for(i=1;i<BLOB_NUM;++i)
{
AverVx += CV_BLOB_X(pBlobSeq+i-1)-CV_BLOB_X(pBlobSeq+i);
AverVy += CV_BLOB_Y(pBlobSeq+i-1)-CV_BLOB_Y(pBlobSeq+i);
}
AverVx /= BLOB_NUM-1;
AverVy /= BLOB_NUM-1;
if( fabs(AverVx) < T && fabs(AverVy) < T)
pFVBlob->state++;
else
pFVBlob->state=0;
}
if(pFVBlob->state == 5)
{ /* object is stoped */
float FV[MAX_FV_SIZE];
FV[0] = pFVBlob->blob.x;
FV[1] = pFVBlob->blob.y;
FV[2] = pFVBlob->BlobSeq[0].x;
FV[3] = pFVBlob->BlobSeq[0].y;
*(int*)(FV+m_Dim) = CV_BLOB_ID(pBlob);
cvSeqPush( m_pFVSeq, FV );
} /* object is stoped */
pFVBlob->FrameNum++;
pFVBlob->LastFrame = m_Frame;
}; /* AddBlob */
void Process(IplImage* pImg, IplImage* /*pFG*/)
{
int i;
if(!m_ClearFlag) Clear();
for(i=m_BlobList.GetBlobNum();i>0;--i)
{ /* delete unused blob */
DefBlobFVN* pFVBlob = (DefBlobFVN*)m_BlobList.GetBlob(i-1);
if(pFVBlob->LastFrame < m_Frame)
{
float FV[MAX_FV_SIZE+1];
FV[0] = pFVBlob->blob.x;
FV[1] = pFVBlob->blob.y;
FV[2] = pFVBlob->BlobSeq[0].x;
FV[3] = pFVBlob->BlobSeq[0].y;
*(int*)(FV+m_Dim) = CV_BLOB_ID(pFVBlob);
cvSeqPush( m_pFVSeq, FV );
m_BlobList.DelBlob(i-1);
}
}/* check next blob in list */
/* set max min range */
m_FVMin[0] = 0;
m_FVMin[1] = 0;
m_FVMin[2] = 0;
m_FVMin[3] = 0;
m_FVMax[0] = (float)(pImg->width-1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -