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

📄 enteringblobdetection.cpp

📁 Program use openCV to demo edge detection (algorithm Gradient).
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                    pN[0] = pP[0];
                    pP[0] = temp;
                }
            }
            /* copy only first 10 blobs */
            for(i=0;i<MIN(N,10);++i)
            {
                m_pBlobLists[EBD_FRAME_NUM-1]->AddBlob(Blobs.GetBlob(i));
            }
        }/* sort blobs by size */
        cvReleaseMemStorage(&storage);
    }/* create Blobs */

    /* analize blob list to find best blob trajectory */
    {/* analize blob list to find best blob trajectory */
        int     Count = 0;
        int     pBLIndex[EBD_FRAME_NUM];
        int     pBL_BEST[EBD_FRAME_NUM];
        int     i;
        int     finish = 0;
        double  BestError = -1;
        int     Good = 1;
        for(i=0;i<EBD_FRAME_NUM;++i)
        {
            pBLIndex[i] = 0;
            pBL_BEST[i] = 0;
        }
        
        /* check configuration exist */
        for(i=0;Good && (i<EBD_FRAME_NUM);++i)
            if(m_pBlobLists[i] == NULL || m_pBlobLists[i]->GetBlobNum() == 0)
                Good = 0;

        if(Good)
        do{/* for each configuration */
            CvBlob* pBL[EBD_FRAME_NUM];
            int     Good = 1;
            double  Error = 0;
            CvBlob* pBNew = m_pBlobLists[EBD_FRAME_NUM-1]->GetBlob(pBLIndex[EBD_FRAME_NUM-1]);
            for(i=0;i<EBD_FRAME_NUM;++i)pBL[i] = m_pBlobLists[i]->GetBlob(pBLIndex[i]);

            Count++;

            /* check intersection last blob with existed */
            if(Good && pOldBlobList)
            { /* check intersection last blob with existed */
                int     k;
                for(k=pOldBlobList->GetBlobNum();k>0;--k)
                {
                    CvBlob* pBOld = pOldBlobList->GetBlob(k-1);
                    if((fabs(pBOld->x-pBNew->x) < (CV_BLOB_RX(pBOld)+CV_BLOB_RX(pBNew))) &&
                       (fabs(pBOld->y-pBNew->y) < (CV_BLOB_RY(pBOld)+CV_BLOB_RY(pBNew))))
                        Good = 0;
                }
            }/* check intersection last blob with existed */

            /* check distance to image border */
            if(Good)
            { /* check distance to image border */
                CvBlob*  pB = pBNew;
                float    dx = MIN(pB->x,S.width-pB->x)/CV_BLOB_RX(pB);
                float    dy = MIN(pB->y,S.height-pB->y)/CV_BLOB_RY(pB);

                if(dx < 1.1 || dy < 1.1) Good = 0;
            }/* check distance to image border */

            /* check uniform moveing */
            if(Good)
            {/* check uniform moveing */
                int     N = EBD_FRAME_NUM;
                float   sum[2] = {0,0};   
                float   jsum[2] = {0,0};
                float   a[2],b[2]; /* estimated parameters of moving x(t) = a*t+b*/
                int     j;
                
                for(j=0;j<N;++j)
                {
                    float   x = pBL[j]->x;
                    float   y = pBL[j]->y;
                    sum[0] += x;
                    jsum[0] += j*x;
                    sum[1] += y;
                    jsum[1] += j*y;
                }
                a[0] = 6*((1-N)*sum[0]+2*jsum[0])/(N*(N*N-1));
                b[0] = -2*((1-2*N)*sum[0]+3*jsum[0])/(N*(N+1));
                a[1] = 6*((1-N)*sum[1]+2*jsum[1])/(N*(N*N-1));
                b[1] = -2*((1-2*N)*sum[1]+3*jsum[1])/(N*(N+1));
                for(j=0;j<N;++j)
                {
                    Error += 
                        pow(a[0]*j+b[0]-pBL[j]->x,2)+
                        pow(a[1]*j+b[1]-pBL[j]->y,2);
                }
                Error = sqrt(Error/N);
                if( Error > S.width*0.01 || 
                    fabs(a[0])>S.width*0.1 ||
                    fabs(a[1])>S.height*0.1)
                    Good = 0;
            }/* check configuration */

            
            /* new best trajectory */
            if(Good && (BestError == -1 || BestError > Error))
            {/* new best trajectory */
                for(i=0;i<EBD_FRAME_NUM;++i)
                {
                    pBL_BEST[i] = pBLIndex[i];
                }
                BestError = Error;
            }/* new best trajectory */

            /* set next configuration */
            for(i=0;i<EBD_FRAME_NUM;++i)
            {
                pBLIndex[i]++;
                if(pBLIndex[i] != m_pBlobLists[i]->GetBlobNum()) break;
                pBLIndex[i]=0;
            }/* next time shift */
            if(i==EBD_FRAME_NUM)finish=1;
        }while(!finish); /* check next time configuration of connected components */

        #if 0
        {/**/
            printf("BlobDetector configurations = %d [",Count);
            int i;
            for(i=0;i<EBD_FRAME_NUM;++i)
            {
                printf("%d,",m_pBlobLists[i]?m_pBlobLists[i]->GetBlobNum():0);
            }
            printf("]\n");
        
        }
        #endif

        if(BestError != -1)
        {/* put new blob to output and delete from blob list */
            CvBlob* pNewBlob = m_pBlobLists[EBD_FRAME_NUM-1]->GetBlob(pBL_BEST[EBD_FRAME_NUM-1]);
            pNewBlobList->AddBlob(pNewBlob);
            for(i=0;i<EBD_FRAME_NUM;++i)
            {/* remove blob from each list */
                m_pBlobLists[i]->DelBlob(pBL_BEST[i]);
            }/* remove blob from each list */
            res = 1;
        }/* put new blob to output and delete from blob list */
    }/* analize blod list to find best blob trajectory */

    return res;
}/* cvDetectNewBlob */




/* simple blob detector2  */
/* numer of successive frame to analyse */
#define SEQ_SIZE_MAX    30
#define SEQ_NUM         1000
typedef struct
{
    int     size;
    CvBlob* pBlobs[SEQ_SIZE_MAX];
} DefSeq;
class CvBlobDetectorCC:public CvBlobDetector
{
public:
    CvBlobDetectorCC();
    ~CvBlobDetectorCC();
    int DetectNewBlob(IplImage* pImg, IplImage* pFGMask, CvBlobSeq* pNewBlobList, CvBlobSeq* pOldBlobList);
    void Release(){delete this;};
    virtual void ParamUpdate()
    {
        if(SEQ_SIZE<1)SEQ_SIZE=1;
        if(SEQ_SIZE>SEQ_SIZE_MAX)SEQ_SIZE=SEQ_SIZE_MAX;

#ifdef USE_OBJECT_DETECTOR
        if( m_param_split_detector_file_name )
        {
            m_split_detector = new CvObjectDetector();
            if( !m_split_detector->Load( m_param_split_detector_file_name ) )
            {
                delete m_split_detector;
                m_split_detector = 0;
            }
            else
            {
                m_min_window_size = m_split_detector->GetMinWindowSize();
                m_max_border = m_split_detector->GetMaxBorderSize();
            }
        }
#endif
    }
private:
    /* lists of connected components detected on previouse frames */
    CvBlobSeq*      m_pBlobLists[SEQ_SIZE_MAX];
    DefSeq          m_TrackSeq[SEQ_NUM];
    int             m_TrackNum;
    float           m_HMin;
    float           m_WMin;
    float           m_MinDistToBorder;
    int             m_Clastering;
    int             SEQ_SIZE;

    /* if not 0 then the detector is loaded from the specified file
       and it is applied for splitting blobs which actually correspond
       to groups of objects */ 
    char*           m_param_split_detector_file_name;
    float           m_param_roi_scale;
    int             m_param_only_roi;

    CvObjectDetector* m_split_detector;
    CvSize          m_min_window_size;
    int             m_max_border;

    CvBlobSeq       m_detected_blob_seq;
    CvSeq*          m_roi_seq;

    CvBlobSeq       m_debug_blob_seq;
};

/* Blob detector creator (sole interface function for this file) */
CvBlobDetector* cvCreateBlobDetectorCC(){return new CvBlobDetectorCC;}

/* Constructor of BlobDetector */
CvBlobDetectorCC::CvBlobDetectorCC() :
    m_split_detector(0),
    m_detected_blob_seq(sizeof(CvDetectedBlob)),
    m_roi_seq(0),
    m_debug_blob_seq(sizeof(CvDetectedBlob))
{
    /*CvDrawShape shapes[] = 
    {
        { CvDrawShape::RECT,    {{255,255,255}} },
        { CvDrawShape::RECT,    {{0,0,255}} },
        { CvDrawShape::ELLIPSE, {{0,255,0}} }
    };
    int num_shapes = sizeof(shapes) / sizeof(shapes[0]);*/

    int i = 0;
    SEQ_SIZE = 10;
    AddParam("Latency",&SEQ_SIZE);
    for(i=0;i<SEQ_SIZE_MAX;++i)m_pBlobLists[i] = NULL;
    for(i=0;i<SEQ_NUM;++i)m_TrackSeq[i].size = 0;
    m_TrackNum = 0;

    m_HMin = 0.02f;
    m_WMin = 0.01f;
    AddParam("HMin",&m_HMin);
    AddParam("WMin",&m_WMin);
    m_MinDistToBorder = 1.1f;
    AddParam("MinDistToBorder",&m_MinDistToBorder);
    CommentParam("MinDistToBorder","Minimal allowed distance from blob center to image border in blob sizes");
    
    m_Clastering=1;
    AddParam("Clastering",&m_Clastering);
    CommentParam("Clastering","Minimal allowed distance from blob center to image border in blob sizes");

    m_param_split_detector_file_name = 0;
#ifdef USE_OBJECT_DETECTOR
    AddParam("Detector", &m_param_split_detector_file_name);
    CommentParam("Detector", "Detector file name");
#endif

    m_param_roi_scale = 1.5F;
    AddParam("ROIScale", &m_param_roi_scale);
    CommentParam("ROIScale", "Determines the size of search window around a blob");

    m_param_only_roi = 1;
    AddParam("OnlyROI", &m_param_only_roi);
    CommentParam("OnlyROI", "Shows the whole debug image (0) or only ROIs where the detector was applied (1)");

    m_min_window_size = cvSize(0,0);
    m_max_border = 0;
    m_roi_seq = cvCreateSeq( 0, sizeof(*m_roi_seq), sizeof(CvRect), cvCreateMemStorage() );
    
    SetModuleName("BD_CC");

}

/* destructor of BlobDetector*/
CvBlobDetectorCC::~CvBlobDetectorCC()
{
    int i;
    for(i=0;i<SEQ_SIZE_MAX;++i)
    {
        if(m_pBlobLists[i]) 
            delete m_pBlobLists[i];
    }

    if( m_roi_seq )
    {
        cvReleaseMemStorage( &m_roi_seq->storage );
        m_roi_seq = 0;
    }
    //cvDestroyWindow( "EnteringBlobDetectionDebug" );
}/* cvReleaseBlobDetector */


/* cvDetectNewBlobs return 1 and fill blob pNewBlob by blob parameters if new blob is detected */
int CvBlobDetectorCC::DetectNewBlob(IplImage* /*pImg*/, IplImage* pFGMask, CvBlobSeq* pNewBlobList, CvBlobSeq* pOldBlobList)
{
    int         res = 0;
    CvSize      S = cvSize(pFGMask->width,pFGMask->height);
    
    /* shift blob list */
    {
        int     i;
        if(m_pBlobLists[SEQ_SIZE-1]) delete m_pBlobLists[SEQ_SIZE-1];
        for(i=SEQ_SIZE-1;i>0;--i)m_pBlobLists[i]=m_pBlobLists[i-1];
        m_pBlobLists[0] = new CvBlobSeq;
    }/* shift blob list */
    
    /* create contours and add new blobs to blob list */
    {/* create blobs */
        CvBlobSeq       Blobs;
        CvMemStorage*   storage = cvCreateMemStorage();

        if(m_Clastering)
        {/* glue contours */
            cvFindBlobsByCCClasters(pFGMask, &Blobs, storage );
        }/* glue contours */
        else
        { /**/
            IplImage*       pIB = cvCloneImage(pFGMask);
            CvSeq*          cnts = NULL;
            CvSeq*          cnt = NULL;
            cvThreshold(pIB,pIB,128,255,CV_THRESH_BINARY);
            cvFindContours(pIB,storage, &cnts, sizeof(CvContour), CV_RETR_EXTERNAL);
            /* process each contours*/
            for(cnt = cnts;cnt;cnt=cnt->h_next)
            {
                CvBlob  NewBlob;

⌨️ 快捷键说明

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