Moved corner values to another optional variable to preserve backward compatibility

This commit is contained in:
Anas 2020-07-22 12:11:38 +03:00
parent b88fb40c6e
commit 6137383d32
9 changed files with 94 additions and 64 deletions

View File

@ -714,14 +714,6 @@ public:
//! the default constructor
CV_WRAP KeyPoint();
/**
@param _pt x & y coordinates of the keypoint while z is the response
@param _size keypoint diameter
@param _angle keypoint orientation
@param _octave pyramid octave in which the keypoint has been detected
@param _class_id object id
*/
KeyPoint(Point3f _pt, float _size, float _angle=-1, int _octave=0, int _class_id=-1);
/**
@param _pt x & y coordinates of the keypoint
@param _size keypoint diameter
@param _angle keypoint orientation
@ -2435,10 +2427,6 @@ inline
KeyPoint::KeyPoint()
: pt(0,0), size(0), angle(-1), response(0), octave(0), class_id(-1) {}
inline
KeyPoint::KeyPoint(Point3f _pt, float _size, float _angle, int _octave, int _class_id)
: pt(_pt.x, _pt.y), size(_size), angle(_angle), response(_pt.z), octave(_octave), class_id(_class_id) {}
inline
KeyPoint::KeyPoint(Point2f _pt, float _size, float _angle, float _response, int _octave, int _class_id)
: pt(_pt), size(_size), angle(_angle), response(_response), octave(_octave), class_id(_class_id) {}

View File

@ -86,7 +86,8 @@ public:
return;
}
std::vector<Point3f> corners;
std::vector<Point2f> corners;
std::vector<float> corners_values;
if (_image.isUMat())
{
@ -96,7 +97,7 @@ public:
else
ugrayImage = _image.getUMat();
goodFeaturesToTrack( ugrayImage, corners, nfeatures, qualityLevel, minDistance, _mask,
goodFeaturesToTrack( ugrayImage, corners, corners_values, nfeatures, qualityLevel, minDistance, _mask,
blockSize, gradSize, useHarrisDetector, k );
}
else
@ -105,15 +106,16 @@ public:
if( image.type() != CV_8U )
cvtColor( image, grayImage, COLOR_BGR2GRAY );
goodFeaturesToTrack( grayImage, corners, nfeatures, qualityLevel, minDistance, _mask,
goodFeaturesToTrack( grayImage, corners, corners_values, nfeatures, qualityLevel, minDistance, _mask,
blockSize, gradSize, useHarrisDetector, k );
}
keypoints.resize(corners.size());
std::vector<Point3f>::const_iterator corner_it = corners.begin();
std::vector<Point2f>::const_iterator corner_it = corners.begin();
std::vector<KeyPoint>::iterator keypoint_it = keypoints.begin();
for( ; corner_it != corners.end() && keypoint_it != keypoints.end(); ++corner_it, ++keypoint_it )
*keypoint_it = KeyPoint( *corner_it, (float)blockSize);
std::vector<float>::iterator corners_values_it = corners_values.begin();
for( ; corner_it != corners.end() && keypoint_it != keypoints.end() && corners_values_it != corners_values.end(); ++corner_it, ++keypoint_it, ++corners_values_it )
*keypoint_it = KeyPoint( *corner_it, (float)blockSize, -1, *corners_values_it);
}

View File

@ -1981,6 +1981,7 @@ pixel neighborhood. See cornerEigenValsAndVecs .
@param useHarrisDetector Parameter indicating whether to use a Harris detector (see #cornerHarris)
or #cornerMinEigenVal.
@param k Free parameter of the Harris detector.
@param corners_values Optional vector of detected corens response.
@sa cornerMinEigenVal, cornerHarris, calcOpticalFlowPyrLK, estimateRigidTransform,
*/
@ -1988,13 +1989,14 @@ or #cornerMinEigenVal.
CV_EXPORTS_W void goodFeaturesToTrack( InputArray image, OutputArray corners,
int maxCorners, double qualityLevel, double minDistance,
InputArray mask = noArray(), int blockSize = 3,
bool useHarrisDetector = false, double k = 0.04 );
bool useHarrisDetector = false, double k = 0.04,
OutputArray corners_values = noArray());
CV_EXPORTS_W void goodFeaturesToTrack( InputArray image, OutputArray corners,
int maxCorners, double qualityLevel, double minDistance,
InputArray mask, int blockSize,
int gradientSize, bool useHarrisDetector = false,
double k = 0.04 );
double k = 0.04, OutputArray corners_values = noArray());
/** @example samples/cpp/tutorial_code/ImgTrans/houghlines.cpp
An example using the Hough line detector
![Sample input image](Hough_Lines_Tutorial_Original_Image.jpg) ![Output image](Hough_Lines_Tutorial_Result.jpg)

View File

@ -896,13 +896,13 @@ CVAPI(void) cvFindCornerSubPix( const CvArr* image, CvPoint2D32f* corners,
@see cv::goodFeaturesToTrack
*/
CVAPI(void) cvGoodFeaturesToTrack( const CvArr* image, CvArr* eig_image,
CvArr* temp_image, CvPoint3D32f* corners,
CvArr* temp_image, CvPoint2D32f* corners,
int* corner_count, double quality_level,
double min_distance,
const CvArr* mask CV_DEFAULT(NULL),
int block_size CV_DEFAULT(3),
int use_harris CV_DEFAULT(0),
double k CV_DEFAULT(0.04) );
double k CV_DEFAULT(0.04), float* corners_values CV_DEFAULT(NULL));
/** @brief Finds lines on binary image using one of several methods.

View File

@ -71,15 +71,17 @@ OCL_PERF_TEST_P(GoodFeaturesToTrackFixture, GoodFeaturesToTrack,
checkDeviceMaxMemoryAllocSize(img.size(), img.type());
UMat src(img.size(), img.type()), dst(1, maxCorners, CV_32FC3);
UMat src(img.size(), img.type()), dst(1, maxCorners, CV_32FC2);
std::vector<float> values;
img.copyTo(src);
declare.in(src, WARMUP_READ).out(dst);
OCL_TEST_CYCLE() cv::goodFeaturesToTrack(src, dst, maxCorners, qualityLevel,
minDistance, noArray(), 3, 3, harrisDetector, 0.04);
minDistance, noArray(), 3, 3, harrisDetector, 0.04, values);
SANITY_CHECK(dst);
SANITY_CHECK(values);
}
} } // namespace opencv_test::ocl

View File

@ -30,15 +30,20 @@ PERF_TEST_P(Image_MaxCorners_QualityLevel_MinDistance_BlockSize_gradientSize_Use
if (image.empty())
FAIL() << "Unable to load source image" << filename;
std::vector<Point3f> corners;
std::vector<Point2f> corners;
std::vector<float> corners_values;
double minDistance = 1;
TEST_CYCLE() goodFeaturesToTrack(image, corners, maxCorners, qualityLevel, minDistance, noArray(), blockSize, gradientSize, useHarrisDetector);
TEST_CYCLE() goodFeaturesToTrack(image, corners, maxCorners, qualityLevel, minDistance, noArray(), blockSize, gradientSize, useHarrisDetector, corners_values);
if (corners.size() > 50)
corners.erase(corners.begin() + 50, corners.end());
if (corners_values.size() > 50)
corners_values.erase(corners_values.begin() + 50, corners_values.end());
SANITY_CHECK(corners);
SANITY_CHECK(corners_values);
}
} // namespace

View File

@ -75,7 +75,7 @@ struct Corner
static bool ocl_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
int maxCorners, double qualityLevel, double minDistance,
InputArray _mask, int blockSize, int gradientSize,
bool useHarrisDetector, double harrisK )
bool useHarrisDetector, double harrisK, OutputArray _corners_values)
{
UMat eig, maxEigenValue;
if( useHarrisDetector )
@ -175,8 +175,10 @@ static bool ocl_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
Corner* corner_ptr = tmpCorners.ptr<Corner>() + 1;
std::sort(corner_ptr, corner_ptr + total);
std::vector<Point3f> corners;
std::vector<Point2f> corners;
std::vector<float> corners_values;
corners.reserve(total);
corners_values.reserve(total);
if (minDistance >= 1)
{
@ -236,7 +238,8 @@ static bool ocl_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
{
grid[y_cell*grid_width + x_cell].push_back(Point2f((float)c.x, (float)c.y));
corners.push_back(Point3f((float)c.x, (float)c.y, eig.getMat(ACCESS_READ).at<float>(c.y, c.x)));
corners.push_back(Point2f((float)c.x, (float)c.y));
corners_values.push_back(eig.getMat(ACCESS_READ).at<float>(c.y, c.x));
++ncorners;
if( maxCorners > 0 && (int)ncorners == maxCorners )
@ -250,14 +253,18 @@ static bool ocl_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
{
const Corner & c = corner_ptr[i];
corners.push_back(Point3f((float)c.x, (float)c.y, eig.getMat(ACCESS_READ).at<float>(c.y, c.x)));
corners.push_back(Point2f((float)c.x, (float)c.y));
corners_values.push_back(eig.getMat(ACCESS_READ).at<float>(c.y, c.x));
++ncorners;
if( maxCorners > 0 && (int)ncorners == maxCorners )
break;
}
}
Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
Mat(corners_values).convertTo(_corners_values, _corners_values.fixedType() ? _corners_values.type() : CV_32F);
return true;
}
@ -357,7 +364,7 @@ static bool openvx_harris(Mat image, OutputArray _corners,
void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
int maxCorners, double qualityLevel, double minDistance,
InputArray _mask, int blockSize, int gradientSize,
bool useHarrisDetector, double harrisK )
bool useHarrisDetector, double harrisK, OutputArray _corners_values)
{
CV_INSTRUMENT_REGION();
@ -366,12 +373,13 @@ void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
CV_OCL_RUN(_image.dims() <= 2 && _image.isUMat(),
ocl_goodFeaturesToTrack(_image, _corners, maxCorners, qualityLevel, minDistance,
_mask, blockSize, gradientSize, useHarrisDetector, harrisK))
_mask, blockSize, gradientSize, useHarrisDetector, harrisK, _corners_values))
Mat image = _image.getMat(), eig, tmp;
if (image.empty())
{
_corners.release();
_corners_values.release();
return;
}
@ -409,12 +417,14 @@ void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
}
}
std::vector<Point3f> corners;
std::vector<Point2f> corners;
std::vector<float> corners_values;
size_t i, j, total = tmpCorners.size(), ncorners = 0;
if (total == 0)
{
_corners.release();
_corners_values.release();
return;
}
@ -485,7 +495,8 @@ void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
{
grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y));
corners.push_back(Point3f((float)x, (float)y, eig.at<float>(y,x)));
corners.push_back(Point2f((float)x, (float)y));
corners_values.push_back(eig.at<float>(y,x));
++ncorners;
if( maxCorners > 0 && (int)ncorners == maxCorners )
@ -501,45 +512,52 @@ void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
int y = (int)(ofs / eig.step);
int x = (int)((ofs - y*eig.step)/sizeof(float));
corners.push_back(Point3f((float)x, (float)y, eig.at<float>(y,x)));
corners.push_back(Point2f((float)x, (float)y));
corners_values.push_back(eig.at<float>(y,x));
++ncorners;
if( maxCorners > 0 && (int)ncorners == maxCorners )
break;
}
}
Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
Mat(corners_values).convertTo(_corners_values, _corners_values.fixedType() ? _corners_values.type() : CV_32F);
}
CV_IMPL void
cvGoodFeaturesToTrack( const void* _image, void*, void*,
CvPoint3D32f* _corners, int *_corner_count,
CvPoint2D32f* _corners, int *_corner_count,
double quality_level, double min_distance,
const void* _maskImage, int block_size,
int use_harris, double harris_k )
int use_harris, double harris_k, float* _corners_values)
{
cv::Mat image = cv::cvarrToMat(_image), mask;
std::vector<cv::Point3f> corners;
std::vector<cv::Point2f> corners;
std::vector<float> corners_values;
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 );
min_distance, mask, block_size, use_harris != 0, harris_k, corners_values);
size_t i, ncorners = corners.size();
for( i = 0; i < ncorners; i++ )
_corners[i] = cvPoint3D32f(corners[i]);
for( i = 0; i < ncorners; i++ ) {
_corners[i] = cvPoint2D32f(corners[i]);
_corners_values[i] = corners_values[i];
}
*_corner_count = (int)ncorners;
}
void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners,
int maxCorners, double qualityLevel, double minDistance,
InputArray _mask, int blockSize,
bool useHarrisDetector, double harrisK )
bool useHarrisDetector, double harrisK, OutputArray _corners_values)
{
cv::goodFeaturesToTrack(_image, _corners, maxCorners, qualityLevel, minDistance,
_mask, blockSize, 3, useHarrisDetector, harrisK );
_mask, blockSize, 3, useHarrisDetector, harrisK, _corners_values);
}
/* End of file. */

View File

@ -62,6 +62,7 @@ PARAM_TEST_CASE(GoodFeaturesToTrack, double, bool)
TEST_DECLARE_INPUT_PARAMETER(src);
UMat points, upoints;
std::vector<float> values, uvalues;
virtual void SetUp()
{
@ -82,10 +83,10 @@ PARAM_TEST_CASE(GoodFeaturesToTrack, double, bool)
UMAT_UPLOAD_INPUT_PARAMETER(src);
}
void UMatToVector(const UMat & um, std::vector<Point3f> & v) const
void UMatToVector(const UMat & um, std::vector<Point2f> & v) const
{
v.resize(um.size().area());
um.copyTo(Mat(um.size(), CV_32FC3, &v[0]));
um.copyTo(Mat(um.size(), CV_32FC2, &v[0]));
}
};
@ -98,14 +99,16 @@ OCL_TEST_P(GoodFeaturesToTrack, Accuracy)
{
generateTestData();
std::vector<Point3f> upts, pts;
std::vector<Point2f> upts, pts;
OCL_OFF(cv::goodFeaturesToTrack(src_roi, points, maxCorners, qualityLevel, minDistance, noArray()));
OCL_OFF(cv::goodFeaturesToTrack(src_roi, points, maxCorners, qualityLevel, minDistance, noArray(), 3, 0, 0.04, values));
ASSERT_FALSE(points.empty());
ASSERT_FALSE(values.empty());
UMatToVector(points, pts);
OCL_ON(cv::goodFeaturesToTrack(usrc_roi, upoints, maxCorners, qualityLevel, minDistance));
OCL_ON(cv::goodFeaturesToTrack(usrc_roi, upoints, maxCorners, qualityLevel, minDistance, noArray(), 3, 0, 0.04, uvalues));
ASSERT_FALSE(upoints.empty());
ASSERT_FALSE(uvalues.empty());
UMatToVector(upoints, upts);
ASSERT_EQ(upts.size(), pts.size());
@ -113,9 +116,9 @@ OCL_TEST_P(GoodFeaturesToTrack, Accuracy)
int mistmatch = 0;
for (size_t i = 0; i < pts.size(); ++i)
{
Point3f a = upts[i], b = pts[i];
Point2f a = upts[i], b = pts[i];
bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1 && std::abs(a.z - b.z) < 1;
bool eq = std::abs(a.x - b.x) < 1 && std::abs(a.y - b.y) < 1 && std::abs(values[i] - uvalues[i]) < 1;
if (!eq)
++mistmatch;
@ -131,7 +134,7 @@ OCL_TEST_P(GoodFeaturesToTrack, EmptyCorners)
generateTestData();
usrc_roi.setTo(Scalar::all(0));
OCL_ON(cv::goodFeaturesToTrack(usrc_roi, upoints, maxCorners, qualityLevel, minDistance));
OCL_ON(cv::goodFeaturesToTrack(usrc_roi, upoints, maxCorners, qualityLevel, minDistance, uvalues));
ASSERT_TRUE(upoints.empty());
}

View File

@ -66,7 +66,7 @@ static void
test_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
int maxCorners, double qualityLevel, double minDistance,
InputArray _mask, int blockSize, int gradientSize,
bool useHarrisDetector, double harrisK )
bool useHarrisDetector, double harrisK, OutputArray _corners_values)
{
CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 );
@ -74,7 +74,6 @@ test_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
Mat image = _image.getMat(), mask = _mask.getMat();
int aperture_size = gradientSize;
int borderType = BORDER_DEFAULT;
Mat eig, tmp, tt;
@ -113,7 +112,8 @@ test_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
}
}
vector<Point3f> corners;
vector<Point2f> corners;
vector<float> corners_values;
size_t i, j, total = tmpCorners.size(), ncorners = 0;
std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() );
@ -183,7 +183,8 @@ test_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
{
grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y));
corners.push_back(Point3f((float)x, (float)y, eig.at<float>(y, x)));
corners.push_back(Point2f((float)x, (float)y));
corners_values.push_back(eig.at<float>(y, x));
++ncorners;
if( maxCorners > 0 && (int)ncorners == maxCorners )
@ -199,14 +200,17 @@ test_goodFeaturesToTrack( InputArray _image, OutputArray _corners,
int y = (int)(ofs / eig.step);
int x = (int)((ofs - y*eig.step)/sizeof(float));
corners.push_back(Point3f((float)x, (float)y, eig.at<float>(y, x)));
corners.push_back(Point2f((float)x, (float)y));
corners_values.push_back(eig.at<float>(y, x));
++ncorners;
if( maxCorners > 0 && (int)ncorners == maxCorners )
break;
}
}
Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F);
Mat(corners_values).convertTo(_corners_values, _corners_values.fixedType() ? _corners_values.type() : CV_32F);
}
@ -229,8 +233,10 @@ protected:
Mat mask;
int maxCorners;
vector<Point3f> corners;
vector<Point3f> Refcorners;
vector<Point2f> corners;
vector<float> corners_values;
vector<Point2f> Refcorners;
vector<float> Refcorners_values;
double qualityLevel;
double minDistance;
int blockSize;
@ -305,7 +311,8 @@ void CV_GoodFeatureToTTest::run_func()
blockSize,
gradientSize,
useHarrisDetector,
k );
k,
corners_values);
}
else
{
@ -323,7 +330,8 @@ void CV_GoodFeatureToTTest::run_func()
blockSize,
gradientSize,
useHarrisDetector,
k );
k,
corners_values);
}
}
@ -348,7 +356,8 @@ int CV_GoodFeatureToTTest::validate_test_results( int test_case_idx )
blockSize,
gradientSize,
useHarrisDetector,
k );
k,
Refcorners_values);
}
else
{
@ -366,7 +375,8 @@ int CV_GoodFeatureToTTest::validate_test_results( int test_case_idx )
blockSize,
gradientSize,
useHarrisDetector,
k );
k,
Refcorners_values);
}
double e = cv::norm(corners, Refcorners); // TODO cvtest
@ -381,8 +391,8 @@ int CV_GoodFeatureToTTest::validate_test_results( int test_case_idx )
ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
for(int i = 0; i < (int)std::min((unsigned int)(corners.size()), (unsigned int)(Refcorners.size())); i++){
if ( (corners[i].x != Refcorners[i].x) || (corners[i].y != Refcorners[i].y) || (corners[i].z != Refcorners[i].z))
printf("i = %i X %2.6f Xref %2.6f Y %2.6f Yref %2.6f Z %2.6f Zref %2.6f\n",i,corners[i].x,Refcorners[i].x,corners[i].y,Refcorners[i].y,corners[i].z,Refcorners[i].z);
if ( (corners[i].x != Refcorners[i].x) || (corners[i].y != Refcorners[i].y) || (corners_values[i] != Refcorners_values[i]))
printf("i = %i X %2.6f Xref %2.6f Y %2.6f Yref %2.6f Values %2.6f Values ref %2.6f\n",i,corners[i].x,Refcorners[i].x,corners[i].y,Refcorners[i].y,corners_values[i],Refcorners_values[i]);
}
}
else