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) 2000, 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 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's 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.
41 #include <cvstreams.h>
48 #include "Kalmanprop.h"
50 #include "Kalmanuids.h"
54 const AMOVIESETUP_MEDIATYPE sudPinTypes =
56 &MEDIATYPE_Video, // Major type
57 &MEDIASUBTYPE_RGB24 // Minor type
60 const AMOVIESETUP_PIN psudPins[] =
63 L"Input", // String pin name
64 FALSE, // Is it rendered
65 FALSE, // Is it an output
66 FALSE, // Allowed none
67 FALSE, // Allowed many
68 &CLSID_NULL, // Connects to filter
69 L"Output", // Connects to pin
71 &sudPinTypes }, // The pin details
72 { L"Output", // String pin name
73 FALSE, // Is it rendered
74 TRUE, // Is it an output
75 FALSE, // Allowed none
76 FALSE, // Allowed many
77 &CLSID_NULL, // Connects to filter
78 L"Input", // Connects to pin
80 &sudPinTypes // The pin details
85 const AMOVIESETUP_FILTER sudCKalmTrack =
87 &CLSID_CKalmTrack, // Filter CLSID
88 L"Kalman", // Filter name
89 MERIT_DO_NOT_USE, // Its merit
91 psudPins // Pin details
95 // List of class IDs and creator functions for the class factory. This
96 // provides the link between the OLE entry point in the DLL and an object
97 // being created. The class factory will call the static CreateInstance
99 CFactoryTemplate g_Templates[2] = {
103 , CKalmTrack::CreateInstance
107 { L"Kalman Property Page"
108 , &CLSID_CKalmTrackPropertyPage
109 , CKalmTrackProperties::CreateInstance }
111 int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
118 CKalmTrack::CKalmTrack(TCHAR *tszName,LPUNKNOWN punk,HRESULT *phr) :
119 CTransInPlaceFilter(tszName, punk, CLSID_CKalmTrack,phr)
124 m_params.width = 0.2f;
125 m_params.height = 0.3f;
133 Kalman = cvCreateKalman(4,4);
134 CvMat Dyn = cvMat(4,4,CV_MAT4x4_32F,Kalman->DynamMatr);
135 CvMat Mes = cvMat(4,4,CV_MAT4x4_32F,Kalman->MeasurementMatr);
136 CvMat PNC = cvMat(4,4,CV_MAT4x4_32F,Kalman->PNCovariance);
137 CvMat MNC = cvMat(4,4,CV_MAT4x4_32F,Kalman->MNCovariance);
138 CvMat PriErr = cvMat(4,4,CV_MAT4x4_32F,Kalman->PriorErrorCovariance);
139 CvMat PostErr = cvMat(4,4,CV_MAT4x4_32F,Kalman->PosterErrorCovariance);
140 CvMat PriState = cvMat(4,1,CV_MAT4x1_32F,Kalman->PriorState);
141 cvmSetIdentity(&PNC);
142 cvmSetIdentity(&PriErr);
143 cvmSetIdentity(&PostErr);
145 cvmSetZero(&PriState);
146 cvmSetIdentity(&Dyn);
147 cvmSet(&Dyn,0,1,0.9f);
148 cvmSet(&Dyn,2,3,0.9f);
149 cvmSetIdentity(&Mes);
150 MVect = cvMat(4,1,CV_MAT4x1_32F,Measurement);
156 CKalmTrack::~CKalmTrack()
158 cvReleaseKalman(&Kalman);
164 // Provide the way for COM to create a CKalmTrack object
166 CUnknown * WINAPI CKalmTrack::CreateInstance(LPUNKNOWN punk, HRESULT *phr) {
168 CKalmTrack *pNewObject = new CKalmTrack(NAME("Kalman"), punk, phr);
169 if (pNewObject == NULL) {
170 *phr = E_OUTOFMEMORY;
178 // NonDelegatingQueryInterface
180 // Reveals ICKalmTrack and ISpecifyPropertyPages
182 STDMETHODIMP CKalmTrack::NonDelegatingQueryInterface(REFIID riid, void **ppv)
184 CheckPointer(ppv,E_POINTER);
186 if (riid == IID_ICKalmTrack) {
187 return GetInterface((ICKalmTrack *) this, ppv);
188 } else if (riid == IID_ISpecifyPropertyPages) {
189 return GetInterface((ISpecifyPropertyPages *) this, ppv);
191 return CTransInPlaceFilter::NonDelegatingQueryInterface(riid, ppv);
194 } // NonDelegatingQueryInterface
197 void CKalmTrack::ApplyCamShift( CvImage* image, bool initialize )
202 m_cCamShift.set_hist_dims( 1, &bins );
203 m_cCamShift.set_hist_bin_range( 0, 1, 180 );
204 m_cCamShift.set_min_ch_val( 1, m_params.Smin );
205 m_cCamShift.set_max_ch_val( 1, 255 );
206 m_cCamShift.set_min_ch_val( 2, m_params.Vmin );
207 m_cCamShift.set_max_ch_val( 2, 255 );
209 cvGetImageRawData( image, 0, 0, &size );
211 if( m_object.x < 0 ) m_object.x = 0;
212 if( m_object.x > size.width - m_object.width - 1 )
213 m_object.x = size.width - m_object.width - 1;
215 if( m_object.y < 0 ) m_object.y = 0;
216 if( m_object.y > size.height - m_object.height - 1 )
217 m_object.y = size.height - m_object.height - 1;
219 m_cCamShift.set_window(m_object);
223 m_cCamShift.reset_histogram();
224 m_cCamShift.update_histogram( image );
227 m_cCamShift.track_object( image );
228 m_object = m_cCamShift.get_window();
230 Measurement[0] = (float)m_object.x + m_object.width*0.5f;
231 Measurement[1] = initialize ? 0 : Measurement[0] - m_Old.x;
232 Measurement[2] = (float)m_object.y + m_object.height*0.5f;
233 Measurement[3] = initialize ? 0 : Measurement[2] - m_Old.y;
235 m_Old.x = cvRound( Measurement[0] );
236 m_Old.y = cvRound( Measurement[2] );
237 cvKalmanUpdateByMeasurement(Kalman,&MVect);
241 void CKalmTrack::CheckBackProject( CvImage* image )
243 if( m_params.view != 0 )
245 IplImage* src = m_cCamShift.get_back_project();
246 if( src && src->imageData && image )
248 cvCvtColor( src, image, CV_GRAY2BGR );
255 // Transform the sample 'in place'
257 HRESULT CKalmTrack::Transform(IMediaSample *pSample)
262 pSample->GetPointer(&pData);
264 AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType();
265 VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) pType->pbFormat;
267 // Get the image properties from the BITMAPINFOHEADER
268 CvSize size = cvSize( pvi->bmiHeader.biWidth, pvi->bmiHeader.biHeight );
269 int stride = (size.width * 3 + 3) & -4;
271 cvInitImageHeader( &image, size, IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 );
272 cvSetImageData( &image, pData,stride );
274 if(IsTracking == false)
280 p1.x = cvRound( size.width * m_params.x );
281 p1.y = cvRound( size.height * m_params.y );
283 p2.x = cvRound( size.width * (m_params.x + m_params.width));
284 p2.y = cvRound( size.height * (m_params.y + m_params.height));
286 CheckBackProject( &image );
288 cvRectangle( &image, p1, p2, -1, 1 );
292 m_object.x = cvRound( size.width * m_params.x );
293 m_object.y = cvRound( size.height * m_params.y );
294 m_object.width = cvRound( size.width * m_params.width );
295 m_object.height = cvRound( size.height * m_params.height );
296 ApplyCamShift( &image, true );
298 CheckBackProject( &image );
305 cvKalmanUpdateByTime(Kalman);
306 m_object.x = cvRound( Kalman->PriorState[0]-m_object.width*0.5);
307 m_object.y = cvRound( Kalman->PriorState[2]-m_object.height*0.5 );
309 ApplyCamShift( &image, false );
311 CheckBackProject( &image );
314 cvPoint( m_object.x, m_object.y ),
315 cvPoint( m_object.x + m_object.width, m_object.y + m_object.height ),
318 Rectang(&image,m_Indicat1,-1);
321 m_X.width=50*m_Old.x/size.width;
323 Rectang(&image,m_X,CV_RGB(0,0,255));
327 m_Y.height = 50*m_Old.y/size.height;
328 Rectang(&image,m_Y,CV_RGB(255,0,0));
330 m_Indicat2.y = size.height-50;
331 m_Indicat2.width = 50;
332 m_Indicat2.height = 50;
333 Rectang(&image,m_Indicat2,-1);
334 float Norm = cvSqrt(Measurement[1]*Measurement[1]+Measurement[3]*Measurement[3]);
335 int VXNorm = (fabs(Measurement[1])>5)?(int)(12*Measurement[1]/Norm):0;
336 int VYNorm = (fabs(Measurement[3])>5)?(int)(12*Measurement[3]/Norm):0;
337 CvPoint pp1 = {25,size.height-25};
338 CvPoint pp2 = {25+VXNorm,size.height-25+VYNorm};
339 cvLine(&image,pp1,pp2,CV_RGB(0,0,0),3);
340 /*CvPoint pp1 = {25,size.height-25};
341 double angle = atan2( Measurement[3], Measurement[1] );
342 CvPoint pp2 = {cvRound(25+12*cos(angle)),cvRound(size.height-25-12*sin(angle))};
343 cvLine(&image,pp1,pp2,0,3);*/
346 cvSetImageData( &image, 0, 0 );
355 // Check the input type is OK, return an error otherwise
357 HRESULT CKalmTrack::CheckInputType(const CMediaType *mtIn)
359 // Check this is a VIDEOINFO type
361 if (*mtIn->FormatType() != FORMAT_VideoInfo)
365 if ((IsEqualGUID(*mtIn->Type(), MEDIATYPE_Video))
366 && (IsEqualGUID(*mtIn->Subtype(), MEDIASUBTYPE_RGB24)))return NOERROR;
376 // To be able to transform the formats must be identical
379 HRESULT CKalmTrack::CheckTransform(const CMediaType *mtIn,const CMediaType *mtOut)
382 if (FAILED(hr = CheckInputType(mtIn))) {
386 // format must be a VIDEOINFOHEADER
387 if (*mtOut->FormatType() != FORMAT_VideoInfo) {
391 // formats must be big enough
392 if (mtIn->FormatLength() < sizeof(VIDEOINFOHEADER) ||
393 mtOut->FormatLength() < sizeof(VIDEOINFOHEADER))
396 VIDEOINFO *pInput = (VIDEOINFO *) mtIn->Format();
397 VIDEOINFO *pOutput = (VIDEOINFO *) mtOut->Format();
398 if (memcmp(&pInput->bmiHeader,&pOutput->bmiHeader,sizeof(BITMAPINFOHEADER)) == 0) {
409 // Tell the output pin's allocator what size buffers we
410 // require. Can only do this when the input is connected
412 HRESULT CKalmTrack::DecideBufferSize(IMemAllocator *pAlloc,ALLOCATOR_PROPERTIES *pProperties)
414 // Is the input pin connected
416 if (m_pInput->IsConnected() == FALSE) {
424 } // DecideBufferSize
430 // I support one type, namely the type of the input pin
431 // We must be connected to support the single output type
433 HRESULT CKalmTrack::GetMediaType(int iPosition, CMediaType *pMediaType)
435 // Is the input pin connected
437 if (m_pInput->IsConnected() == FALSE) {
441 // This should never happen
447 // Do we have more items to offer
450 return VFW_S_NO_MORE_ITEMS;
453 *pMediaType = m_pInput->CurrentMediaType();
459 STDMETHODIMP CKalmTrack::GetState(bool* State)
466 STDMETHODIMP CKalmTrack::SetParams(CvKalmanParams* params)
468 CAutoLock cAutoLock(&m_cKalmTrackLock);
479 STDMETHODIMP CKalmTrack::GetParams(CvKalmanParams* params)
490 STDMETHODIMP CKalmTrack::StartTracking()
492 CAutoLock cAutoLock(&m_cKalmTrackLock);
498 STDMETHODIMP CKalmTrack::StopTracking()
500 CAutoLock cAutoLock(&m_cKalmTrackLock);
511 // This is the sole member of ISpecifyPropertyPages
512 // Returns the clsid's of the property pages we support
515 STDMETHODIMP CKalmTrack::GetPages(CAUUID *pPages)
518 pPages->pElems = (GUID *) CoTaskMemAlloc(sizeof(GUID));
519 if (pPages->pElems == NULL) {
520 return E_OUTOFMEMORY;
522 *(pPages->pElems) = CLSID_CKalmTrackPropertyPage;
528 CKalmTrack::Rectang(IplImage* img,CvRect Rect, int color)
530 for( int j = 0; j < Rect.height; j++ )
532 CvPoint p1={Rect.x,Rect.y+j};
533 CvPoint p2={Rect.x+Rect.width,Rect.y+j};
534 cvLine(img,p1,p2,color);
542 // Handle registration of this filter
544 STDAPI DllRegisterServer()
546 return AMovieDllRegisterServer2( TRUE );
548 } // DllRegisterServer
552 // DllUnregisterServer
554 STDAPI DllUnregisterServer()
556 return AMovieDllRegisterServer2( FALSE );
558 } // DllUnregisterServer