Move the sources to trunk
[opencv] / filters / Tracker3dFilter / trackers / CamShiftTracker / CamShiftTracker.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 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2002, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistributions of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 // ////////////////////////////////////////////////////////////////////////////
43 //    CamShiftTracker.cpp
44 //
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.
50 //
51 // ////////////////////////////////////////////////////////////////////////////
52
53 #include <cvstreams.h>
54 #include <comcat.h>
55 #include "CamShiftTracker.h"
56 #include "CamShiftTrackerPropertyPage.h"
57
58 static void DrawCross(IplImage *image, CvPoint point);
59
60
61 //
62 // CreateInstance
63 //
64 // Used by the DirectShow base classes to create instances
65 //
66 CUnknown *CamShiftTracker::CreateInstance(IUnknown *outer, HRESULT *phr)
67 {
68     CUnknown *punk = new CamShiftTracker(outer, phr);
69     if (punk == NULL) {
70         *phr = E_OUTOFMEMORY;
71     }
72     return punk;
73
74 }
75
76 // ////////////////////////////////////////////////////////////////////////////
77 // CamShiftTracker::CamShiftTracker()
78 //
79 // Constructor.
80 //
81 // ////////////////////////////////////////////////////////////////////////////
82 CamShiftTracker::CamShiftTracker(IUnknown *outer, HRESULT *phr)
83     : CUnknown(NAME("CamShift Tracker"), outer)
84 {
85     // set up default tracking params
86     int dims[] = { 20 };
87     set_hist_dims( 1, dims );
88     set_hist_bin_range( 0, 1, 180 );
89     set_threshold( 0 );
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
94
95     Calibrate();
96 }
97
98 // ////////////////////////////////////////////////////////////////////////////
99 // CamShiftTracker::~CamShiftTracker()
100 //
101 // Destructor.
102 //
103 // ////////////////////////////////////////////////////////////////////////////
104 CamShiftTracker::~CamShiftTracker()
105 {
106 }
107
108
109 HRESULT CamShiftTracker::NonDelegatingQueryInterface(REFIID iid, void **ppv)
110 {
111     if (iid == IID_ICamShiftTracker)
112         return GetInterface((IUnknown *)(void *)static_cast<ICamShiftTracker *>(this), ppv);
113
114     if (iid == IID_ITracker)
115         return GetInterface((IUnknown *)(void *)static_cast<ITracker *>(this), ppv);
116
117     return CUnknown::NonDelegatingQueryInterface(iid, ppv);
118 }
119
120 STDMETHODIMP CamShiftTracker::CheckFormat(IplImage *image_header)
121 {
122     if (image_header->depth != IPL_DEPTH_8U
123         || image_header->width % 4 != 0)
124     {
125         return E_FAIL;
126     }
127
128     if (image_header->nChannels == 3
129         && strncmp(image_header->colorModel, "RGB", 3) == 0)
130     {
131         return NOERROR;
132     }
133
134     return E_FAIL;
135 }
136
137 STDMETHODIMP CamShiftTracker::SetFormat(IplImage *image_header)
138 {
139     HRESULT hr = CheckFormat(image_header);
140     if (FAILED(hr))
141         return hr;
142
143     m_image_format = *image_header;
144
145     set_window(cvRect(0, 0, image_header->width, image_header->height));
146
147     return NOERROR;
148 }
149
150
151 STDMETHODIMP CamShiftTracker::GetPropertyPage(GUID *page)
152 {
153     *page = CLSID_CamShiftTrackerPropertyPage;
154     return NOERROR;
155 }
156
157 static inline bool MatchFormat(IplImage *image, IplImage *format)
158 {
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);
166
167 }
168
169 STDMETHODIMP CamShiftTracker::Calibrate()
170 {
171     m_calibrate = 30;
172     reset_histogram();
173     return NOERROR;
174 }
175
176 STDMETHODIMP CamShiftTracker::Process(IplImage *image)
177 {
178     HRESULT hr = MatchFormat(image, &m_image_format);
179     if (FAILED(hr))
180         return hr;
181
182     if (m_calibrate > 0)
183     {
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);
186         set_window(rect);
187         update_histogram(static_cast<CvImage *>(image));
188         m_calibrate--;
189     }
190     //else
191     {
192         track_object(static_cast<CvImage *>(image));
193
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);
198     }
199
200     return NOERROR;
201 }
202
203 STDMETHODIMP CamShiftTracker::GetTrackedObjects(ITracker::TrackingInfo &tracked_objects)
204 {
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);
209     return NOERROR;
210 }
211
212
213 static void DrawCross(IplImage *image, CvPoint point)
214 {
215     const int SIZE_OF_CROSS = 20;
216
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);
221
222     cvLine(image, cvPoint(point.x, Top), cvPoint(point.x, Bottom), 0xffffff);
223     cvLine(image, cvPoint(Left, point.y), cvPoint(Right, point.y), 0xffffff);
224 }
225
226
227 // Setup information
228
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
232
233 CFactoryTemplate g_Templates[] = {
234     { L"CamShift Tracker", &CLSID_CamShiftTracker, CamShiftTracker::CreateInstance },
235     { L"CamShift Tracker Property Page", &CLSID_CamShiftTrackerPropertyPage, CamShiftTrackerPropertyPage::CreateInstance }
236 };
237 int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
238
239
240 //
241 // DllRegisterServer
242 //
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()
246 {
247     HRESULT hr = AMovieDllRegisterServer2( TRUE );
248     if (FAILED(hr))
249         return hr;
250
251     ICatRegister *reg;
252     hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_ALL, IID_ICatRegister, (void **)&reg);
253     if (FAILED(hr))
254         return hr;
255
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));
262
263     reg->Release();
264
265     return NOERROR;
266 } // DllRegisterServer
267
268
269 //
270 // DllUnregisterServer
271 //
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()
275 {
276     HRESULT hr = AMovieDllRegisterServer2( FALSE );
277     if (FAILED(hr))
278         return hr;
279
280     ICatRegister *reg;
281     hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_ALL, IID_ICatRegister, (void **)&reg);
282     if (FAILED(hr))
283         return hr;
284
285     reg->UnRegisterClassImplCategories(CLSID_CamShiftTracker, 1, const_cast<GUID *>(&CATID_Trackers));
286
287     reg->Release();
288
289     return NOERROR;
290 } // DllUnregisterServer