1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
18 // * Redistribution's of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
21 // * Redistribution's in binary form must reproduce the above copyright notice,
22 // this list of conditions and the following disclaimer in the documentation
23 // and/or other materials provided with the distribution.
25 // * The name of Intel Corporation may not be used to endorse or promote products
26 // derived from this software without specific prior written permission.
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
42 This file contain implementation of virtual interface of CvBlobDetector
43 this implementation based on simple algorithm
44 new blob is detected when several successive frames contains connected componets
45 which have uniform motion with not high speed.
46 Also separation from border and already tracked blobs are considered.
51 /* blob detector based on real data (groundtruth data)*/
52 class CvBlobDetectorReal:public CvBlobDetector
55 CvTestSeq* m_pTestSeq;
56 CvBlobSeq m_DetectedBlobs;
59 CvBlobDetectorReal(CvTestSeq* pTestSeq)
61 m_pTestSeq = pTestSeq;
62 m_pMem = cvCreateMemStorage(0);
64 /* destructor of BlobDetector*/
67 if(m_pMem) cvReleaseMemStorage(&m_pMem);
68 }/* cvReleaseBlobDetector */
70 /* cvDetectNewBlobs return 1 and fill blob pNewBlob by blob parameters if new blob is detected */
71 int DetectNewBlob(IplImage* /*pImg*/, IplImage* /*pFGMask*/, CvBlobSeq* pNewBlobList, CvBlobSeq* /*pOldBlobList*/)
75 IplImage* pMask = NULL;
76 IplImage* pMaskCopy = NULL;
79 if(m_pTestSeq==NULL) return 0;
80 TestObjNum = cvTestSeqGetObjectNum(m_pTestSeq);
81 pMask = cvTestSeqGetFGMask(m_pTestSeq);
82 if(pMask == NULL) return 0;
83 pMaskCopy = cvCloneImage(pMask);
86 cvClearMemStorage(m_pMem);
87 cvFindContours( pMaskCopy, m_pMem, &cnts, sizeof(CvContour), CV_RETR_EXTERNAL);
88 cvReleaseImage(&pMaskCopy);
90 for(i=0;i<TestObjNum;++i)
91 {/* check each object */
93 CvPoint2D32f RealSize;
94 int RealPosFlag = cvTestSeqGetObjectPos(m_pTestSeq,i,&RealPos);
95 int RealSizeFlag = cvTestSeqGetObjectSize(m_pTestSeq,i,&RealSize);
97 if(!RealPosFlag) continue;
98 if(m_DetectedBlobs.GetBlobByID(i)) continue;
101 { /* real size is know */
102 float W2 = RealSize.x * 0.5f;
103 float H2 = RealSize.y * 0.5f;
104 if( RealPos.x > W2 && RealPos.x < (pMask->width-W2) &&
105 RealPos.y > H2 && RealPos.y < (pMask->height-H2) )
106 { /* yes !! we found new blob, Let's add it to list */
108 NewBlob.x = RealPos.x;
109 NewBlob.y = RealPos.y;
110 NewBlob.w = RealSize.x;
111 NewBlob.h = RealSize.y;
113 m_DetectedBlobs.AddBlob(&NewBlob);
114 pNewBlobList->AddBlob(&NewBlob);
116 }/* real size is know */
120 if(m_DetectedBlobs.GetBlobByID(i)) continue;
121 for(cnt=cnts;cnt;cnt=cnt->h_next)
123 //CvBlob* pNewBlob = NULL;
125 CvRect r = cvBoundingRect( cnt );
126 float x = RealPos.x - r.x;
127 float y = RealPos.y - r.y;
129 if(x<0 || x > r.width || y < 0 || y > r.height ) continue;
132 r.x + r.width >= pMask->width - 2 ||
133 r.y + r.height >= pMask->height - 2 ) continue;
135 /* yes !! we found new blob, Let's add it to list */
136 NewBlob.x = RealPos.x;
137 NewBlob.y = RealPos.y;
138 NewBlob.w = (float)r.width;
139 NewBlob.h = (float)r.height;
141 m_DetectedBlobs.AddBlob(&NewBlob);
142 pNewBlobList->AddBlob(&NewBlob);
144 }/* check new blob entrance */
145 }/* check next object */
147 return pNewBlobList->GetBlobNum();
148 }/* cvDetectNewBlob */
149 void Release(){delete this;};
152 /* Blob detector creator */
153 CvBlobDetector* cvCreateBlobDetectorReal(CvTestSeq* pTestSeq){return new CvBlobDetectorReal(pTestSeq);}