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.
42 #include "precomp.hpp"
43 #include "opencl_kernels.hpp"
53 struct greaterThanPtr :
54 public std::binary_function<const float *, const float *, bool>
56 bool operator () (const float * a, const float * b) const
66 bool operator < (const Corner & c) const
67 { return val > c.val; }
72 static bool ocl_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
73 int maxCorners, double qualityLevel, double minDistance,
74 InputArray _mask, int blockSize,
75 bool useHarrisDetector, double harrisK )
78 if( useHarrisDetector )
79 cornerHarris( _image, eig, blockSize, 3, harrisK );
81 cornerMinEigenVal( _image, eig, blockSize, 3 );
84 minMaxLoc( eig, NULL, &maxVal, NULL, NULL, _mask );
85 threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );
86 dilate( eig, tmp, Mat());
88 Size imgsize = _image.size();
89 std::vector<Corner> tmpCorners;
90 size_t total, i, j, ncorners = 0, possibleCornersCount =
91 std::max(1024, static_cast<int>(imgsize.area() * 0.1));
92 bool haveMask = !_mask.empty();
94 // collect list of pointers to features - put them into temporary image
96 ocl::Kernel k("findCorners", ocl::imgproc::gftt_oclsrc,
97 format(haveMask ? "-D HAVE_MASK" : ""));
101 UMat counter(1, 1, CV_32SC1, Scalar::all(0)),
102 corners(1, (int)(possibleCornersCount * sizeof(Corner)), CV_8UC1);
103 ocl::KernelArg eigarg = ocl::KernelArg::ReadOnlyNoSize(eig),
104 tmparg = ocl::KernelArg::ReadOnlyNoSize(tmp),
105 cornersarg = ocl::KernelArg::PtrWriteOnly(corners),
106 counterarg = ocl::KernelArg::PtrReadWrite(counter);
109 k.args(eigarg, tmparg, cornersarg, counterarg,
110 imgsize.height - 2, imgsize.width - 2);
113 UMat mask = _mask.getUMat();
114 k.args(eigarg, ocl::KernelArg::ReadOnlyNoSize(mask), tmparg,
115 cornersarg, counterarg, imgsize.height - 2, imgsize.width - 2);
118 size_t globalsize[2] = { imgsize.width - 2, imgsize.height - 2 };
119 if (!k.run(2, globalsize, NULL, false))
122 total = counter.getMat(ACCESS_READ).at<int>(0, 0);
123 int totalb = (int)(sizeof(Corner) * total);
125 tmpCorners.resize(total);
126 Mat mcorners(1, totalb, CV_8UC1, &tmpCorners[0]);
127 corners.colRange(0, totalb).copyTo(mcorners);
130 std::sort( tmpCorners.begin(), tmpCorners.end() );
131 std::vector<Point2f> corners;
132 corners.reserve(total);
134 if (minDistance >= 1)
136 // Partition the image into larger grids
137 int w = imgsize.width, h = imgsize.height;
139 const int cell_size = cvRound(minDistance);
140 const int grid_width = (w + cell_size - 1) / cell_size;
141 const int grid_height = (h + cell_size - 1) / cell_size;
143 std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
144 minDistance *= minDistance;
146 for( i = 0; i < total; i++ )
148 const Corner & c = tmpCorners[i];
151 int x_cell = c.x / cell_size;
152 int y_cell = c.y / cell_size;
160 x1 = std::max(0, x1);
161 y1 = std::max(0, y1);
162 x2 = std::min(grid_width-1, x2);
163 y2 = std::min(grid_height-1, y2);
165 for( int yy = y1; yy <= y2; yy++ )
166 for( int xx = x1; xx <= x2; xx++ )
168 std::vector<Point2f> &m = grid[yy*grid_width + xx];
172 for(j = 0; j < m.size(); j++)
174 float dx = c.x - m[j].x;
175 float dy = c.y - m[j].y;
177 if( dx*dx + dy*dy < minDistance )
190 grid[y_cell*grid_width + x_cell].push_back(Point2f((float)c.x, (float)c.y));
192 corners.push_back(Point2f((float)c.x, (float)c.y));
195 if( maxCorners > 0 && (int)ncorners == maxCorners )
202 for( i = 0; i < total; i++ )
204 const Corner & c = tmpCorners[i];
206 corners.push_back(Point2f((float)c.x, (float)c.y));
208 if( maxCorners > 0 && (int)ncorners == maxCorners )
213 Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
221 void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
222 int maxCorners, double qualityLevel, double minDistance,
223 InputArray _mask, int blockSize,
224 bool useHarrisDetector, double harrisK )
226 CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 );
227 CV_Assert( _mask.empty() || (_mask.type() == CV_8UC1 && _mask.sameSize(_image)) );
229 CV_OCL_RUN(_image.dims() <= 2 && _image.isUMat(),
230 ocl_goodFeaturesToTrack(_image, _corners, maxCorners, qualityLevel, minDistance,
231 _mask, blockSize, useHarrisDetector, harrisK))
233 Mat image = _image.getMat(), eig, tmp;
234 if( useHarrisDetector )
235 cornerHarris( image, eig, blockSize, 3, harrisK );
237 cornerMinEigenVal( image, eig, blockSize, 3 );
240 minMaxLoc( eig, 0, &maxVal, 0, 0, _mask );
241 threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );
242 dilate( eig, tmp, Mat());
244 Size imgsize = image.size();
245 std::vector<const float*> tmpCorners;
247 // collect list of pointers to features - put them into temporary image
248 Mat mask = _mask.getMat();
249 for( int y = 1; y < imgsize.height - 1; y++ )
251 const float* eig_data = (const float*)eig.ptr(y);
252 const float* tmp_data = (const float*)tmp.ptr(y);
253 const uchar* mask_data = mask.data ? mask.ptr(y) : 0;
255 for( int x = 1; x < imgsize.width - 1; x++ )
257 float val = eig_data[x];
258 if( val != 0 && val == tmp_data[x] && (!mask_data || mask_data[x]) )
259 tmpCorners.push_back(eig_data + x);
263 std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() );
264 std::vector<Point2f> corners;
265 size_t i, j, total = tmpCorners.size(), ncorners = 0;
267 if (minDistance >= 1)
269 // Partition the image into larger grids
273 const int cell_size = cvRound(minDistance);
274 const int grid_width = (w + cell_size - 1) / cell_size;
275 const int grid_height = (h + cell_size - 1) / cell_size;
277 std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
279 minDistance *= minDistance;
281 for( i = 0; i < total; i++ )
283 int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);
284 int y = (int)(ofs / eig.step);
285 int x = (int)((ofs - y*eig.step)/sizeof(float));
289 int x_cell = x / cell_size;
290 int y_cell = y / cell_size;
298 x1 = std::max(0, x1);
299 y1 = std::max(0, y1);
300 x2 = std::min(grid_width-1, x2);
301 y2 = std::min(grid_height-1, y2);
303 for( int yy = y1; yy <= y2; yy++ )
304 for( int xx = x1; xx <= x2; xx++ )
306 std::vector <Point2f> &m = grid[yy*grid_width + xx];
310 for(j = 0; j < m.size(); j++)
312 float dx = x - m[j].x;
313 float dy = y - m[j].y;
315 if( dx*dx + dy*dy < minDistance )
328 grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y));
330 corners.push_back(Point2f((float)x, (float)y));
333 if( maxCorners > 0 && (int)ncorners == maxCorners )
340 for( i = 0; i < total; i++ )
342 int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);
343 int y = (int)(ofs / eig.step);
344 int x = (int)((ofs - y*eig.step)/sizeof(float));
346 corners.push_back(Point2f((float)x, (float)y));
348 if( maxCorners > 0 && (int)ncorners == maxCorners )
353 Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
357 cvGoodFeaturesToTrack( const void* _image, void*, void*,
358 CvPoint2D32f* _corners, int *_corner_count,
359 double quality_level, double min_distance,
360 const void* _maskImage, int block_size,
361 int use_harris, double harris_k )
363 cv::Mat image = cv::cvarrToMat(_image), mask;
364 std::vector<cv::Point2f> corners;
367 mask = cv::cvarrToMat(_maskImage);
369 CV_Assert( _corners && _corner_count );
370 cv::goodFeaturesToTrack( image, corners, *_corner_count, quality_level,
371 min_distance, mask, block_size, use_harris != 0, harris_k );
373 size_t i, ncorners = corners.size();
374 for( i = 0; i < ncorners; i++ )
375 _corners[i] = corners[i];
376 *_corner_count = (int)ncorners;