Update to 2.0.0 tree from current Fremantle build
[opencv] / src / cvaux / vs / enteringblobdetectionreal.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
14 //
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
17 //
18 //   * Redistribution's of source code must retain the above copyright notice,
19 //     this list of conditions and the following disclaimer.
20 //
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.
24 //
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.
27 //
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.
38 //
39 //M*/
40
41 /*
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.
47 */
48
49 #include "_cvaux.h"
50
51 /* blob detector  based on real data (groundtruth data)*/
52 class CvBlobDetectorReal:public CvBlobDetector
53 {
54 protected:
55     CvTestSeq*      m_pTestSeq;
56     CvBlobSeq       m_DetectedBlobs;
57     CvMemStorage*   m_pMem;
58
59 public:
60     CvBlobDetectorReal(CvTestSeq* pTestSeq)
61     {
62         m_pTestSeq = pTestSeq;
63         m_pMem = cvCreateMemStorage(0);
64         SetModuleName("Real");
65     }
66
67     /* Destructor of BlobDetector: */
68    ~CvBlobDetectorReal()
69     {
70         if(m_pMem) cvReleaseMemStorage(&m_pMem);
71     }   /* cvReleaseBlobDetector */
72
73     /* cvDetectNewBlobs:
74      * Return 1 and fill blob pNewBlob with
75      * blob parameters if new blob is detected:
76      */
77     int DetectNewBlob(IplImage* /*pImg*/, IplImage* /*pFGMask*/, CvBlobSeq* pNewBlobList, CvBlobSeq* /*pOldBlobList*/)
78     {
79         int         i;
80         int         TestObjNum;
81         IplImage*   pMask = NULL;
82         IplImage*   pMaskCopy = NULL;
83         CvSeq*      cnts = NULL;
84
85         if(m_pTestSeq==NULL) return 0;
86         TestObjNum = cvTestSeqGetObjectNum(m_pTestSeq);
87         pMask = cvTestSeqGetFGMask(m_pTestSeq);
88         if(pMask == NULL) return 0;
89         pMaskCopy = cvCloneImage(pMask);
90         assert(pMaskCopy);
91
92         cvClearMemStorage(m_pMem);
93         cvFindContours( pMaskCopy, m_pMem, &cnts, sizeof(CvContour), CV_RETR_EXTERNAL);
94         cvReleaseImage(&pMaskCopy);
95
96         for(i=0; i<TestObjNum; ++i)
97         {   /* Check each object: */
98             CvPoint2D32f    RealPos;
99             CvPoint2D32f    RealSize;
100             int             RealPosFlag = cvTestSeqGetObjectPos(m_pTestSeq,i,&RealPos);
101             int             RealSizeFlag = cvTestSeqGetObjectSize(m_pTestSeq,i,&RealSize);
102
103             if(!RealPosFlag) continue;
104             if(m_DetectedBlobs.GetBlobByID(i)) continue;
105
106             if(RealSizeFlag)
107             {   /* Real size is known: */
108                 float W2 = RealSize.x * 0.5f;
109                 float H2 = RealSize.y * 0.5f;
110                 if( RealPos.x > W2 && RealPos.x < (pMask->width-W2) &&
111                     RealPos.y > H2 && RealPos.y < (pMask->height-H2) )
112                 {   /* Yes!!  We found new blob, let's add it to list: */
113                     CvBlob  NewBlob;
114                     NewBlob.x = RealPos.x;
115                     NewBlob.y = RealPos.y;
116                     NewBlob.w = RealSize.x;
117                     NewBlob.h = RealSize.y;
118                     NewBlob.ID = i;
119                     m_DetectedBlobs.AddBlob(&NewBlob);
120                     pNewBlobList->AddBlob(&NewBlob);
121                 }
122             }   /* Real size is known. */
123             else
124             {
125                 CvSeq*  cnt;
126                 if(m_DetectedBlobs.GetBlobByID(i)) continue;
127
128                 for(cnt=cnts; cnt; cnt=cnt->h_next)
129                 {
130                     //CvBlob* pNewBlob = NULL;
131                     CvBlob  NewBlob;
132                     CvRect  r = cvBoundingRect( cnt );
133                     float   x = RealPos.x - r.x;
134                     float   y = RealPos.y - r.y;
135
136                     if(x<0 || x > r.width || y < 0 || y > r.height ) continue;
137
138                     if( r.x <= 1 ||
139                         r.y <= 1 ||
140                         r.x + r.width >= pMask->width - 2 ||
141                         r.y + r.height >= pMask->height - 2 ) continue;
142
143                     /* Yes!!  We found new blob, let's add it to list: */
144                     NewBlob.x = RealPos.x;
145                     NewBlob.y = RealPos.y;
146                     NewBlob.w = (float)r.width;
147                     NewBlob.h = (float)r.height;
148                     NewBlob.ID = i;
149                     m_DetectedBlobs.AddBlob(&NewBlob);
150                     pNewBlobList->AddBlob(&NewBlob);
151                 }
152             }   /* Check new blob entrance. */
153         }   /* Check next object. */
154
155         return pNewBlobList->GetBlobNum();
156
157     }   /* cvDetectNewBlob */
158
159     void Release(){delete this;};
160 };
161
162 /* Blob detector constructor: */
163 CvBlobDetector* cvCreateBlobDetectorReal(CvTestSeq* pTestSeq){return new CvBlobDetectorReal(pTestSeq);}
164
165
166
167
168