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 #define cmp_features( f1, f2 ) (*(f1) > *(f2))
45 static CV_IMPLEMENT_QSORT( icvSortFeatures, int *, cmp_features )
48 cvGoodFeaturesToTrack( const void* image, void* eigImage, void* tempImage,
49 CvPoint2D32f* corners, int *corner_count,
50 double quality_level, double min_distance,
51 const void* maskImage, int block_size,
52 int use_harris, double harris_k )
57 CV_FUNCNAME( "cvGoodFeaturesToTrack" );
66 int eig_step, tmp_step;
68 /* when selecting points, use integer coordinates */
69 CvPoint *ptr = (CvPoint *) corners;
71 /* process floating-point images using integer arithmetics */
79 int coi1 = 0, coi2 = 0, coi3 = 0;
80 CvMat stub, *img = (CvMat*)image;
81 CvMat eig_stub, *eig = (CvMat*)eigImage;
82 CvMat tmp_stub, *tmp = (CvMat*)tempImage;
83 CvMat mask_stub, *mask = (CvMat*)maskImage;
87 max_count = *corner_count;
91 CV_CALL( img = cvGetMat( img, &stub, &coi1 ));
94 CV_CALL( eig = cvGetMat( eig, &eig_stub, &coi2 ));
98 CV_CALL( _eigImg = cvCreateMat( img->rows, img->cols, CV_32FC1 ));
104 CV_CALL( tmp = cvGetMat( tmp, &tmp_stub, &coi3 ));
108 CV_CALL( _tmpImg = cvCreateMat( img->rows, img->cols, CV_32FC1 ));
114 CV_CALL( mask = cvGetMat( mask, &mask_stub ));
115 if( !CV_IS_MASK_ARR( mask ))
117 CV_ERROR( CV_StsBadMask, "" );
121 if( coi1 != 0 || coi2 != 0 || coi3 != 0 )
122 CV_ERROR( CV_BadCOI, "" );
124 if( CV_MAT_CN(img->type) != 1 ||
125 CV_MAT_CN(eig->type) != 1 ||
126 CV_MAT_CN(tmp->type) != 1 )
127 CV_ERROR( CV_BadNumChannels, cvUnsupportedFormat );
129 if( CV_MAT_DEPTH(tmp->type) != CV_32F ||
130 CV_MAT_DEPTH(eig->type) != CV_32F )
131 CV_ERROR( CV_BadDepth, cvUnsupportedFormat );
133 if( !corners || !corner_count )
134 CV_ERROR( CV_StsNullPtr, "" );
137 CV_ERROR( CV_StsBadArg, "maximal corners number is non positive" );
139 if( quality_level <= 0 || min_distance < 0 )
140 CV_ERROR( CV_StsBadArg, "quality level or min distance are non positive" );
144 CV_CALL( cvCornerHarris( img, eig, block_size, 3, harris_k ));
148 CV_CALL( cvCornerMinEigenVal( img, eig, block_size, 3 ));
150 CV_CALL( cvMinMaxLoc( eig, 0, &max_val, 0, 0, mask ));
151 CV_CALL( cvThreshold( eig, eig, max_val * quality_level,
152 0, CV_THRESH_TOZERO ));
153 CV_CALL( cvDilate( eig, tmp ));
155 min_dist = cvRound( min_distance * min_distance );
157 size = cvGetMatSize( img );
158 ptr_data = (int**)(tmp->data.ptr);
159 eig_data = (int*)(eig->data.ptr);
160 tmp_data = (int*)(tmp->data.ptr);
163 mask_data = (uchar*)(mask->data.ptr);
164 mask_step = mask->step;
167 eig_step = eig->step / sizeof(eig_data[0]);
168 tmp_step = tmp->step / sizeof(tmp_data[0]);
170 /* collect list of pointers to features - put them into temporary image */
171 for( y = 1, k = 0; y < size.height - 1; y++ )
173 eig_data += eig_step;
174 tmp_data += tmp_step;
175 mask_data += mask_step;
177 for( x = 1; x < size.width - 1; x++ )
179 int val = eig_data[x];
180 if( val != 0 && val == tmp_data[x] && (!mask || mask_data[x]) )
181 ptr_data[k++] = eig_data + x;
185 icvSortFeatures( ptr_data, k, 0 );
187 /* select the strongest features */
188 for( i = 0; i < k; i++ )
190 int j = count, ofs = (int)((uchar*)(ptr_data[i]) - eig->data.ptr);
192 x = (ofs - y * eig->step)/sizeof(float);
196 for( j = 0; j < count; j++ )
198 int dx = x - ptr[j].x;
199 int dy = y - ptr[j].y;
200 int dist = dx * dx + dy * dy;
202 if( dist < min_dist )
211 if( ++count >= max_count )
216 /* convert points to floating-point format */
217 for( i = 0; i < count; i++ )
219 assert( (unsigned)ptr[i].x < (unsigned)size.width &&
220 (unsigned)ptr[i].y < (unsigned)size.height );
222 corners[i].x = (float)ptr[i].x;
223 corners[i].y = (float)ptr[i].y;
226 *corner_count = count;
230 cvReleaseMat( &_eigImg );
231 cvReleaseMat( &_tmpImg );