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.
46 template<typename T> struct lessThanPtr
48 bool operator()(const T* a, const T* b) const { return *a < *b; }
51 void goodFeaturesToTrack( const Mat& image, vector<Point2f>& corners,
52 int maxCorners, double qualityLevel, double minDistance,
53 const Mat& mask, int blockSize,
54 bool useHarrisDetector, double harrisK )
56 CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 );
59 CV_Assert( mask.type() == CV_8UC1 && mask.size() == image.size() );
62 if( useHarrisDetector )
63 cornerHarris( image, eig, blockSize, 3, harrisK );
65 cornerMinEigenVal( image, eig, blockSize, 3 );
68 minMaxLoc( eig, 0, &maxVal, 0, 0, mask );
69 threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );
70 dilate( eig, tmp, Mat());
72 Size imgsize = image.size();
74 vector<const float*> tmpCorners;
76 // collect list of pointers to features - put them into temporary image
77 for( int y = 1; y < imgsize.height - 1; y++ )
79 const float* eig_data = (const float*)eig.ptr(y);
80 const float* tmp_data = (const float*)tmp.ptr(y);
81 const uchar* mask_data = mask.data ? mask.ptr(y) : 0;
83 for( int x = 1; x < imgsize.width - 1; x++ )
85 float val = eig_data[x];
86 if( val != 0 && val == tmp_data[x] && (!mask_data || mask_data[x]) )
87 tmpCorners.push_back(eig_data + x);
91 sort( tmpCorners, lessThanPtr<float>() );
93 size_t i, j, total = tmpCorners.size(), ncorners = 0;
95 minDistance *= minDistance;
97 // select the strongest features
98 for( i = 0; i < total; i++ )
100 int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);
101 int y = (int)(ofs / eig.step);
102 int x = (int)((ofs - y*eig.step)/sizeof(float));
104 if( minDistance > 0 )
106 for( j = 0; j < ncorners; j++ )
108 float dx = x - corners[j].x;
109 float dy = y - corners[j].y;
110 if( dx*dx + dy*dy < minDistance )
117 corners.push_back(Point2f((float)x, (float)y));
119 if( maxCorners > 0 && (int)ncorners == maxCorners )
124 void write(FileStorage& fs, const string& objname, const vector<KeyPoint>& keypoints)
126 WriteStructContext ws(fs, objname, CV_NODE_SEQ + CV_NODE_FLOW);
128 int i, npoints = (int)keypoints.size();
129 for( i = 0; i < npoints; i++ )
131 const KeyPoint& kpt = keypoints[i];
135 write(fs, kpt.angle);
136 write(fs, kpt.response);
137 write(fs, kpt.octave);
142 void read(const FileNode& node, vector<KeyPoint>& keypoints)
145 FileNodeIterator it = node.begin(), it_end = node.end();
146 for( ; it != it_end; )
149 it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave;
150 keypoints.push_back(kpt);
157 cvGoodFeaturesToTrack( const void* _image, void*, void*,
158 CvPoint2D32f* _corners, int *_corner_count,
159 double quality_level, double min_distance,
160 const void* _maskImage, int block_size,
161 int use_harris, double harris_k )
163 cv::Mat image = cv::cvarrToMat(_image), mask;
164 cv::vector<cv::Point2f> corners;
167 mask = cv::cvarrToMat(_maskImage);
169 CV_Assert( _corners && _corner_count );
170 cv::goodFeaturesToTrack( image, corners, *_corner_count, quality_level,
171 min_distance, mask, block_size, use_harris != 0, harris_k );
173 size_t i, ncorners = corners.size();
174 for( i = 0; i < ncorners; i++ )
175 _corners[i] = corners[i];
176 *_corner_count = (int)ncorners;