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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The names of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
45 //////////////////////////// CvVSModule /////////////////////////////
47 CvVSModule::CvVSModule()
51 m_pModuleTypeName = NULL;
54 AddParam("DebugWnd",&m_Wnd);
57 CvVSModule::~CvVSModule()
59 CvDefParam* p = m_pParamList;
67 if(m_pModuleTypeName)free(m_pModuleTypeName);
68 if(m_pModuleName)free(m_pModuleName);
71 void CvVSModule::FreeParam(CvDefParam** pp)
73 CvDefParam* p = pp[0];
74 if(p->Str)free(p->Str);
75 if(p->pName)free(p->pName);
76 if(p->pComment)free(p->pComment);
80 CvDefParam* CvVSModule::NewParam(const char* name)
82 CvDefParam* pNew = (CvDefParam*)cvAlloc(sizeof(CvDefParam));
83 memset(pNew,0,sizeof(CvDefParam));
84 pNew->pName = strdup(name);
85 if(m_pParamList==NULL)
91 CvDefParam* p = m_pParamList;
92 for(;p->next;p=p->next) ;
98 CvDefParam* CvVSModule::GetParamPtr(int index)
100 CvDefParam* p = m_pParamList;
101 for(;index>0 && p;index--,p=p->next) ;
105 CvDefParam* CvVSModule::GetParamPtr(const char* name)
107 CvDefParam* p = m_pParamList;
110 if(cv_stricmp(p->pName,name)==0) break;
115 int CvVSModule::IsParam(const char* name)
117 return GetParamPtr(name)?1:0;
120 void CvVSModule::AddParam(const char* name, double* pAddr)
122 NewParam(name)->pDouble = pAddr;
125 void CvVSModule::AddParam(const char* name, float* pAddr)
127 NewParam(name)->pFloat=pAddr;
130 void CvVSModule::AddParam(const char* name, int* pAddr)
132 NewParam(name)->pInt=pAddr;
135 void CvVSModule::AddParam(const char* name, const char** pAddr)
137 CvDefParam* pP = NewParam(name);
138 const char* p = pAddr?pAddr[0]:NULL;
139 pP->pStr = pAddr?(char**)pAddr:&(pP->Str);
143 pP->pStr[0] = pP->Str;
147 void CvVSModule::AddParam(const char* name)
149 CvDefParam* p = NewParam(name);
150 p->pDouble = &p->Double;
153 void CvVSModule::CommentParam(const char* name, const char* pComment)
155 CvDefParam* p = GetParamPtr(name);
156 if(p)p->pComment = pComment ? strdup(pComment) : 0;
159 void CvVSModule::SetTypeName(const char* name){m_pModuleTypeName = strdup(name);}
161 void CvVSModule::SetModuleName(const char* name){m_pModuleName = strdup(name);}
163 void CvVSModule::DelParam(const char* name)
165 CvDefParam* p = m_pParamList;
166 CvDefParam* pPrev = NULL;
169 if(cv_stricmp(p->pName,name)==0) break;
176 pPrev->next = p->next;
180 m_pParamList = p->next;
187 const char* CvVSModule::GetParamName(int index)
189 CvDefParam* p = GetParamPtr(index);
190 return p?p->pName:NULL;
192 const char* CvVSModule::GetParamComment(const char* name)
194 CvDefParam* p = GetParamPtr(name);
195 if(p && p->pComment) return p->pComment;
198 double CvVSModule::GetParam(const char* name)
200 CvDefParam* p = GetParamPtr(name);
203 if(p->pDouble) return p->pDouble[0];
204 if(p->pFloat) return p->pFloat[0];
205 if(p->pInt) return p->pInt[0];
210 const char* CvVSModule::GetParamStr(const char* name)
212 CvDefParam* p = GetParamPtr(name);
213 return p?p->Str:NULL;
215 void CvVSModule::SetParam(const char* name, double val)
217 CvDefParam* p = m_pParamList;
220 if(cv_stricmp(p->pName,name) != 0) continue;
221 if(p->pDouble)p->pDouble[0] = val;
222 if(p->pFloat)p->pFloat[0] = (float)val;
223 if(p->pInt)p->pInt[0] = cvRound(val);
226 void CvVSModule::SetParamStr(const char* name, const char* str)
228 CvDefParam* p = m_pParamList;
231 if(cv_stricmp(p->pName,name) != 0) continue;
234 if(p->Str)free(p->Str);
236 if(str)p->Str = strdup(str);
240 /* Convert to double and set: */
241 if(str) SetParam(name,atof(str));
244 void CvVSModule::TransferParamsFromChild(CvVSModule* pM, const char* prefix)
247 const char* FN = NULL;
251 const char* N = pM->GetParamName(i);
263 if(pM->GetParamStr(N))
265 AddParam(FN,(const char**)NULL);
272 if(pM->GetParamStr(N))
274 const char* val = pM->GetParamStr(N);
279 double val = pM->GetParam(N);
282 CommentParam(FN, pM->GetParamComment(N));
283 }/* transfer next param */
284 }/* Transfer params */
286 void CvVSModule::TransferParamsToChild(CvVSModule* pM, char* prefix)
292 const char* N = pM->GetParamName(i);
308 pM->SetParamStr(N,GetParamStr(tmp));
310 pM->SetParam(N,GetParam(tmp));
312 }/* Transfer next parameter */
314 }/* Transfer params */
316 void CvVSModule::ParamUpdate(){}
318 const char* CvVSModule::GetTypeName()
320 return m_pModuleTypeName;
323 int CvVSModule::IsModuleTypeName(const char* name)
325 return m_pModuleTypeName?(cv_stricmp(m_pModuleTypeName,name)==0):0;
328 char* CvVSModule::GetModuleName()
330 return m_pModuleName;
333 int CvVSModule::IsModuleName(const char* name)
335 return m_pModuleName?(cv_stricmp(m_pModuleName,name)==0):0;
338 void CvVSModule::SetNickName(const char* pStr)
346 m_pNickName = strdup(pStr);
349 const char* CvVSModule::GetNickName()
351 return m_pNickName ? m_pNickName : "unknown";
354 void CvVSModule::SaveState(CvFileStorage*)
358 void CvVSModule::LoadState(CvFileStorage*, CvFileNode*)
362 /////////////////////////////////////////////////////////////////////
364 void cvWriteStruct(CvFileStorage* fs, const char* name, void* addr, const char* desc, int num)
366 cvStartWriteStruct(fs,name,CV_NODE_SEQ|CV_NODE_FLOW);
367 cvWriteRawData(fs,addr,num,desc);
368 cvEndWriteStruct(fs);
371 void cvReadStructByName(CvFileStorage* fs, CvFileNode* node, const char* name, void* addr, const char* desc)
373 CvFileNode* pSeqNode = cvGetFileNodeByName(fs, node, name);
376 printf("WARNING!!! Can't read structure %s\n",name);
380 if(CV_NODE_IS_SEQ(pSeqNode->tag))
382 cvReadRawData( fs, pSeqNode, addr, desc );
386 printf("WARNING!!! Structure %s is not sequence and can not be read\n",name);
391 ////////////////////////////// CvFGDetector ///////////////////////////////////////////
393 CvFGDetector::CvFGDetector()
395 SetTypeName("FGDetector");
398 void cvReleaseFGDetector(CvFGDetector** ppT )
404 ///////////////////////////// CvBlobSeq ///////////////////////////////////////////////
406 CvBlobTrackSeq::CvBlobTrackSeq(int TrackSize)
408 m_pMem = cvCreateMemStorage();
409 m_pSeq = cvCreateSeq(0,sizeof(CvSeq),TrackSize,m_pMem);
412 CvBlobTrackSeq::~CvBlobTrackSeq()
415 cvReleaseMemStorage(&m_pMem);
418 CvBlobTrack* CvBlobTrackSeq::GetBlobTrack(int TrackIndex)
420 return (CvBlobTrack*)cvGetSeqElem(m_pSeq,TrackIndex);
423 CvBlobTrack* CvBlobTrackSeq::GetBlobTrackByID(int TrackID)
426 for(i=0; i<m_pSeq->total; ++i)
428 CvBlobTrack* pP = GetBlobTrack(i);
429 if(pP && pP->TrackID == TrackID)
435 void CvBlobTrackSeq::DelBlobTrack(int TrackIndex)
437 CvBlobTrack* pP = GetBlobTrack(TrackIndex);
438 if(pP && pP->pBlobSeq) delete pP->pBlobSeq;
439 cvSeqRemove(m_pSeq,TrackIndex);
442 void CvBlobTrackSeq::DelBlobTrackByID(int TrackID)
445 for(i=0; i<m_pSeq->total; ++i)
447 CvBlobTrack* pP = GetBlobTrack(i);
448 if(TrackID == pP->TrackID)
456 void CvBlobTrackSeq::Clear()
459 for(i=GetBlobTrackNum();i>0;i--)
466 void CvBlobTrackSeq::AddBlobTrack(int TrackID, int StartFrame)
470 N.StartFrame = StartFrame;
471 N.pBlobSeq = new CvBlobSeq;
472 cvSeqPush(m_pSeq,&N);
475 int CvBlobTrackSeq::GetBlobTrackNum()
477 return m_pSeq->total;
480 void cvReleaseBlobDetector(CvBlobDetector** ppBD)
487 ///////////////////////////////////// CvObjectDetector /////////////////////////////////
489 CvObjectDetector::CvObjectDetector( const char* /*detector_file_name*/ )
493 CvObjectDetector::~CvObjectDetector()
498 * Release the current detector and load new detector from file
499 * (if detector_file_name is not 0)
500 * Return true on success:
502 bool CvObjectDetector::Load( const char* /*detector_file_name*/ )
507 /* Return min detector window size: */
508 CvSize CvObjectDetector::GetMinWindowSize() const
513 /* Return max border: */
514 int CvObjectDetector::GetMaxBorderSize() const
520 * Detect the object on the image and push the detected
521 * blobs into <detected_blob_seq> which must be the sequence of <CvDetectedBlob>s
523 void CvObjectDetector::Detect( const CvArr* /*img*/,
524 /* out */ CvBlobSeq* /*detected_blob_seq*/ )
528 //////////////////////////////// CvBlobTracker //////////////////////////////////////
530 CvBlobTracker::CvBlobTracker(){SetTypeName("BlobTracker");}
532 /* Process one blob (for multi hypothesis tracing): */
533 void CvBlobTracker::ProcessBlob(int BlobIndex, CvBlob* pBlob, IplImage* /*pImg*/, IplImage* /*pImgFG*/)
539 pB = GetBlob(BlobIndex);
545 /* Get confidence/wieght/probability (0-1) for blob: */
546 double CvBlobTracker::GetConfidence(int /*BlobIndex*/, CvBlob* /*pBlob*/, IplImage* /*pImg*/, IplImage* /*pImgFG*/)
551 double CvBlobTracker::GetConfidenceList(CvBlobSeq* pBlobList, IplImage* pImg, IplImage* pImgFG)
553 int b,bN = pBlobList->GetBlobNum();
557 CvBlob* pB = pBlobList->GetBlob(b);
558 int BI = GetBlobIndexByID(pB->ID);
559 W *= GetConfidence(BI,pB,pImg,pImgFG);
564 void CvBlobTracker::UpdateBlob(int /*BlobIndex*/, CvBlob* /*pBlob*/, IplImage* /*pImg*/, IplImage* /*pImgFG*/)
568 /* Update all blob models: */
569 void CvBlobTracker::Update(IplImage* pImg, IplImage* pImgFG)
572 for(i=GetBlobNum();i>0;i--)
574 CvBlob* pB=GetBlob(i-1);
575 UpdateBlob(i-1, pB, pImg, pImgFG);
579 /* Return pointer to blob by its unique ID: */
580 int CvBlobTracker::GetBlobIndexByID(int BlobID)
583 for(i=GetBlobNum();i>0;i--)
585 CvBlob* pB=GetBlob(i-1);
586 if(CV_BLOB_ID(pB) == BlobID) return i-1;
591 /* Return pointer to blob by its unique ID: */
592 CvBlob* CvBlobTracker::GetBlobByID(int BlobID)
594 return GetBlob(GetBlobIndexByID(BlobID));
597 /* Delete blob by its ID: */
598 void CvBlobTracker::DelBlobByID(int BlobID)
600 DelBlob(GetBlobIndexByID(BlobID));
603 /* Set new parameters for specified (by index) blob: */
604 void CvBlobTracker::SetBlob(int /*BlobIndex*/, CvBlob* /*pBlob*/)
608 /* Set new parameters for specified (by ID) blob: */
609 void CvBlobTracker::SetBlobByID(int BlobID, CvBlob* pBlob)
611 SetBlob(GetBlobIndexByID(BlobID),pBlob);
614 /* =============== MULTI HYPOTHESIS INTERFACE ================== */
616 /* Return number of position hyposetis of currently tracked blob: */
617 int CvBlobTracker::GetBlobHypNum(int /*BlobIdx*/)
622 /* Return pointer to specified blob hypothesis by index blob: */
623 CvBlob* CvBlobTracker::GetBlobHyp(int BlobIndex, int /*hypothesis*/)
625 return GetBlob(BlobIndex);
628 /* Set new parameters for specified (by index) blob hyp
629 * (can be called several times for each hyp ):
631 void CvBlobTracker::SetBlobHyp(int /*BlobIndex*/, CvBlob* /*pBlob*/)
635 void cvReleaseBlobTracker(CvBlobTracker**ppT )