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.
43 CvCamShiftTracker::CvCamShiftTracker()
47 memset( &m_box, 0, sizeof(m_box));
48 memset( &m_comp, 0, sizeof(m_comp));
49 memset( m_color_planes, 0, sizeof(m_color_planes));
52 for( i = 0; i < CV_MAX_DIM; i++ )
55 m_max_ch_val[i] = 255;
56 m_hist_ranges[i] = m_hist_ranges_data[i];
57 m_hist_ranges[i][0] = 0.f;
58 m_hist_ranges[i][1] = 256.f;
68 CvCamShiftTracker::~CvCamShiftTracker()
72 cvReleaseHist( &m_hist );
73 for( i = 0; i < CV_MAX_DIM; i++ )
74 cvReleaseImage( &m_color_planes[i] );
75 cvReleaseImage( &m_back_project );
76 cvReleaseImage( &m_temp );
77 cvReleaseImage( &m_mask );
82 CvCamShiftTracker::color_transform( const IplImage* image )
84 CvSize size = cvGetSize(image);
85 uchar* color_data = 0, *mask = 0;
86 uchar* planes[CV_MAX_DIM];
87 int x, color_step = 0, plane_step = 0, mask_step;
89 int i, n = get_hist_dims(dims);
91 assert( image->nChannels == 3 && m_hist != 0 );
93 if( !m_temp || !m_mask || !m_color_planes[0] || !m_color_planes[n-1] || !m_back_project ||
94 m_temp->width != size.width || m_temp->height != size.height ||
95 m_temp->nChannels != 3 )
97 cvReleaseImage( &m_temp );
98 m_temp = cvCreateImage( size, IPL_DEPTH_8U, 3 );
99 cvReleaseImage( &m_mask );
100 m_mask = cvCreateImage( size, IPL_DEPTH_8U, 1 );
101 cvReleaseImage( &m_back_project );
102 m_back_project = cvCreateImage( size, IPL_DEPTH_8U, 1 );
103 for( i = 0; i < CV_MAX_DIM; i++ )
105 cvReleaseImage( &m_color_planes[i] );
107 m_color_planes[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
111 cvCvtColor( image, m_temp, CV_BGR2HSV );
112 cvGetRawData( m_temp, &color_data, &color_step, &size );
113 cvGetRawData( m_mask, &mask, &mask_step, &size );
115 for( i = 0; i < n; i++ )
116 cvGetRawData( m_color_planes[i], &planes[i], &plane_step, &size );
118 for( ; size.height--; color_data += color_step, mask += mask_step )
120 for( x = 0; x < size.width; x++ )
122 int val0 = color_data[x*3];
123 int val1 = color_data[x*3+1];
124 int val2 = color_data[x*3+2];
125 if( m_min_ch_val[0] <= val0 && val0 <= m_max_ch_val[0] &&
126 m_min_ch_val[1] <= val1 && val1 <= m_max_ch_val[1] &&
127 m_min_ch_val[2] <= val2 && val2 <= m_max_ch_val[2] )
129 // hue is written to the 0-th plane, saturation - to the 1-st one,
130 // so 1d histogram will automagically correspond to hue-based tracking,
131 // 2d histogram - to saturation-based tracking.
132 planes[0][x] = (uchar)val0;
134 planes[1][x] = (uchar)val1;
136 planes[2][x] = (uchar)val2;
138 mask[x] = (uchar)255;
150 for( i = 0; i < n; i++ )
151 planes[i] += plane_step;
157 CvCamShiftTracker::update_histogram( const IplImage* cur_frame )
162 if( m_comp.rect.width == 0 || m_comp.rect.height == 0 ||
169 color_transform(cur_frame);
171 dims = cvGetDims( m_hist->bins );
172 for( i = 0; i < dims; i++ )
173 cvSetImageROI( m_color_planes[i], m_comp.rect );
174 cvSetImageROI( m_mask, m_comp.rect );
176 cvSetHistBinRanges( m_hist, m_hist_ranges, 1 );
177 cvCalcHist( m_color_planes, m_hist, 0, m_mask );
179 for( i = 0; i < dims; i++ )
180 cvSetImageROI( m_color_planes[i], m_comp.rect );
182 for( i = 0; i < dims; i++ )
183 cvResetImageROI( m_color_planes[i] );
184 cvResetImageROI( m_mask );
186 cvGetMinMaxHistValue( m_hist, 0, &max_val );
187 cvScale( m_hist->bins, m_hist->bins, max_val ? 255. / max_val : 0. );
194 CvCamShiftTracker::reset_histogram()
197 cvClearHist( m_hist );
202 CvCamShiftTracker::track_object( const IplImage* cur_frame )
213 if( m_comp.rect.width == 0 || m_comp.rect.height == 0 ||
219 color_transform( cur_frame );
220 u.img = m_color_planes;
221 cvCalcArrBackProject( u.arr, m_back_project, m_hist );
222 cvAnd( m_back_project, m_mask, m_back_project );
225 bp_size = cvGetSize( m_back_project );
228 if( rect.x + rect.width > bp_size.width )
229 rect.width = bp_size.width - rect.x;
232 if( rect.y + rect.height > bp_size.height )
233 rect.height = bp_size.height - rect.y;
235 cvCamShift( m_back_project, rect,
236 cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
239 if( m_comp.rect.width == 0 || m_comp.rect.height == 0 )
240 m_comp.rect = rect; // do not allow tracker to loose the object
242 return m_comp.rect.width != 0 && m_comp.rect.height != 0;
247 CvCamShiftTracker::set_hist_dims( int c_dims, int *dims )
249 if( (unsigned)(c_dims-1) >= (unsigned)CV_MAX_DIM || dims == 0 )
254 int dims2[CV_MAX_DIM];
255 int c_dims2 = cvGetDims( m_hist->bins, dims2 );
257 if( c_dims2 == c_dims && memcmp( dims, dims2, c_dims*sizeof(dims[0])) == 0 )
260 cvReleaseHist( &m_hist );
263 m_hist = cvCreateHist( c_dims, dims, CV_HIST_ARRAY, 0, 0 );
270 CvCamShiftTracker::set_hist_bin_range( int channel, int min_val, int max_val )
272 if( (unsigned)channel >= (unsigned)CV_MAX_DIM ||
273 min_val >= max_val || min_val < 0 || max_val > 256 )
279 m_hist_ranges[channel][0] = (float)min_val;
280 m_hist_ranges[channel][1] = (float)max_val;