c461b91f345ea80879bbc042a9720bdb0628f3de
[opencv] / src / cv / cvfeatureselect.cpp
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 "_cv.h"
42
43 namespace cv
44 {
45
46 template<typename T> struct lessThanPtr
47 {
48     bool operator()(const T* a, const T* b) const { return *a < *b; }
49 };
50
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 )
55 {
56     CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 );
57
58     if( mask.data )
59         CV_Assert( mask.type() == CV_8UC1 && mask.size() == image.size() );
60
61     Mat eig, tmp;
62     if( useHarrisDetector )
63         cornerHarris( image, eig, blockSize, 3, harrisK );
64     else
65         cornerMinEigenVal( image, eig, blockSize, 3 );
66
67     double maxVal = 0;
68     minMaxLoc( eig, 0, &maxVal, 0, 0, mask );
69     threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );
70     dilate( eig, tmp, Mat());
71
72     Size imgsize = image.size();
73
74     vector<const float*> tmpCorners;
75
76     // collect list of pointers to features - put them into temporary image
77     for( int y = 1; y < imgsize.height - 1; y++ )
78     {
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;
82
83         for( int x = 1; x < imgsize.width - 1; x++ )
84         {
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);
88         }
89     }
90
91     sort( tmpCorners, lessThanPtr<float>() );
92     corners.clear();
93     size_t i, j, total = tmpCorners.size(), ncorners = 0;
94     
95     minDistance *= minDistance;
96
97     // select the strongest features
98     for( i = 0; i < total; i++ )
99     {
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));
103
104         if( minDistance > 0 )
105         {
106             for( j = 0; j < ncorners; j++ )
107             {
108                 float dx = x - corners[j].x;
109                 float dy = y - corners[j].y;
110                 if( dx*dx + dy*dy < minDistance )
111                     break;
112             }
113             if( j < ncorners )
114                 continue;
115         }
116
117         corners.push_back(Point2f((float)x, (float)y));
118         ++ncorners;
119         if( maxCorners > 0 && (int)ncorners == maxCorners )
120             break;
121     }
122 }
123     
124 void write(FileStorage& fs, const string& objname, const vector<KeyPoint>& keypoints)
125 {
126     WriteStructContext ws(fs, objname, CV_NODE_SEQ + CV_NODE_FLOW);
127     
128     int i, npoints = (int)keypoints.size();
129     for( i = 0; i < npoints; i++ )
130     {
131         const KeyPoint& kpt = keypoints[i];
132         write(fs, kpt.pt.x);
133         write(fs, kpt.pt.y);
134         write(fs, kpt.size);
135         write(fs, kpt.angle);
136         write(fs, kpt.response);
137         write(fs, kpt.octave);
138     }
139 }
140
141
142 void read(const FileNode& node, vector<KeyPoint>& keypoints)
143 {
144     keypoints.resize(0);
145     FileNodeIterator it = node.begin(), it_end = node.end();
146     for( ; it != it_end; )
147     {
148         KeyPoint kpt;
149         it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave;
150         keypoints.push_back(kpt);
151     }
152 }
153     
154 }
155
156 CV_IMPL void
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 )
162 {
163     cv::Mat image = cv::cvarrToMat(_image), mask;
164     cv::vector<cv::Point2f> corners;
165
166     if( _maskImage )
167         mask = cv::cvarrToMat(_maskImage);
168
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 );
172
173     size_t i, ncorners = corners.size();
174     for( i = 0; i < ncorners; i++ )
175         _corners[i] = corners[i];
176     *_corner_count = (int)ncorners;
177 }
178
179 /* End of file. */