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_imgproc.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 size_t total, i, j, ncorners = 0, possibleCornersCount =
85 std::max(1024, static_cast<int>(imgsize.area() * 0.1));
86 bool haveMask = !_mask.empty();
87 UMat corners_buffer(1, (int)possibleCornersCount + 1, CV_32FC2);
88 CV_Assert(sizeof(Corner) == corners_buffer.elemSize());
93 CV_Assert(eig.type() == CV_32FC1);
94 int dbsize = ocl::Device::getDefault().maxComputeUnits();
95 size_t wgs = ocl::Device::getDefault().maxWorkGroupSize();
98 while (wgs2_aligned < (int)wgs)
102 ocl::Kernel k("maxEigenVal", ocl::imgproc::gftt_oclsrc,
103 format("-D OP_MAX_EIGEN_VAL -D WGS=%d -D groupnum=%d -D WGS2_ALIGNED=%d%s",
104 (int)wgs, dbsize, wgs2_aligned, haveMask ? " -D HAVE_MASK" : ""));
108 UMat mask = _mask.getUMat();
109 maxEigenValue.create(1, dbsize, CV_32FC1);
111 ocl::KernelArg eigarg = ocl::KernelArg::ReadOnlyNoSize(eig),
112 dbarg = ocl::KernelArg::PtrWriteOnly(maxEigenValue),
113 maskarg = ocl::KernelArg::ReadOnlyNoSize(mask),
114 cornersarg = ocl::KernelArg::PtrWriteOnly(corners_buffer);
117 k.args(eigarg, eig.cols, (int)eig.total(), dbarg, maskarg);
119 k.args(eigarg, eig.cols, (int)eig.total(), dbarg);
121 size_t globalsize = dbsize * wgs;
122 if (!k.run(1, &globalsize, &wgs, false))
125 ocl::Kernel k2("maxEigenValTask", ocl::imgproc::gftt_oclsrc,
126 format("-D OP_MAX_EIGEN_VAL -D WGS=%d -D WGS2_ALIGNED=%d -D groupnum=%d",
127 wgs, wgs2_aligned, dbsize));
131 k2.args(dbarg, (float)qualityLevel, cornersarg);
133 if (!k2.runTask(false))
137 // collect list of pointers to features - put them into temporary image
139 ocl::Kernel k("findCorners", ocl::imgproc::gftt_oclsrc,
140 format("-D OP_FIND_CORNERS%s", haveMask ? " -D HAVE_MASK" : ""));
144 ocl::KernelArg eigarg = ocl::KernelArg::ReadOnlyNoSize(eig),
145 cornersarg = ocl::KernelArg::PtrWriteOnly(corners_buffer),
146 thresholdarg = ocl::KernelArg::PtrReadOnly(maxEigenValue);
149 k.args(eigarg, cornersarg, eig.rows - 2, eig.cols - 2, thresholdarg,
150 (int)possibleCornersCount);
153 UMat mask = _mask.getUMat();
154 k.args(eigarg, ocl::KernelArg::ReadOnlyNoSize(mask),
155 cornersarg, eig.rows - 2, eig.cols - 2,
156 thresholdarg, (int)possibleCornersCount);
159 size_t globalsize[2] = { eig.cols - 2, eig.rows - 2 };
160 if (!k.run(2, globalsize, NULL, false))
163 tmpCorners = corners_buffer.getMat(ACCESS_RW);
164 total = std::min<size_t>(tmpCorners.at<Vec2i>(0, 0)[0], possibleCornersCount);
172 Corner* corner_ptr = tmpCorners.ptr<Corner>() + 1;
173 std::sort(corner_ptr, corner_ptr + total);
175 std::vector<Point2f> corners;
176 corners.reserve(total);
178 if (minDistance >= 1)
180 // Partition the image into larger grids
181 int w = imgsize.width, h = imgsize.height;
183 const int cell_size = cvRound(minDistance);
184 const int grid_width = (w + cell_size - 1) / cell_size;
185 const int grid_height = (h + cell_size - 1) / cell_size;
187 std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
188 minDistance *= minDistance;
190 for( i = 0; i < total; i++ )
192 const Corner & c = corner_ptr[i];
195 int x_cell = c.x / cell_size;
196 int y_cell = c.y / cell_size;
204 x1 = std::max(0, x1);
205 y1 = std::max(0, y1);
206 x2 = std::min(grid_width - 1, x2);
207 y2 = std::min(grid_height - 1, y2);
209 for( int yy = y1; yy <= y2; yy++ )
210 for( int xx = x1; xx <= x2; xx++ )
212 std::vector<Point2f> &m = grid[yy * grid_width + xx];
216 for(j = 0; j < m.size(); j++)
218 float dx = c.x - m[j].x;
219 float dy = c.y - m[j].y;
221 if( dx*dx + dy*dy < minDistance )
234 grid[y_cell*grid_width + x_cell].push_back(Point2f((float)c.x, (float)c.y));
236 corners.push_back(Point2f((float)c.x, (float)c.y));
239 if( maxCorners > 0 && (int)ncorners == maxCorners )
246 for( i = 0; i < total; i++ )
248 const Corner & c = corner_ptr[i];
250 corners.push_back(Point2f((float)c.x, (float)c.y));
252 if( maxCorners > 0 && (int)ncorners == maxCorners )
257 Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
265 void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
266 int maxCorners, double qualityLevel, double minDistance,
267 InputArray _mask, int blockSize,
268 bool useHarrisDetector, double harrisK )
270 CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 );
271 CV_Assert( _mask.empty() || (_mask.type() == CV_8UC1 && _mask.sameSize(_image)) );
273 CV_OCL_RUN(_image.dims() <= 2 && _image.isUMat(),
274 ocl_goodFeaturesToTrack(_image, _corners, maxCorners, qualityLevel, minDistance,
275 _mask, blockSize, useHarrisDetector, harrisK))
277 Mat image = _image.getMat(), eig, tmp;
278 if( useHarrisDetector )
279 cornerHarris( image, eig, blockSize, 3, harrisK );
281 cornerMinEigenVal( image, eig, blockSize, 3 );
284 minMaxLoc( eig, 0, &maxVal, 0, 0, _mask );
285 threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );
286 dilate( eig, tmp, Mat());
288 Size imgsize = image.size();
289 std::vector<const float*> tmpCorners;
291 // collect list of pointers to features - put them into temporary image
292 Mat mask = _mask.getMat();
293 for( int y = 1; y < imgsize.height - 1; y++ )
295 const float* eig_data = (const float*)eig.ptr(y);
296 const float* tmp_data = (const float*)tmp.ptr(y);
297 const uchar* mask_data = mask.data ? mask.ptr(y) : 0;
299 for( int x = 1; x < imgsize.width - 1; x++ )
301 float val = eig_data[x];
302 if( val != 0 && val == tmp_data[x] && (!mask_data || mask_data[x]) )
303 tmpCorners.push_back(eig_data + x);
306 std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() );
308 std::vector<Point2f> corners;
309 size_t i, j, total = tmpCorners.size(), ncorners = 0;
311 if (minDistance >= 1)
313 // Partition the image into larger grids
317 const int cell_size = cvRound(minDistance);
318 const int grid_width = (w + cell_size - 1) / cell_size;
319 const int grid_height = (h + cell_size - 1) / cell_size;
321 std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
323 minDistance *= minDistance;
325 for( i = 0; i < total; i++ )
327 int ofs = (int)((const uchar*)tmpCorners[i] - eig.ptr());
328 int y = (int)(ofs / eig.step);
329 int x = (int)((ofs - y*eig.step)/sizeof(float));
333 int x_cell = x / cell_size;
334 int y_cell = y / cell_size;
342 x1 = std::max(0, x1);
343 y1 = std::max(0, y1);
344 x2 = std::min(grid_width-1, x2);
345 y2 = std::min(grid_height-1, y2);
347 for( int yy = y1; yy <= y2; yy++ )
348 for( int xx = x1; xx <= x2; xx++ )
350 std::vector <Point2f> &m = grid[yy*grid_width + xx];
354 for(j = 0; j < m.size(); j++)
356 float dx = x - m[j].x;
357 float dy = y - m[j].y;
359 if( dx*dx + dy*dy < minDistance )
372 grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y));
374 corners.push_back(Point2f((float)x, (float)y));
377 if( maxCorners > 0 && (int)ncorners == maxCorners )
384 for( i = 0; i < total; i++ )
386 int ofs = (int)((const uchar*)tmpCorners[i] - eig.ptr());
387 int y = (int)(ofs / eig.step);
388 int x = (int)((ofs - y*eig.step)/sizeof(float));
390 corners.push_back(Point2f((float)x, (float)y));
392 if( maxCorners > 0 && (int)ncorners == maxCorners )
397 Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
401 cvGoodFeaturesToTrack( const void* _image, void*, void*,
402 CvPoint2D32f* _corners, int *_corner_count,
403 double quality_level, double min_distance,
404 const void* _maskImage, int block_size,
405 int use_harris, double harris_k )
407 cv::Mat image = cv::cvarrToMat(_image), mask;
408 std::vector<cv::Point2f> corners;
411 mask = cv::cvarrToMat(_maskImage);
413 CV_Assert( _corners && _corner_count );
414 cv::goodFeaturesToTrack( image, corners, *_corner_count, quality_level,
415 min_distance, mask, block_size, use_harris != 0, harris_k );
417 size_t i, ncorners = corners.size();
418 for( i = 0; i < ncorners; i++ )
419 _corners[i] = corners[i];
420 *_corner_count = (int)ncorners;