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
68 bool operator < (const Corner & c) const
69 { 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 )
77 UMat eig, maxEigenValue;
78 if( useHarrisDetector )
79 cornerHarris( _image, eig, blockSize, 3, harrisK );
81 cornerMinEigenVal( _image, eig, blockSize, 3 );
83 Size imgsize = _image.size();
84 std::vector<Corner> tmpCorners;
85 size_t total, i, j, ncorners = 0, possibleCornersCount =
86 std::max(1024, static_cast<int>(imgsize.area() * 0.1));
87 bool haveMask = !_mask.empty();
91 CV_Assert(eig.type() == CV_32FC1);
92 int dbsize = ocl::Device::getDefault().maxComputeUnits();
93 size_t wgs = ocl::Device::getDefault().maxWorkGroupSize();
96 while (wgs2_aligned < (int)wgs)
100 ocl::Kernel k("maxEigenVal", ocl::imgproc::gftt_oclsrc,
101 format("-D OP_MAX_EIGEN_VAL -D WGS=%d -D groupnum=%d -D WGS2_ALIGNED=%d%s",
102 (int)wgs, dbsize, wgs2_aligned, haveMask ? " -D HAVE_MASK" : ""));
106 UMat mask = _mask.getUMat();
107 maxEigenValue.create(1, dbsize, CV_32FC1);
109 ocl::KernelArg eigarg = ocl::KernelArg::ReadOnlyNoSize(eig),
110 dbarg = ocl::KernelArg::PtrWriteOnly(maxEigenValue),
111 maskarg = ocl::KernelArg::ReadOnlyNoSize(mask);
114 k.args(eigarg, eig.cols, (int)eig.total(), dbarg, maskarg);
116 k.args(eigarg, eig.cols, (int)eig.total(), dbarg);
118 size_t globalsize = dbsize * wgs;
119 if (!k.run(1, &globalsize, &wgs, false))
122 ocl::Kernel k2("maxEigenValTask", ocl::imgproc::gftt_oclsrc,
123 format("-D OP_MAX_EIGEN_VAL -D WGS=%d -D WGS2_ALIGNED=%d -D groupnum=%d",
124 wgs, wgs2_aligned, dbsize));
128 k2.args(dbarg, (float)qualityLevel);
130 if (!k2.runTask(false))
134 // collect list of pointers to features - put them into temporary image
136 ocl::Kernel k("findCorners", ocl::imgproc::gftt_oclsrc,
137 format("-D OP_FIND_CORNERS%s", haveMask ? " -D HAVE_MASK" : ""));
141 UMat counter(1, 1, CV_32SC1, Scalar::all(0)),
142 corners(1, (int)possibleCornersCount, CV_32FC2, Scalar::all(-1));
143 CV_Assert(sizeof(Corner) == corners.elemSize());
145 ocl::KernelArg eigarg = ocl::KernelArg::ReadOnlyNoSize(eig),
146 cornersarg = ocl::KernelArg::PtrWriteOnly(corners),
147 counterarg = ocl::KernelArg::PtrReadWrite(counter),
148 thresholdarg = ocl::KernelArg::PtrReadOnly(maxEigenValue);
151 k.args(eigarg, cornersarg, counterarg,
152 eig.rows - 2, eig.cols - 2, thresholdarg,
153 (int)possibleCornersCount);
156 UMat mask = _mask.getUMat();
157 k.args(eigarg, ocl::KernelArg::ReadOnlyNoSize(mask),
158 cornersarg, counterarg, eig.rows - 2, eig.cols - 2,
159 thresholdarg, (int)possibleCornersCount);
162 size_t globalsize[2] = { eig.cols - 2, eig.rows - 2 };
163 if (!k.run(2, globalsize, NULL, false))
166 total = std::min<size_t>(counter.getMat(ACCESS_READ).at<int>(0, 0), possibleCornersCount);
173 tmpCorners.resize(total);
175 Mat mcorners(1, (int)total, CV_32FC2, &tmpCorners[0]);
176 corners.colRange(0, (int)total).copyTo(mcorners);
178 std::sort(tmpCorners.begin(), tmpCorners.end());
180 std::vector<Point2f> corners;
181 corners.reserve(total);
183 if (minDistance >= 1)
185 // Partition the image into larger grids
186 int w = imgsize.width, h = imgsize.height;
188 const int cell_size = cvRound(minDistance);
189 const int grid_width = (w + cell_size - 1) / cell_size;
190 const int grid_height = (h + cell_size - 1) / cell_size;
192 std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
193 minDistance *= minDistance;
195 for( i = 0; i < total; i++ )
197 const Corner & c = tmpCorners[i];
200 int x_cell = c.x / cell_size;
201 int y_cell = c.y / cell_size;
209 x1 = std::max(0, x1);
210 y1 = std::max(0, y1);
211 x2 = std::min(grid_width - 1, x2);
212 y2 = std::min(grid_height - 1, y2);
214 for( int yy = y1; yy <= y2; yy++ )
215 for( int xx = x1; xx <= x2; xx++ )
217 std::vector<Point2f> &m = grid[yy * grid_width + xx];
221 for(j = 0; j < m.size(); j++)
223 float dx = c.x - m[j].x;
224 float dy = c.y - m[j].y;
226 if( dx*dx + dy*dy < minDistance )
239 grid[y_cell*grid_width + x_cell].push_back(Point2f((float)c.x, (float)c.y));
241 corners.push_back(Point2f((float)c.x, (float)c.y));
244 if( maxCorners > 0 && (int)ncorners == maxCorners )
251 for( i = 0; i < total; i++ )
253 const Corner & c = tmpCorners[i];
255 corners.push_back(Point2f((float)c.x, (float)c.y));
257 if( maxCorners > 0 && (int)ncorners == maxCorners )
262 Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
270 void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
271 int maxCorners, double qualityLevel, double minDistance,
272 InputArray _mask, int blockSize,
273 bool useHarrisDetector, double harrisK )
275 CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 );
276 CV_Assert( _mask.empty() || (_mask.type() == CV_8UC1 && _mask.sameSize(_image)) );
278 CV_OCL_RUN(_image.dims() <= 2 && _image.isUMat(),
279 ocl_goodFeaturesToTrack(_image, _corners, maxCorners, qualityLevel, minDistance,
280 _mask, blockSize, useHarrisDetector, harrisK))
282 Mat image = _image.getMat(), eig, tmp;
283 if( useHarrisDetector )
284 cornerHarris( image, eig, blockSize, 3, harrisK );
286 cornerMinEigenVal( image, eig, blockSize, 3 );
289 minMaxLoc( eig, 0, &maxVal, 0, 0, _mask );
290 threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );
291 dilate( eig, tmp, Mat());
293 Size imgsize = image.size();
294 std::vector<const float*> tmpCorners;
296 // collect list of pointers to features - put them into temporary image
297 Mat mask = _mask.getMat();
298 for( int y = 1; y < imgsize.height - 1; y++ )
300 const float* eig_data = (const float*)eig.ptr(y);
301 const float* tmp_data = (const float*)tmp.ptr(y);
302 const uchar* mask_data = mask.data ? mask.ptr(y) : 0;
304 for( int x = 1; x < imgsize.width - 1; x++ )
306 float val = eig_data[x];
307 if( val != 0 && val == tmp_data[x] && (!mask_data || mask_data[x]) )
308 tmpCorners.push_back(eig_data + x);
311 std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() );
313 std::vector<Point2f> corners;
314 size_t i, j, total = tmpCorners.size(), ncorners = 0;
316 if (minDistance >= 1)
318 // Partition the image into larger grids
322 const int cell_size = cvRound(minDistance);
323 const int grid_width = (w + cell_size - 1) / cell_size;
324 const int grid_height = (h + cell_size - 1) / cell_size;
326 std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
328 minDistance *= minDistance;
330 for( i = 0; i < total; i++ )
332 int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);
333 int y = (int)(ofs / eig.step);
334 int x = (int)((ofs - y*eig.step)/sizeof(float));
338 int x_cell = x / cell_size;
339 int y_cell = y / cell_size;
347 x1 = std::max(0, x1);
348 y1 = std::max(0, y1);
349 x2 = std::min(grid_width-1, x2);
350 y2 = std::min(grid_height-1, y2);
352 for( int yy = y1; yy <= y2; yy++ )
353 for( int xx = x1; xx <= x2; xx++ )
355 std::vector <Point2f> &m = grid[yy*grid_width + xx];
359 for(j = 0; j < m.size(); j++)
361 float dx = x - m[j].x;
362 float dy = y - m[j].y;
364 if( dx*dx + dy*dy < minDistance )
377 grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y));
379 corners.push_back(Point2f((float)x, (float)y));
382 if( maxCorners > 0 && (int)ncorners == maxCorners )
389 for( i = 0; i < total; i++ )
391 int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);
392 int y = (int)(ofs / eig.step);
393 int x = (int)((ofs - y*eig.step)/sizeof(float));
395 corners.push_back(Point2f((float)x, (float)y));
397 if( maxCorners > 0 && (int)ncorners == maxCorners )
402 Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
406 cvGoodFeaturesToTrack( const void* _image, void*, void*,
407 CvPoint2D32f* _corners, int *_corner_count,
408 double quality_level, double min_distance,
409 const void* _maskImage, int block_size,
410 int use_harris, double harris_k )
412 cv::Mat image = cv::cvarrToMat(_image), mask;
413 std::vector<cv::Point2f> corners;
416 mask = cv::cvarrToMat(_maskImage);
418 CV_Assert( _corners && _corner_count );
419 cv::goodFeaturesToTrack( image, corners, *_corner_count, quality_level,
420 min_distance, mask, block_size, use_harris != 0, harris_k );
422 size_t i, ncorners = corners.size();
423 for( i = 0; i < ncorners; i++ )
424 _corners[i] = corners[i];
425 *_corner_count = (int)ncorners;