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
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2002, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistributions of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistributions in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 // ////////////////////////////////////////////////////////////////////////////
43 // CamShiftTracker.cpp
45 // This is a COM wrapper for the OpenCV CamShift tracker.
46 // It implements ITracker so that CamShift can be used by the 3d Tracker filter.
47 // This is intended as sample code only.
48 // A complete implementation would have additional methods (in ICamShiftTracker)
49 // to control the CamShift tracker.
51 // ////////////////////////////////////////////////////////////////////////////
53 #include <cvstreams.h>
55 #include "CamShiftTracker.h"
56 #include "CamShiftTrackerPropertyPage.h"
58 static void DrawCross(IplImage *image, CvPoint point);
64 // Used by the DirectShow base classes to create instances
66 CUnknown *CamShiftTracker::CreateInstance(IUnknown *outer, HRESULT *phr)
68 CUnknown *punk = new CamShiftTracker(outer, phr);
76 // ////////////////////////////////////////////////////////////////////////////
77 // CamShiftTracker::CamShiftTracker()
81 // ////////////////////////////////////////////////////////////////////////////
82 CamShiftTracker::CamShiftTracker(IUnknown *outer, HRESULT *phr)
83 : CUnknown(NAME("CamShift Tracker"), outer)
85 // set up default tracking params
87 set_hist_dims( 1, dims );
88 set_hist_bin_range( 0, 1, 180 );
90 set_min_ch_val( 1, 20 ); // S MIN
91 set_max_ch_val( 1, 255 ); // S MAX
92 set_min_ch_val( 2, 40 ); // V MIN
93 set_max_ch_val( 2, 240 ); // V MAX
98 // ////////////////////////////////////////////////////////////////////////////
99 // CamShiftTracker::~CamShiftTracker()
103 // ////////////////////////////////////////////////////////////////////////////
104 CamShiftTracker::~CamShiftTracker()
109 HRESULT CamShiftTracker::NonDelegatingQueryInterface(REFIID iid, void **ppv)
111 if (iid == IID_ICamShiftTracker)
112 return GetInterface((IUnknown *)(void *)static_cast<ICamShiftTracker *>(this), ppv);
114 if (iid == IID_ITracker)
115 return GetInterface((IUnknown *)(void *)static_cast<ITracker *>(this), ppv);
117 return CUnknown::NonDelegatingQueryInterface(iid, ppv);
120 STDMETHODIMP CamShiftTracker::CheckFormat(IplImage *image_header)
122 if (image_header->depth != IPL_DEPTH_8U
123 || image_header->width % 4 != 0)
128 if (image_header->nChannels == 3
129 && strncmp(image_header->colorModel, "RGB", 3) == 0)
137 STDMETHODIMP CamShiftTracker::SetFormat(IplImage *image_header)
139 HRESULT hr = CheckFormat(image_header);
143 m_image_format = *image_header;
145 set_window(cvRect(0, 0, image_header->width, image_header->height));
151 STDMETHODIMP CamShiftTracker::GetPropertyPage(GUID *page)
153 *page = CLSID_CamShiftTrackerPropertyPage;
157 static inline bool MatchFormat(IplImage *image, IplImage *format)
159 return (image->nChannels == format->nChannels
160 && image->width == format->width
161 && image->height == format->height
162 && image->depth == format->depth
163 && image->dataOrder == format->dataOrder
164 && image->origin == format->origin
165 && strncmp(image->channelSeq, format->channelSeq, 4) == 0);
169 STDMETHODIMP CamShiftTracker::Calibrate()
176 STDMETHODIMP CamShiftTracker::Process(IplImage *image)
178 HRESULT hr = MatchFormat(image, &m_image_format);
184 CvRect rect = cvRect(image->width*0.47, image->height*0.47, image->width*0.06, image->height*0.07);
185 cvRectangle(image, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), 0xffffff, 1);
187 update_histogram(static_cast<CvImage *>(image));
192 track_object(static_cast<CvImage *>(image));
194 CvRect rect = get_window();
195 CvPoint center = cvPoint(rect.x + rect.width/2, rect.y + rect.height/2);
196 DrawCross(image, center);
197 cvRectangle(image, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), 0xffffff, 1);
203 STDMETHODIMP CamShiftTracker::GetTrackedObjects(ITracker::TrackingInfo &tracked_objects)
205 CvRect rect = get_window();
206 CvPoint center = cvPoint(rect.x + rect.width/2, rect.y + rect.height/2);
207 tracked_objects.resize(1);
208 tracked_objects[0] = cv3dTracker2dTrackedObject(0, center);
213 static void DrawCross(IplImage *image, CvPoint point)
215 const int SIZE_OF_CROSS = 20;
217 int Left = MAX((int)point.x - SIZE_OF_CROSS, 0);
218 int Right = MIN((int)point.x + SIZE_OF_CROSS, (int)image->width - 1);
219 int Top = MAX((int)point.y - SIZE_OF_CROSS, 0);
220 int Bottom = MIN((int)point.y + SIZE_OF_CROSS, (int)image->height - 1);
222 cvLine(image, cvPoint(point.x, Top), cvPoint(point.x, Bottom), 0xffffff);
223 cvLine(image, cvPoint(Left, point.y), cvPoint(Right, point.y), 0xffffff);
229 // List of class IDs and creator functions for the class factory. This
230 // provides the link between the OLE entry point in the DLL and an object
231 // being created. The class factory will call the static CreateInstance
233 CFactoryTemplate g_Templates[] = {
234 { L"CamShift Tracker", &CLSID_CamShiftTracker, CamShiftTracker::CreateInstance },
235 { L"CamShift Tracker Property Page", &CLSID_CamShiftTrackerPropertyPage, CamShiftTrackerPropertyPage::CreateInstance }
237 int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
243 // Register the COM objects (the tracker and the property page).
244 // Also add the tracker to the "Video Trackers" component category.
245 STDAPI DllRegisterServer()
247 HRESULT hr = AMovieDllRegisterServer2( TRUE );
252 hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_ALL, IID_ICatRegister, (void **)®);
256 CATEGORYINFO catinfo;
257 catinfo.catid = CATID_Trackers;
258 catinfo.lcid = 0x409;
259 wcscpy(catinfo.szDescription, L"Video Trackers");
260 reg->RegisterCategories(1, &catinfo);
261 reg->RegisterClassImplCategories(CLSID_CamShiftTracker, 1, const_cast<GUID *>(&CATID_Trackers));
266 } // DllRegisterServer
270 // DllUnregisterServer
272 // Unregister the COM objects (the tracker and the property page).
273 // Also remove the tracker from the "Video Trackers" component category.
274 STDAPI DllUnregisterServer()
276 HRESULT hr = AMovieDllRegisterServer2( FALSE );
281 hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_ALL, IID_ICatRegister, (void **)®);
285 reg->UnRegisterClassImplCategories(CLSID_CamShiftTracker, 1, const_cast<GUID *>(&CATID_Trackers));
290 } // DllUnregisterServer