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.
40 //M*/#include "stdafx.h"
42 #include "LkTracker.h"
45 CLkTracker::CLkTracker()
47 m_tracked = m_count = 0;
51 CLkTracker::~CLkTracker()
56 void CLkTracker::Initialize( CImage& image, int max_features, double quality,
59 IplImage* img = image.GetImage();
66 for( int i = 0; i < 2; i++ )
68 m_features[i].SetSize( max_features, 10 );
69 m_frame[i].Create( image.Width(), image.Height(), 8 );
70 m_pyr[i].Create( image.Width(), image.Height(), 8 );
71 m_temp[i].Create( image.Width(), image.Height(), 32 );
73 IplImage* tmp_img = m_temp[i].GetImage();
74 tmp_img->depth = IPL_DEPTH_32F;
75 tmp_img->nChannels = 1;
78 m_status.SetSize( max_features, 10 );
79 m_matrices.SetSize( max_features*2, 20 );
81 img0 = m_frame[0].GetImage();
82 img1 = m_frame[1].GetImage();
84 m_frame[0].CopyOf( image, 0 );
86 int count = max_features;
87 cvGoodFeaturesToTrack( img0, m_temp[0].GetImage(), m_temp[1].GetImage(),
88 &m_features[0][0], &count, quality, min_distance );
92 cvFindCornerSubPix( img0, &m_features[0][0], count,
93 cvSize(5,5), cvSize(-1,-1),
94 cvTermCriteria( CV_TERMCRIT_ITER, 10, 0.1f ));
95 for( int i = 0; i < m_count; i++ )
107 void CLkTracker::PushFrame( CImage& image )
109 static int frame_idx = 0;
111 IplImage* img = image.GetImage();
113 if( m_count > 0 && img )
115 int I = m_idx, J = m_idx ^ 1;
116 int pyr0_ready = m_tracked != 0;
120 assert( m_idx == 0 || m_idx == 1 );
122 m_frame[I].Create( image.Width(), image.Height(), 8 );
123 m_frame[J].Create( image.Width(), image.Height(), 8 );
124 m_pyr[I].Create( image.Width(), image.Height(), 8 );
125 m_pyr[J].Create( image.Width(), image.Height(), 8 );
127 m_frame[J].CopyOf( image, 0 );
129 IplImage* img0 = m_frame[I].GetImage();
130 IplImage* img1 = m_frame[J].GetImage();
131 IplImage* pyr0 = m_pyr[I].GetImage();
132 IplImage* pyr1 = m_pyr[J].GetImage();
134 cvCalcOpticalFlowPyrLK( img0, img1, pyr0, pyr1,
135 &m_features[I][0], &m_features[J][0],
136 m_count, cvSize(10,10), 3,
138 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03),
139 pyr0_ready ? CV_LKFLOW_PYR_A_READY : 0 );
143 /*cvCalcAffineFlowPyrLK( img0, img1, pyr0, pyr1,
144 &m_features[I][0], &m_features[J][0],
146 m_count, cvSize(5,5), 3,
148 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,5,0.3),
149 pyr0_ready ? CV_LKFLOW_PYR_A_READY : 0 );*/
152 for( i = 0, k = 0; i < count; i++ )
156 m_features[J][k++] = m_features[J][i];
170 cvGoodFeaturesToTrack( img.GetImage(), m_temp[0].GetImage(),
171 m_temp[1].GetImage(),
172 &m_features[0][0], &m_count, 0.1, 10 );
178 void CLkTracker::RemovePoint( int index )
180 if( (unsigned)index < (unsigned)m_count )
182 m_features[m_idx].RemoveAt( index );
183 m_features[m_idx^1].RemoveAt( index );
184 m_matrices.RemoveAt( m_count*2 - 1 );
185 m_matrices.RemoveAt( m_count*2 - 2 );
191 void CLkTracker::AddPoint( CPoint p, CImage& image )
193 CvPoint2D32f pt = AdjustPoint( p, image );
195 if( m_count < m_features[m_idx].GetSize() )
197 m_features[m_idx][m_count] = pt;
201 m_features[m_idx].Add(pt);
202 m_features[m_idx^1].Add(pt);
211 void CLkTracker::MovePoint( int index, CPoint p, CImage& image )
213 if((unsigned)index < (unsigned)m_count )
215 CvPoint2D32f pt = AdjustPoint( p, image );
216 m_features[m_idx][index] = pt;
221 CvPoint2D32f CLkTracker::AdjustPoint( CPoint p, CImage& image )
226 cvFindCornerSubPix( image.GetImage(), &pt, 1, cvSize(5,5), cvSize(-1,-1),
227 cvTermCriteria( CV_TERMCRIT_ITER, 10, 0.1f ));