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

📄 cvvidsurv.hpp

📁 Simple ellipse fitting example on C++Builder6 + OpenCV1.0.
💻 HPP
📖 第 1 页 / 共 3 页
字号:


/* BLOB STRUCTURE*/
struct CvBlob
{
    float   x,y; /* blob position   */
    float   w,h; /* blob sizes      */
    int     ID;  /* blbo ID         */     
};

inline CvBlob cvBlob(float x,float y, float w, float h)
{
    CvBlob B = {x,y,w,h,0}; 
    return B;
}
#define CV_BLOB_MINW 5
#define CV_BLOB_MINH 5
#define CV_BLOB_ID(pB) (((CvBlob*)(pB))->ID)
#define CV_BLOB_CENTER(pB) cvPoint2D32f(((CvBlob*)(pB))->x,((CvBlob*)(pB))->y)
#define CV_BLOB_X(pB) (((CvBlob*)(pB))->x)
#define CV_BLOB_Y(pB) (((CvBlob*)(pB))->y)
#define CV_BLOB_WX(pB) (((CvBlob*)(pB))->w)
#define CV_BLOB_WY(pB) (((CvBlob*)(pB))->h)
#define CV_BLOB_RX(pB) (0.5f*CV_BLOB_WX(pB))
#define CV_BLOB_RY(pB) (0.5f*CV_BLOB_WY(pB))
#define CV_BLOB_RECT(pB) cvRect(cvRound(((CvBlob*)(pB))->x-CV_BLOB_RX(pB)),cvRound(((CvBlob*)(pB))->y-CV_BLOB_RY(pB)),cvRound(CV_BLOB_WX(pB)),cvRound(CV_BLOB_WY(pB)))
/* END BLOB STRUCTURE*/


/* simple BLOBLIST */
class CV_EXPORTS CvBlobSeq
{
public:
    CvBlobSeq(int BlobSize = sizeof(CvBlob))
    {
        m_pMem = cvCreateMemStorage();
        m_pSeq = cvCreateSeq(0,sizeof(CvSeq),BlobSize,m_pMem);
        strcpy(m_pElemFormat,"ffffi");
    }
    virtual ~CvBlobSeq()
    { 
        cvReleaseMemStorage(&m_pMem);
    };
    virtual CvBlob* GetBlob(int BlobIndex)
    {
        return (CvBlob*)cvGetSeqElem(m_pSeq,BlobIndex);
    };
    virtual CvBlob* GetBlobByID(int BlobID)
    {
        int i;
        for(i=0;i<m_pSeq->total;++i)
            if(BlobID == CV_BLOB_ID(GetBlob(i)))
                return GetBlob(i);
        return NULL;
    };
    virtual void DelBlob(int BlobIndex)
    {
        cvSeqRemove(m_pSeq,BlobIndex);
    };
    virtual void DelBlobByID(int BlobID)
    {
        int i;
        for(i=0;i<m_pSeq->total;++i)
        {
            if(BlobID == CV_BLOB_ID(GetBlob(i)))
            {
                DelBlob(i);
                return;
            }
        }
    };
    virtual void Clear()
    {
        cvClearSeq(m_pSeq);
    };
    virtual void AddBlob(CvBlob* pB)
    {
        cvSeqPush(m_pSeq,pB);
    };
    virtual int GetBlobNum()
    {
        return m_pSeq->total;
    };
    virtual void Write(CvFileStorage* fs, char* name)
    {
        char*  attr[] = {"dt",m_pElemFormat,NULL};
        if(fs)
        {
            cvWrite(fs,name,m_pSeq,cvAttrList((const char**)attr,NULL));
        }
    }
    virtual void Load(CvFileStorage* fs, CvFileNode* node)
    {
        if(fs==NULL) return;
        CvSeq* pSeq = (CvSeq*)cvRead(fs, node);
        if(pSeq)
        {
            int i;
            cvClearSeq(m_pSeq);
            for(i=0;i<pSeq->total;++i)
            {
                void* pB = cvGetSeqElem( pSeq, i );
                cvSeqPush( m_pSeq, pB );
            }
        }
    }
    void AddFormat(char* str){strcat(m_pElemFormat,str);}
protected:
    CvMemStorage*   m_pMem;
    CvSeq*          m_pSeq;
    char            m_pElemFormat[1024];
};
/* simple BLOBLIST */


/* simple TRACKLIST */
struct CvBlobTrack
{
    int         TrackID;
    int         StartFrame;
    CvBlobSeq*  pBlobSeq;
};

class CV_EXPORTS CvBlobTrackSeq
{
public:
    CvBlobTrackSeq(int TrackSize = sizeof(CvBlobTrack))
    {
        m_pMem = cvCreateMemStorage();
        m_pSeq = cvCreateSeq(0,sizeof(CvSeq),TrackSize,m_pMem);
    }
    virtual ~CvBlobTrackSeq()
    { 
        Clear();
        cvReleaseMemStorage(&m_pMem);
    };
    virtual CvBlobTrack* GetBlobTrack(int TrackIndex)
    {
        return (CvBlobTrack*)cvGetSeqElem(m_pSeq,TrackIndex);
    };
    virtual CvBlobTrack* GetBlobTrackByID(int TrackID)
    {
        int i;
        for(i=0;i<m_pSeq->total;++i)
        {
            CvBlobTrack* pP = GetBlobTrack(i);
            if(pP && pP->TrackID == TrackID)
                return pP;
        }
        return NULL;
    };
    virtual void DelBlobTrack(int TrackIndex)
    {
        CvBlobTrack* pP = GetBlobTrack(TrackIndex);
        if(pP && pP->pBlobSeq) delete pP->pBlobSeq;
        cvSeqRemove(m_pSeq,TrackIndex);
    };
    virtual void DelBlobTrackByID(int TrackID)
    {
        int i;
        for(i=0;i<m_pSeq->total;++i)
        {
            CvBlobTrack* pP = GetBlobTrack(i);
            if(TrackID == pP->TrackID)
            {
                DelBlobTrack(i);
                return;
            }
        }
    };
    virtual void Clear()
    {
        int i;
        for(i=GetBlobTrackNum();i>0;i--)
        {
            DelBlobTrack(i-1);
        }
        cvClearSeq(m_pSeq);
    };
    virtual void AddBlobTrack(int TrackID, int StartFrame = 0)
    {
        CvBlobTrack N;
        N.TrackID = TrackID;
        N.StartFrame = StartFrame;
        N.pBlobSeq = new CvBlobSeq;
        cvSeqPush(m_pSeq,&N);
    };
    virtual int GetBlobTrackNum()
    {
        return m_pSeq->total;
    };
protected:
    CvMemStorage*   m_pMem;
    CvSeq*          m_pSeq;
};

/* simple TRACKLIST */


/* BLOB DETECTOR INTERFACE */
class CV_EXPORTS CvBlobDetector: public CvVSModule
{
public:
    /* try to detect new blob entrance based on foreground mask */
    /* pFGMask - image of foreground mask */
    /* pNewBlob - pointer to CvBlob structure which will bew filled if new blob entrance detected */
    /* pOldBlobList - pointer to blob list which already exist on image */
    virtual int DetectNewBlob(IplImage* pImg, IplImage* pImgFG, CvBlobSeq* pNewBlobList, CvBlobSeq* pOldBlobList) = 0;
    /* release blob detector */
    virtual void Release()=0;
};
/* release any blob detector*/
inline void cvReleaseBlobDetector(CvBlobDetector** ppBD)
{
    ppBD[0]->Release();
    ppBD[0] = NULL;
}
/* END BLOB DETECTOR INTERFACE */

/* declaration of constructors of implemented modules */
CV_EXPORTS CvBlobDetector* cvCreateBlobDetectorSimple();
CV_EXPORTS CvBlobDetector* cvCreateBlobDetectorCC();


struct CV_EXPORTS CvDetectedBlob : public CvBlob
{
    float response;
};

CV_INLINE CvDetectedBlob cvDetectedBlob( float x, float y, float w, float h, int ID = 0, float response = 0.0F )
{
    CvDetectedBlob b;
    b.x = x; b.y = y; b.w = w; b.h = h; b.ID = ID; b.response = response;
    return b;
}


class CV_EXPORTS CvObjectDetector
{
public:
    CvObjectDetector( const char* /*detector_file_name*/ = 0 ) {};
    
    ~CvObjectDetector() {};

    /*
     * Releases the current detector and loads new detector from file
     * (if detector_file_name is not 0)
     * Returns true on success
     */
    bool Load( const char* /*detector_file_name*/ = 0 ) { return false; }

    /* Returns min detector window size */
    CvSize GetMinWindowSize() const { return cvSize(0,0); }
    
    /* Returns max border */
    int GetMaxBorderSize() const { return 0; }

    /*
     * Detects the objects on the image and pushes the detected
     * blobs into <detected_blob_seq> which must be the sequence of <CvDetectedBlob>s
     */
    void Detect( const CvArr* /*img*/, /* out */ CvBlobSeq* /*detected_blob_seq*/ = 0 ) {};

protected:
    class CvObjectDetectorImpl* impl;
};


CV_INLINE CvRect cvRectIntersection( const CvRect r1, const CvRect r2 )
{
    CvRect r = cvRect( MAX(r1.x, r2.x), MAX(r1.y, r2.y), 0, 0 );

    r.width  = MIN(r1.x + r1.width, r2.x + r2.width) - r.x;
    r.height = MIN(r1.y + r1.height, r2.y + r2.height) - r.y;

    return r;
}


/*
 * CvImageDrawer
 *
 * Draws on an image the specified ROIs from the source image and
 * given blobs as ellipses or rectangles
 */

struct CvDrawShape
{
    enum {RECT, ELLIPSE} shape;
    CvScalar color;
};

/*extern const CvDrawShape icv_shape[] =
{
    { CvDrawShape::ELLIPSE, CV_RGB(255,0,0) },
    { CvDrawShape::ELLIPSE, CV_RGB(0,255,0) },
    { CvDrawShape::ELLIPSE, CV_RGB(0,0,255) },
    { CvDrawShape::ELLIPSE, CV_RGB(255,255,0) },
    { CvDrawShape::ELLIPSE, CV_RGB(0,255,255) },
    { CvDrawShape::ELLIPSE, CV_RGB(255,0,255) }
};*/

class CV_EXPORTS CvImageDrawer
{
public:
    CvImageDrawer() : m_image(0) {}
    ~CvImageDrawer() { cvReleaseImage( &m_image ); }
    void SetShapes( const CvDrawShape* shapes, int num );
    /* <blob_seq> must be the sequence of <CvDetectedBlob>s */
    IplImage* Draw( const CvArr* src, CvBlobSeq* blob_seq = 0, const CvSeq* roi_seq = 0 );
    IplImage* GetImage() { return m_image; }
protected:
    //static const int MAX_SHAPES = sizeof(icv_shape) / sizeof(icv_shape[0]);;

    IplImage* m_image;    
    CvDrawShape m_shape[16];
};



/* Trajectory generation module */
class CV_EXPORTS CvBlobTrackGen: public CvVSModule
{
public:
    virtual void    SetFileName(char* pFileName) = 0;
    virtual void    AddBlob(CvBlob* pBlob) = 0;
    virtual void    Process(IplImage* pImg = NULL, IplImage* pFG = NULL) = 0;
    virtual void    Release() = 0;
};

inline void cvReleaseBlobTrackGen(CvBlobTrackGen** pBTGen)
{
    if(*pBTGen)(*pBTGen)->Release();
    *pBTGen = 0;
}

/* declaration of constructors of implemented modules */
CV_EXPORTS CvBlobTrackGen* cvCreateModuleBlobTrackGen1();
CV_EXPORTS CvBlobTrackGen* cvCreateModuleBlobTrackGenYML();



/* BLOB TRACKER INTERFACE */
class CV_EXPORTS CvBlobTracker: public CvVSModule
{
public:
    CvBlobTracker(){SetTypeName("BlobTracker");};
    /* Add new blob to track it and assign to this blob personal ID */
    /* pBlob - pinter to structure with blob parameters (ID is ignored)*/
    /* pImg - current image */
    /* pImgFG - current foreground mask */
    /* return pointer to new added blob */
    virtual CvBlob* AddBlob(CvBlob* pBlob, IplImage* pImg, IplImage* pImgFG = NULL ) = 0;
    /* return number of currently tracked blobs */
    virtual int     GetBlobNum() = 0;
    /* return pointer to specified by index blob */
    virtual CvBlob* GetBlob(int BlobIndex) = 0;
    
    /* delete blob by its index */
    virtual void    DelBlob(int BlobIndex) = 0;
    /* process current image and track all existed blobs */
    virtual void    Process(IplImage* pImg, IplImage* pImgFG = NULL) = 0;
    /* release blob tracker */
    virtual void    Release() = 0;


    /* Process on blob (for multi hypothesis tracing) */
    virtual void ProcessBlob(int BlobIndex, CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
    {
        CvBlob* pB;
        int ID = 0;
        assert(pBlob);
        //pBlob->ID;
        pB = GetBlob(BlobIndex);
        if(pB)
            pBlob[0] = pB[0];
        pBlob->ID = ID;
    };
    /* get confidence/wieght/probability (0-1) for blob */
    virtual double  GetConfidence(int /*BlobIndex*/, CvBlob* /*pBlob*/, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL)
    {
        return 1;
    };
    virtual double GetConfidenceList(CvBlobSeq* pBlobList, IplImage* pImg, IplImage* pImgFG = NULL)
    {
        int     b,bN = pBlobList->GetBlobNum();
        double  W = 1;
        for(b=0;b<bN;++b)
        {
            CvBlob* pB = pBlobList->GetBlob(b);
            int     BI = GetBlobIndexByID(pB->ID);
            W *= GetConfidence(BI,pB,pImg,pImgFG);
        }
        return W;
    };
    virtual void UpdateBlob(int /*BlobIndex*/, CvBlob* /*pBlob*/, IplImage* /*pImg*/, IplImage* /*pImgFG*/ = NULL){};
    /* update all blob models */
    virtual void Update(IplImage* pImg, IplImage* pImgFG = NULL)
    {
        int i;
        for(i=GetBlobNum();i>0;i--)
        {
            CvBlob* pB=GetBlob(i-1);
            UpdateBlob(i-1, pB, pImg, pImgFG);
        }

    };

    /* return pinter to blob by its unique ID */
    virtual int     GetBlobIndexByID(int BlobID)
    {
        int i;
        for(i=GetBlobNum();i>0;i--)
        {
            CvBlob* pB=GetBlob(i-1);
            if(CV_BLOB_ID(pB) == BlobID) return i-1;
        }
        return -1;
    };
    /* return pinter to blob by its unique ID */
    virtual CvBlob* GetBlobByID(int BlobID){return GetBlob(GetBlobIndexByID(BlobID));};
    /* delete blob by its ID */

⌨️ 快捷键说明

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