2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
2014-01-17 23:00:52 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
#include "precomp.hpp"
|
2014-01-17 23:00:52 +08:00
|
|
|
#include "opencl_kernels.hpp"
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <vector>
|
2014-01-17 23:00:52 +08:00
|
|
|
#include <iostream>
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
2014-01-17 23:00:52 +08:00
|
|
|
struct greaterThanPtr :
|
|
|
|
public std::binary_function<const float *, const float *, bool>
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-01-17 23:00:52 +08:00
|
|
|
bool operator () (const float * a, const float * b) const
|
|
|
|
{ return *a > *b; }
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
2014-01-17 23:00:52 +08:00
|
|
|
struct Corner
|
|
|
|
{
|
|
|
|
float val;
|
|
|
|
short y;
|
|
|
|
short x;
|
|
|
|
|
|
|
|
bool operator < (const Corner & c) const
|
|
|
|
{ return val > c.val; }
|
|
|
|
};
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
|
2014-01-17 23:00:52 +08:00
|
|
|
static bool ocl_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
|
|
|
|
int maxCorners, double qualityLevel, double minDistance,
|
|
|
|
InputArray _mask, int blockSize,
|
|
|
|
bool useHarrisDetector, double harrisK )
|
|
|
|
{
|
|
|
|
UMat eig, tmp;
|
|
|
|
if( useHarrisDetector )
|
|
|
|
cornerHarris( _image, eig, blockSize, 3, harrisK );
|
|
|
|
else
|
|
|
|
cornerMinEigenVal( _image, eig, blockSize, 3 );
|
|
|
|
|
|
|
|
double maxVal = 0;
|
|
|
|
minMaxLoc( eig, NULL, &maxVal, NULL, NULL, _mask );
|
|
|
|
threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );
|
|
|
|
dilate( eig, tmp, Mat());
|
|
|
|
|
|
|
|
Size imgsize = _image.size();
|
|
|
|
std::vector<Corner> tmpCorners;
|
|
|
|
size_t total, i, j, ncorners = 0, possibleCornersCount =
|
|
|
|
std::max(1024, static_cast<int>(imgsize.area() * 0.1));
|
|
|
|
bool haveMask = !_mask.empty();
|
|
|
|
|
|
|
|
// collect list of pointers to features - put them into temporary image
|
|
|
|
{
|
|
|
|
ocl::Kernel k("findCorners", ocl::imgproc::gftt_oclsrc,
|
|
|
|
format(haveMask ? "-D HAVE_MASK" : ""));
|
|
|
|
if (k.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UMat counter(1, 1, CV_32SC1, Scalar::all(0)),
|
2014-01-19 01:13:50 +08:00
|
|
|
corners(1, (int)(possibleCornersCount * sizeof(Corner)), CV_8UC1);
|
2014-01-17 23:00:52 +08:00
|
|
|
ocl::KernelArg eigarg = ocl::KernelArg::ReadOnlyNoSize(eig),
|
|
|
|
tmparg = ocl::KernelArg::ReadOnlyNoSize(tmp),
|
|
|
|
cornersarg = ocl::KernelArg::PtrWriteOnly(corners),
|
|
|
|
counterarg = ocl::KernelArg::PtrReadWrite(counter);
|
|
|
|
|
|
|
|
if (!haveMask)
|
|
|
|
k.args(eigarg, tmparg, cornersarg, counterarg,
|
|
|
|
imgsize.height - 2, imgsize.width - 2);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UMat mask = _mask.getUMat();
|
|
|
|
k.args(eigarg, ocl::KernelArg::ReadOnlyNoSize(mask), tmparg,
|
|
|
|
cornersarg, counterarg, imgsize.height - 2, imgsize.width - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t globalsize[2] = { imgsize.width - 2, imgsize.height - 2 };
|
|
|
|
if (!k.run(2, globalsize, NULL, false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
total = counter.getMat(ACCESS_READ).at<int>(0, 0);
|
2014-01-19 01:13:50 +08:00
|
|
|
int totalb = (int)(sizeof(Corner) * total);
|
2014-01-17 23:00:52 +08:00
|
|
|
|
|
|
|
tmpCorners.resize(total);
|
|
|
|
Mat mcorners(1, totalb, CV_8UC1, &tmpCorners[0]);
|
2014-01-20 20:46:20 +08:00
|
|
|
corners.colRange(0, totalb).getMat(ACCESS_READ).copyTo(mcorners);
|
2014-01-17 23:00:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::sort( tmpCorners.begin(), tmpCorners.end() );
|
|
|
|
std::vector<Point2f> corners;
|
|
|
|
corners.reserve(total);
|
|
|
|
|
|
|
|
if (minDistance >= 1)
|
|
|
|
{
|
|
|
|
// Partition the image into larger grids
|
|
|
|
int w = imgsize.width, h = imgsize.height;
|
|
|
|
|
|
|
|
const int cell_size = cvRound(minDistance);
|
|
|
|
const int grid_width = (w + cell_size - 1) / cell_size;
|
|
|
|
const int grid_height = (h + cell_size - 1) / cell_size;
|
|
|
|
|
|
|
|
std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
|
|
|
|
minDistance *= minDistance;
|
|
|
|
|
|
|
|
for( i = 0; i < total; i++ )
|
|
|
|
{
|
|
|
|
const Corner & c = tmpCorners[i];
|
|
|
|
bool good = true;
|
|
|
|
|
|
|
|
int x_cell = c.x / cell_size;
|
|
|
|
int y_cell = c.y / cell_size;
|
|
|
|
|
|
|
|
int x1 = x_cell - 1;
|
|
|
|
int y1 = y_cell - 1;
|
|
|
|
int x2 = x_cell + 1;
|
|
|
|
int y2 = y_cell + 1;
|
|
|
|
|
|
|
|
// boundary check
|
|
|
|
x1 = std::max(0, x1);
|
|
|
|
y1 = std::max(0, y1);
|
|
|
|
x2 = std::min(grid_width-1, x2);
|
|
|
|
y2 = std::min(grid_height-1, y2);
|
|
|
|
|
|
|
|
for( int yy = y1; yy <= y2; yy++ )
|
|
|
|
for( int xx = x1; xx <= x2; xx++ )
|
|
|
|
{
|
|
|
|
std::vector<Point2f> &m = grid[yy*grid_width + xx];
|
|
|
|
|
|
|
|
if( m.size() )
|
|
|
|
{
|
|
|
|
for(j = 0; j < m.size(); j++)
|
|
|
|
{
|
|
|
|
float dx = c.x - m[j].x;
|
|
|
|
float dy = c.y - m[j].y;
|
|
|
|
|
|
|
|
if( dx*dx + dy*dy < minDistance )
|
|
|
|
{
|
|
|
|
good = false;
|
|
|
|
goto break_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break_out:
|
|
|
|
|
|
|
|
if (good)
|
|
|
|
{
|
|
|
|
grid[y_cell*grid_width + x_cell].push_back(Point2f((float)c.x, (float)c.y));
|
|
|
|
|
|
|
|
corners.push_back(Point2f((float)c.x, (float)c.y));
|
|
|
|
++ncorners;
|
|
|
|
|
|
|
|
if( maxCorners > 0 && (int)ncorners == maxCorners )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( i = 0; i < total; i++ )
|
|
|
|
{
|
|
|
|
const Corner & c = tmpCorners[i];
|
|
|
|
|
|
|
|
corners.push_back(Point2f((float)c.x, (float)c.y));
|
|
|
|
++ncorners;
|
|
|
|
if( maxCorners > 0 && (int)ncorners == maxCorners )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
#endif
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-06-06 22:51:27 +08:00
|
|
|
void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
|
2011-04-17 21:14:45 +08:00
|
|
|
int maxCorners, double qualityLevel, double minDistance,
|
2011-06-06 22:51:27 +08:00
|
|
|
InputArray _mask, int blockSize,
|
2011-04-17 21:14:45 +08:00
|
|
|
bool useHarrisDetector, double harrisK )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 );
|
2014-01-17 23:00:52 +08:00
|
|
|
CV_Assert( _mask.empty() || (_mask.type() == CV_8UC1 && _mask.sameSize(_image)) );
|
|
|
|
|
2014-01-27 17:25:21 +08:00
|
|
|
CV_OCL_RUN(_image.dims() <= 2 && _image.isUMat(),
|
|
|
|
ocl_goodFeaturesToTrack(_image, _corners, maxCorners, qualityLevel, minDistance,
|
2014-01-19 01:13:50 +08:00
|
|
|
_mask, blockSize, useHarrisDetector, harrisK))
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-01-17 23:00:52 +08:00
|
|
|
Mat image = _image.getMat(), eig, tmp;
|
2010-05-12 01:44:00 +08:00
|
|
|
if( useHarrisDetector )
|
|
|
|
cornerHarris( image, eig, blockSize, 3, harrisK );
|
|
|
|
else
|
|
|
|
cornerMinEigenVal( image, eig, blockSize, 3 );
|
|
|
|
|
|
|
|
double maxVal = 0;
|
2014-01-17 23:00:52 +08:00
|
|
|
minMaxLoc( eig, 0, &maxVal, 0, 0, _mask );
|
2010-05-12 01:44:00 +08:00
|
|
|
threshold( eig, eig, maxVal*qualityLevel, 0, THRESH_TOZERO );
|
|
|
|
dilate( eig, tmp, Mat());
|
|
|
|
|
|
|
|
Size imgsize = image.size();
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<const float*> tmpCorners;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// collect list of pointers to features - put them into temporary image
|
2014-01-17 23:00:52 +08:00
|
|
|
Mat mask = _mask.getMat();
|
2010-05-12 01:44:00 +08:00
|
|
|
for( int y = 1; y < imgsize.height - 1; y++ )
|
|
|
|
{
|
|
|
|
const float* eig_data = (const float*)eig.ptr(y);
|
|
|
|
const float* tmp_data = (const float*)tmp.ptr(y);
|
|
|
|
const uchar* mask_data = mask.data ? mask.ptr(y) : 0;
|
|
|
|
|
|
|
|
for( int x = 1; x < imgsize.width - 1; x++ )
|
|
|
|
{
|
|
|
|
float val = eig_data[x];
|
|
|
|
if( val != 0 && val == tmp_data[x] && (!mask_data || mask_data[x]) )
|
|
|
|
tmpCorners.push_back(eig_data + x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 23:00:52 +08:00
|
|
|
std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() );
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Point2f> corners;
|
2010-05-12 01:44:00 +08:00
|
|
|
size_t i, j, total = tmpCorners.size(), ncorners = 0;
|
|
|
|
|
2014-01-17 23:00:52 +08:00
|
|
|
if (minDistance >= 1)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
// Partition the image into larger grids
|
|
|
|
int w = image.cols;
|
|
|
|
int h = image.rows;
|
|
|
|
|
|
|
|
const int cell_size = cvRound(minDistance);
|
|
|
|
const int grid_width = (w + cell_size - 1) / cell_size;
|
|
|
|
const int grid_height = (h + cell_size - 1) / cell_size;
|
|
|
|
|
|
|
|
std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
|
|
|
|
|
|
|
|
minDistance *= minDistance;
|
|
|
|
|
|
|
|
for( i = 0; i < total; i++ )
|
|
|
|
{
|
|
|
|
int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);
|
|
|
|
int y = (int)(ofs / eig.step);
|
|
|
|
int x = (int)((ofs - y*eig.step)/sizeof(float));
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
bool good = true;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
int x_cell = x / cell_size;
|
|
|
|
int y_cell = y / cell_size;
|
|
|
|
|
|
|
|
int x1 = x_cell - 1;
|
|
|
|
int y1 = y_cell - 1;
|
|
|
|
int x2 = x_cell + 1;
|
|
|
|
int y2 = y_cell + 1;
|
|
|
|
|
|
|
|
// boundary check
|
|
|
|
x1 = std::max(0, x1);
|
|
|
|
y1 = std::max(0, y1);
|
|
|
|
x2 = std::min(grid_width-1, x2);
|
|
|
|
y2 = std::min(grid_height-1, y2);
|
|
|
|
|
|
|
|
for( int yy = y1; yy <= y2; yy++ )
|
|
|
|
for( int xx = x1; xx <= x2; xx++ )
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector <Point2f> &m = grid[yy*grid_width + xx];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( m.size() )
|
|
|
|
{
|
|
|
|
for(j = 0; j < m.size(); j++)
|
|
|
|
{
|
|
|
|
float dx = x - m[j].x;
|
|
|
|
float dy = y - m[j].y;
|
|
|
|
|
|
|
|
if( dx*dx + dy*dy < minDistance )
|
|
|
|
{
|
|
|
|
good = false;
|
|
|
|
goto break_out;
|
|
|
|
}
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break_out:
|
|
|
|
|
2014-01-17 23:00:52 +08:00
|
|
|
if (good)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y));
|
|
|
|
|
|
|
|
corners.push_back(Point2f((float)x, (float)y));
|
|
|
|
++ncorners;
|
|
|
|
|
|
|
|
if( maxCorners > 0 && (int)ncorners == maxCorners )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( i = 0; i < total; i++ )
|
|
|
|
{
|
|
|
|
int ofs = (int)((const uchar*)tmpCorners[i] - eig.data);
|
|
|
|
int y = (int)(ofs / eig.step);
|
|
|
|
int x = (int)((ofs - y*eig.step)/sizeof(float));
|
|
|
|
|
|
|
|
corners.push_back(Point2f((float)x, (float)y));
|
|
|
|
++ncorners;
|
|
|
|
if( maxCorners > 0 && (int)ncorners == maxCorners )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvGoodFeaturesToTrack( const void* _image, void*, void*,
|
|
|
|
CvPoint2D32f* _corners, int *_corner_count,
|
|
|
|
double quality_level, double min_distance,
|
|
|
|
const void* _maskImage, int block_size,
|
|
|
|
int use_harris, double harris_k )
|
|
|
|
{
|
|
|
|
cv::Mat image = cv::cvarrToMat(_image), mask;
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<cv::Point2f> corners;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( _maskImage )
|
|
|
|
mask = cv::cvarrToMat(_maskImage);
|
|
|
|
|
|
|
|
CV_Assert( _corners && _corner_count );
|
|
|
|
cv::goodFeaturesToTrack( image, corners, *_corner_count, quality_level,
|
|
|
|
min_distance, mask, block_size, use_harris != 0, harris_k );
|
|
|
|
|
|
|
|
size_t i, ncorners = corners.size();
|
|
|
|
for( i = 0; i < ncorners; i++ )
|
|
|
|
_corners[i] = corners[i];
|
|
|
|
*_corner_count = (int)ncorners;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|