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.
45 CLkTracker::CLkTracker()
47 m_tracked = m_count = 0;
52 CLkTracker::~CLkTracker()
57 void CLkTracker::Initialize( CImage& image, int max_features, double quality,
60 IplImage* img = image.GetImage();
67 for( int i = 0; i < 2; i++ )
69 //m_features[i].SetSize( max_features, 10 );
70 m_features[i].reserve(max_features+10);
72 m_frame[i].Create( image.Width(), image.Height(), 8, image.GetImage()->origin );
73 m_pyr[i].Create( image.Width(), image.Height(), 8, image.GetImage()->origin );
74 m_temp[i].Create( image.Width(), image.Height(), 32, image.GetImage()->origin );
76 IplImage* tmp_img = m_temp[i].GetImage();
77 tmp_img->depth = IPL_DEPTH_32F;
78 tmp_img->nChannels = 1;
81 //m_status.SetSize( max_features, 10 );
82 m_status.reserve(max_features+10);
83 //m_matrices.SetSize( max_features*2, 20 );
84 m_matrices.reserve(max_features+20);
86 img0 = m_frame[0].GetImage();
87 img1 = m_frame[1].GetImage();
89 m_frame[0].CopyOf( image, 0 );
91 int count = max_features;
92 cvGoodFeaturesToTrack( img0, m_temp[0].GetImage(), m_temp[1].GetImage(),
93 &m_features[0][0], &count, quality, min_distance );
97 cvFindCornerSubPix( img0, &m_features[0][0], count,
98 cvSize(5,5), cvSize(-1,-1),
99 cvTermCriteria( CV_TERMCRIT_ITER, 10, 0.1f ));
100 for( int i = 0; i < m_count; i++ )
113 void CLkTracker::PushFrame( CImage& image )
115 static int frame_idx = 0;
117 IplImage* img = image.GetImage();
119 if( m_count > 0 && img )
121 int I = m_idx, J = m_idx ^ 1;
122 int pyr0_ready = m_tracked != 0;
126 assert( m_idx == 0 || m_idx == 1 );
128 m_frame[I].Create( image.Width(), image.Height(), 8, image.GetImage()->origin );
129 m_frame[J].Create( image.Width(), image.Height(), 8, image.GetImage()->origin );
130 m_pyr[I].Create( image.Width(), image.Height(), 8, image.GetImage()->origin );
131 m_pyr[J].Create( image.Width(), image.Height(), 8, image.GetImage()->origin );
133 m_frame[J].CopyOf( image, 0 );
135 IplImage* img0 = m_frame[I].GetImage();
136 IplImage* img1 = m_frame[J].GetImage();
137 IplImage* pyr0 = m_pyr[I].GetImage();
138 IplImage* pyr1 = m_pyr[J].GetImage();
140 cvCalcOpticalFlowPyrLK( img0, img1, pyr0, pyr1,
141 &m_features[I][0], &m_features[J][0],
142 m_count, cvSize(10,10), 3,
144 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03),
145 pyr0_ready ? CV_LKFLOW_PYR_A_READY : 0 );
149 /*cvCalcAffineFlowPyrLK( img0, img1, pyr0, pyr1,
150 &m_features[I][0], &m_features[J][0],
152 m_count, cvSize(5,5), 3,
154 cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,5,0.3),
155 pyr0_ready ? CV_LKFLOW_PYR_A_READY : 0 );*/
158 for( i = 0, k = 0; i < count; i++ )
162 m_features[J][k++] = m_features[J][i];
176 cvGoodFeaturesToTrack( img.GetImage(), m_temp[0].GetImage(),
177 m_temp[1].GetImage(),
178 &m_features[0][0], &m_count, 0.1, 10 );
184 void CLkTracker::RemovePoint( int index )
186 if( (unsigned)index < (unsigned)m_count )
188 //m_features[m_idx].RemoveAt( index );
189 m_features[m_idx].erase(m_features[m_idx].begin() + index);
191 //m_features[m_idx^1].RemoveAt( index );
192 m_features[m_idx^1].erase(m_features[m_idx^1].begin() + index);
194 //m_matrices.RemoveAt( m_count*2 - 1 );
195 m_matrices.erase(m_matrices.begin() + m_count*2 - 1);
196 //m_matrices.RemoveAt( m_count*2 - 2 );
197 m_matrices.erase(m_matrices.begin() + m_count*2 - 2);
203 void CLkTracker::AddPoint( CvPoint p, CImage& image )
205 CvPoint2D32f pt = AdjustPoint( p, image );
207 if( m_count < m_features[m_idx].size() )
209 m_features[m_idx][m_count] = pt;
213 //m_features[m_idx].Add(pt);
214 m_features[m_idx].push_back(pt);
215 //m_features[m_idx].Add(pt);
216 m_features[m_idx].push_back(pt);
217 //m_features[m_idx^1].Add(pt);
218 m_features[m_idx^1].push_back(pt);
219 //m_matrices.Add(pt);
220 m_matrices.push_back(pt);
221 //m_matrices.Add(pt);
222 m_matrices.push_back(pt);
224 m_status.push_back(0);
230 void CLkTracker::MovePoint( int index, CvPoint p, CImage& image )
232 if((unsigned)index < (unsigned)m_count )
234 CvPoint2D32f pt = AdjustPoint( p, image );
235 m_features[m_idx][index] = pt;
240 CvPoint2D32f CLkTracker::AdjustPoint( CvPoint p, CImage& image )
245 cvFindCornerSubPix( image.GetImage(), &pt, 1, cvSize(5,5), cvSize(-1,-1),
246 cvTermCriteria( CV_TERMCRIT_ITER, 10, 0.1f ));