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) 2000, 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//   * Redistribution's of source code must retain the above copyright notice,
20//     this list of conditions and the following disclaimer.
21//
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.
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#include "_cvaux.h"
42
43CvCamShiftTracker::CvCamShiftTracker()
44{
45    int i;
46
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));
50    m_threshold = 0;
51
52    for( i = 0; i < CV_MAX_DIM; i++ )
53    {
54        m_min_ch_val[i] = 0;
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;
59    }
60
61    m_hist = 0;
62    m_back_project = 0;
63    m_temp = 0;
64    m_mask = 0;
65}
66
67
68CvCamShiftTracker::~CvCamShiftTracker()
69{
70    int i;
71
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 );
78}
79
80
81void
82CvCamShiftTracker::color_transform( const IplImage* image )
83{
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;
88    int dims[CV_MAX_DIM];
89    int i, n = get_hist_dims(dims);
90
91    assert( image->nChannels == 3 && m_hist != 0 );
92
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 )
96    {
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++ )
104        {
105            cvReleaseImage( &m_color_planes[i] );
106            if( i < n )
107                m_color_planes[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
108        }
109    }
110
111    cvCvtColor( image, m_temp, CV_BGR2HSV );
112    cvGetRawData( m_temp, &color_data, &color_step, &size );
113    cvGetRawData( m_mask, &mask, &mask_step, &size );
114
115    for( i = 0; i < n; i++ )
116        cvGetRawData( m_color_planes[i], &planes[i], &plane_step, &size );
117
118    for( ; size.height--; color_data += color_step, mask += mask_step )
119    {
120        for( x = 0; x < size.width; x++ )
121        {
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] )
128            {
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;
133                if( n > 1 )
134                    planes[1][x] = (uchar)val1;
135                if( n > 2 )
136                    planes[2][x] = (uchar)val2;
137
138                mask[x] = (uchar)255;
139            }
140            else
141            {
142                planes[0][x] = 0;
143                if( n > 1 )
144                    planes[1][x] = 0;
145                if( n > 2 )
146                    planes[2][x] = 0;
147                mask[x] = 0;
148            }
149        }
150        for( i = 0; i < n; i++ )
151            planes[i] += plane_step;
152    }
153}
154
155
156bool
157CvCamShiftTracker::update_histogram( const IplImage* cur_frame )
158{
159    float max_val = 0;
160    int i, dims;
161
162    if( m_comp.rect.width == 0 || m_comp.rect.height == 0 ||
163        m_hist == 0 )
164    {
165        assert(0);
166        return false;
167    }
168
169    color_transform(cur_frame);
170
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 );
175
176    cvSetHistBinRanges( m_hist, m_hist_ranges, 1 );
177    cvCalcHist( m_color_planes, m_hist, 0, m_mask );
178
179    for( i = 0; i < dims; i++ )
180        cvSetImageROI( m_color_planes[i], m_comp.rect );
181
182    for( i = 0; i < dims; i++ )
183        cvResetImageROI( m_color_planes[i] );
184    cvResetImageROI( m_mask );
185
186    cvGetMinMaxHistValue( m_hist, 0, &max_val );
187    cvScale( m_hist->bins, m_hist->bins, max_val ? 255. / max_val : 0. );
188
189    return max_val != 0;
190}
191
192
193void
194CvCamShiftTracker::reset_histogram()
195{
196    if( m_hist )
197        cvClearHist( m_hist );
198}
199
200
201bool
202CvCamShiftTracker::track_object( const IplImage* cur_frame )
203{
204    CvRect rect;
205    CvSize bp_size;
206
207    union
208    {
209        void** arr;
210        IplImage** img;
211    } u;
212
213    if( m_comp.rect.width == 0 || m_comp.rect.height == 0 ||
214        m_hist == 0 )
215    {
216        return false;
217    }
218
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 );
223
224    rect = m_comp.rect;
225    bp_size = cvGetSize( m_back_project );
226    if( rect.x < 0 )
227        rect.x = 0;
228    if( rect.x + rect.width > bp_size.width )
229        rect.width = bp_size.width - rect.x;
230    if( rect.y < 0 )
231        rect.y = 0;
232    if( rect.y + rect.height > bp_size.height )
233        rect.height = bp_size.height - rect.y;
234
235    cvCamShift( m_back_project, rect,
236                cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
237                &m_comp, &m_box );
238
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
241
242    return m_comp.rect.width != 0 && m_comp.rect.height != 0;
243}
244
245
246bool
247CvCamShiftTracker::set_hist_dims( int c_dims, int *dims )
248{
249    if( (unsigned)(c_dims-1) >= (unsigned)CV_MAX_DIM || dims == 0 )
250        return false;
251
252    if( m_hist )
253    {
254        int dims2[CV_MAX_DIM];
255        int c_dims2 = cvGetDims( m_hist->bins, dims2 );
256
257        if( c_dims2 == c_dims && memcmp( dims, dims2, c_dims*sizeof(dims[0])) == 0 )
258            return true;
259
260        cvReleaseHist( &m_hist );
261    }
262
263    m_hist = cvCreateHist( c_dims, dims, CV_HIST_ARRAY, 0, 0 );
264
265    return true;
266}
267
268
269bool
270CvCamShiftTracker::set_hist_bin_range( int channel, int min_val, int max_val )
271{
272    if( (unsigned)channel >= (unsigned)CV_MAX_DIM ||
273        min_val >= max_val || min_val < 0 || max_val > 256 )
274    {
275        assert(0);
276        return false;
277    }
278
279    m_hist_ranges[channel][0] = (float)min_val;
280    m_hist_ranges[channel][1] = (float)max_val;
281
282    return true;
283}
284
285/* End of file. */
286