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

📄 blobtrackanalysishist.cpp

📁 Program use openCV to demo edge detection (algorithm Gradient).
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        m_FVMax[1] = (float)(pImg->height-1);
        m_FVMax[2] = (float)(pImg->width-1);
        m_FVMax[3] = (float)(pImg->height-1);
        m_FVVar[0] = m_FVMax[0]*0.01f;
        m_FVVar[1] = m_FVMax[1]*0.01f;
        m_FVVar[2] = m_FVMax[2]*0.01f;
        m_FVVar[3] = m_FVMax[3]*0.01f;

        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 */
};/* CvBlobTrackFVGenSS */
CvBlobTrackFVGen* cvCreateFVGenSS(){return (CvBlobTrackFVGen*)new CvBlobTrackFVGenSS;}

/*======================= TRAJECTORY ANALAZER MODULES =====================*/
/* Trajectory Analyser module */
#define SPARSE  0
#define ND      1
#define BYSIZE  -1
class DefMat
{
private:
    CvSparseMatIterator m_SparseIterator;
    CvSparseNode*       m_pSparseNode;
    int*                m_IDXs;
    int                 m_Dim;

public:
    CvSparseMat*        m_pSparse;
    CvMatND*            m_pND;
    int                 m_Volume;
    int                 m_Max;
    DefMat(int dim = 0, int* sizes = NULL, int type = SPARSE)
    {
        /* create sparse or ND matrix but not both */
        m_pSparseNode = NULL;
        m_pSparse = NULL;
        m_pND = NULL;
        m_Volume = 0;
        m_Max = 0;
        m_IDXs = NULL;
        m_Dim = 0;
        if(dim>0 && sizes != 0)
            Realloc(dim, sizes, type);
    }
    ~DefMat()
    {
        if(m_pSparse)cvReleaseSparseMat(&m_pSparse);
        if(m_pND)cvReleaseMatND(&m_pND);
        if(m_IDXs) cvFree(&m_IDXs);
    }

    void Realloc(int dim, int* sizes, int type = SPARSE)
    {
        if(m_pSparse)cvReleaseSparseMat(&m_pSparse);
        if(m_pND)cvReleaseMatND(&m_pND);

        if(type == BYSIZE )
        {
            int size = 0;
            int i;
            for(size=1,i=0;i<dim;++i)
            {
                size *= sizes[i];
            }
            size *= sizeof(int);
            if(size > (2<<20))
            { /* if size > 1M */
                type = SPARSE;
            }
            else
            {
                type = ND;
            }
        }/* define matrix type */

        if(type == SPARSE)
        {
            m_pSparse = cvCreateSparseMat( dim, sizes, CV_32SC1 );
            m_Dim = dim;
        }
        if(type == ND )
        {
            m_pND = cvCreateMatND( dim, sizes, CV_32SC1 );
            cvZero(m_pND);
            m_IDXs = (int*)cvAlloc(sizeof(int)*dim);
            m_Dim = dim;
        }
        m_Volume = 0;
        m_Max = 0;
    }
    void Save(char* File)
    {
        if(m_pSparse)cvSave(File, m_pSparse );
        if(m_pND)cvSave(File, m_pND );
    }
    void Save(CvFileStorage* fs, char* name)
    {
        if(m_pSparse)
        {
            cvWrite(fs, name, m_pSparse );
        }
        else if(m_pND)
        {
            cvWrite(fs, name, m_pND );
        }
    }
    void Load(char* File)
    {
        CvFileStorage* fs = cvOpenFileStorage( File, NULL, CV_STORAGE_READ );
        if(fs)
        {
            void* ptr;
            if(m_pSparse) cvReleaseSparseMat(&m_pSparse);
            if(m_pND) cvReleaseMatND(&m_pND);
            m_Volume = 0;
            m_Max = 0;
            ptr = cvLoad(File);
            if(ptr && CV_IS_MATND_HDR(ptr)) m_pND = (CvMatND*)ptr;
            if(ptr && CV_IS_SPARSE_MAT_HDR(ptr)) m_pSparse = (CvSparseMat*)ptr;
            cvReleaseFileStorage(&fs);
        }
        AfterLoad();
    }/* Load */
    
    void Load(CvFileStorage* fs, CvFileNode* node, char* name)
    {
        CvFileNode* n = cvGetFileNodeByName(fs,node,name);
        void* ptr = n?cvRead(fs,n):NULL;
        if(ptr)
        {
            if(m_pSparse) cvReleaseSparseMat(&m_pSparse);
            if(m_pND) cvReleaseMatND(&m_pND);
            m_Volume = 0;
            m_Max = 0;
            if(CV_IS_MATND_HDR(ptr)) m_pND = (CvMatND*)ptr;
            if(CV_IS_SPARSE_MAT_HDR(ptr)) m_pSparse = (CvSparseMat*)ptr;
        }
        else
        {
            printf("WARNING!!! Can't load %s matrix\n",name);
        }
        AfterLoad();
    }/* Load */
    void AfterLoad()
    {
        m_Volume = 0;
        m_Max = 0;
        if(m_pSparse)
        {/* calculate Volume of loaded hist */
            CvSparseMatIterator mat_iterator;
            CvSparseNode* node = cvInitSparseMatIterator( m_pSparse, &mat_iterator );

            for( ; node != 0; node = cvGetNextSparseNode( &mat_iterator ))
            {
                int val = *(int*)CV_NODE_VAL( m_pSparse, node ); /* get value of the element
                                                                (assume that the type is CV_32SC1) */
                m_Volume += val;
                if(m_Max < val)m_Max = val;
            }
        }/* calculate Volume of loaded hist */
        if(m_pND)
        {/* calculate Volume of loaded hist */
            CvMat   mat;
            double  max_val;
            double  vol;
            cvGetMat( m_pND, &mat, NULL, 1 );

            vol = cvSum(&mat).val[0];
            m_Volume = cvRound(vol);
            cvMinMaxLoc( &mat, NULL, &max_val);
            m_Max = cvRound(max_val);
            /* MUST BE WRITTEN LATER */
        }/* calculate Volume of loaded hist */
    }/* AfterLoad */
    int* GetPtr(int* indx)
    {
        if(m_pSparse) return (int*)cvPtrND( m_pSparse, indx, NULL, 1, NULL);
        if(m_pND) return  (int*)cvPtrND( m_pND, indx, NULL, 1, NULL);
        return NULL;
    }/* GetPtr */
    int GetVal(int* indx)
    {
        int* p = GetPtr(indx);
        if(p)return p[0];
        return -1;
    }/* GetVal */
    int Add(int* indx, int val)
    {
        int  NewVal;
        int* pVal = GetPtr(indx);
        if(pVal == NULL) return -1;
        pVal[0] += val;
        NewVal = pVal[0];
        m_Volume += val;
        if(m_Max < NewVal)m_Max = NewVal;
        return NewVal;
    }/* Add */
    void Add(DefMat* pMatAdd)
    {
        int*    pIDXS = NULL;
        int     Val = 0;
        for(Val = pMatAdd->GetNext(&pIDXS, 1 );pIDXS;Val=pMatAdd->GetNext(&pIDXS, 0 ))
        {
            Add(pIDXS,Val);
        }
    }/* Add */
    int SetMax(int* indx, int val)
    {
        int  NewVal;
        int* pVal = GetPtr(indx);
        if(pVal == NULL) return -1;
        if(val > pVal[0])
        {
            m_Volume += val-pVal[0];
            pVal[0] = val;
        }
        NewVal = pVal[0];
        if(m_Max < NewVal)m_Max = NewVal;
        return NewVal;
    }/* Add */
    int GetNext(int** pIDXS, int init = 0)
    {
        int     Val = 0;
        pIDXS[0] = NULL;
        if(m_pSparse)
        {
            m_pSparseNode = (init || m_pSparseNode==NULL)?
                cvInitSparseMatIterator( m_pSparse, &m_SparseIterator ):
                cvGetNextSparseNode( &m_SparseIterator );

            if(m_pSparseNode)
            {
                int* pVal = (int*)CV_NODE_VAL( m_pSparse, m_pSparseNode );
                if(pVal)Val = pVal[0];
                pIDXS[0] = CV_NODE_IDX( m_pSparse, m_pSparseNode );
            }
        }/* sparce matrix */
        if(m_pND)
        {
            int i;
            if(init)
            {
                for(i=0;i<m_Dim;++i)
                {
                    m_IDXs[i] = cvGetDimSize( m_pND, i )-1;
                }
                pIDXS[0] = m_IDXs;
                Val = GetVal(m_IDXs);
            }
            else
            {
                for(i=0;i<m_Dim;++i)
                {
                    if((m_IDXs[i]--)>0)
                        break;
                    m_IDXs[i] = cvGetDimSize( m_pND, i )-1;
                }
                if(i==m_Dim)
                {
                    pIDXS[0] = NULL;
                }
                else
                {
                    pIDXS[0] = m_IDXs;
                    Val = GetVal(m_IDXs);
                }
            }/* get next ND */
        }/* sparce matrix */
        return Val;
    };/* GetNext */
};

#define FV_NUM 10
#define FV_SIZE 10
typedef struct DefTrackFG
{
    CvBlob                  blob;
//    CvBlobTrackFVGen*       pFVGen;
    int                     LastFrame;
    float                   state;
    DefMat*                 pHist;
} DefTrackFG;
class CvBlobTrackAnalysisHist : public CvBlobTrackAnalysis
{
    /*---------------- internal functions --------------------*/
private:
    int                 m_BinNumParam;
    int                 m_SmoothRadius;
    char*               m_SmoothKernel;
    float               m_AbnormalThreshold;
    int                 m_TrackNum;
    int                 m_Frame;
    int                 m_BinNum;
    char                m_DataFileName[1024];
    int                 m_Dim;
    int*                m_Sizes;                 
    DefMat              m_HistMat;
    int                 m_HistVolumeSaved;
    int*                m_pFVi;
    int*                m_pFViVar;
    int*                m_pFViVarRes;
    CvBlobSeq           m_TrackFGList;
    //CvBlobTrackFVGen*   (*m_CreateFVGen)();
    CvBlobTrackFVGen*   m_pFVGen;
    void SaveHist()
    {
        if(m_DataFileName[0])
        {
            m_HistMat.Save(m_DataFileName);
            m_HistVolumeSaved = m_HistMat.m_Volume;
        }
    };
    void LoadHist()
    {
        if(m_DataFileName[0])m_HistMat.Load(m_DataFileName);
        m_HistVolumeSaved = m_HistMat.m_Volume;
    }
    void AllocData()
    {/* AllocData */
        m_pFVi = (int*)cvAlloc(sizeof(int)*m_Dim);
        m_pFViVar = (int*)cvAlloc(sizeof(int)*m_Dim);
        m_pFViVarRes = (int*)cvAlloc(sizeof(int)*m_Dim);
        m_Sizes = (int*)cvAlloc(sizeof(int)*m_Dim);
        
        { /* create init sparce matrix */
            int     i;
            for(i=0;i<m_Dim;++i)m_Sizes[i] = m_BinNum;
            m_HistMat.Realloc(m_Dim,m_Sizes,SPARSE);
            m_HistVolumeSaved = 0;
        } /* create init sparce matrix */
    }/* AllocData */
    void FreeData()
    { /* FreeData */
        int i;
        for(i=m_TrackFGList.GetBlobNum();i>0;--i)
        {
            //DefTrackFG* pF = (DefTrackFG*)m_TrackFGList.GetBlob(i-1);
//            pF->pFVGen->Release();
            m_TrackFGList.DelBlob(i-1);
        }
        cvFree(&m_pFVi);
        cvFree(&m_pFViVar);
        cvFree(&m_pFViVarRes);
        cvFree(&m_Sizes);
    }/* FreeData */
    virtual void ParamUpdate()
    {
        if(m_BinNum != m_BinNumParam)

⌨️ 快捷键说明

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