提交 58f69197 编写于 作者: V Vladislav Vinogradov

made GPU version of SURF more consistent with CPU one

上级 c067c633
...@@ -1537,83 +1537,55 @@ namespace cv ...@@ -1537,83 +1537,55 @@ namespace cv
}; };
////////////////////////////////// SURF ////////////////////////////////////////// ////////////////////////////////// SURF //////////////////////////////////////////
struct CV_EXPORTS SURFParams_GPU
{
SURFParams_GPU() : threshold(0.1f), nOctaves(4), nIntervals(4), initialScale(2.f),
l1(3.f/1.5f), l2(5.f/1.5f), l3(3.f/1.5f), l4(1.f/1.5f),
edgeScale(0.81f), initialStep(1), extended(true), featuresRatio(0.01f) {}
//! The interest operator threshold
float threshold;
//! The number of octaves to process
int nOctaves;
//! The number of intervals in each octave
int nIntervals;
//! The scale associated with the first interval of the first octave
float initialScale;
//! mask parameter l_1
float l1;
//! mask parameter l_2
float l2;
//! mask parameter l_3
float l3;
//! mask parameter l_4
float l4;
//! The amount to scale the edge rejection mask
float edgeScale;
//! The initial sampling step in pixels.
int initialStep;
//! True, if generate 128-len descriptors, false - 64-len descriptors
bool extended;
//! max features = featuresRatio * img.size().srea()
float featuresRatio;
};
class CV_EXPORTS SURF_GPU : public SURFParams_GPU class CV_EXPORTS SURF_GPU : public CvSURFParams
{ {
public: public:
//! the default constructor
SURF_GPU();
//! the full constructor taking all the necessary parameters
explicit SURF_GPU(double _hessianThreshold, int _nOctaves=4,
int _nOctaveLayers=2, bool _extended=false, float _keypointsRatio=0.01f);
//! returns the descriptor size in float's (64 or 128) //! returns the descriptor size in float's (64 or 128)
int descriptorSize() const; int descriptorSize() const;
//! upload host keypoints to device memory //! upload host keypoints to device memory
static void uploadKeypoints(const vector<KeyPoint>& keypoints, GpuMat& keypointsGPU); void uploadKeypoints(const vector<KeyPoint>& keypoints, GpuMat& keypointsGPU);
//! download keypoints from device to host memory //! download keypoints from device to host memory
static void downloadKeypoints(const GpuMat& keypointsGPU, vector<KeyPoint>& keypoints); void downloadKeypoints(const GpuMat& keypointsGPU, vector<KeyPoint>& keypoints);
//! download descriptors from device to host memory //! download descriptors from device to host memory
static void downloadDescriptors(const GpuMat& descriptorsGPU, vector<float>& descriptors); void downloadDescriptors(const GpuMat& descriptorsGPU, vector<float>& descriptors);
//! finds the keypoints using fast hessian detector used in SURF //! finds the keypoints using fast hessian detector used in SURF
//! supports CV_8UC1 images //! supports CV_8UC1 images
//! keypoints will have 1 row and type CV_32FC(6) //! keypoints will have 1 row and type CV_32FC(6)
//! keypoints.at<float[6]>(1, i) contains i'th keypoint //! keypoints.at<float[6]>(1, i) contains i'th keypoint
//! format: (x, y, size, response, angle, octave) //! format: (x, y, laplacian, size, dir, hessian)
void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints); void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints);
//! finds the keypoints and computes their descriptors. //! finds the keypoints and computes their descriptors.
//! Optionally it can compute descriptors for the user-provided keypoints and recompute keypoints direction //! Optionally it can compute descriptors for the user-provided keypoints and recompute keypoints direction
void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints, GpuMat& descriptors, void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints, GpuMat& descriptors,
bool useProvidedKeypoints = false, bool calcOrientation = true); bool useProvidedKeypoints = false);
void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints); void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints);
void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, GpuMat& descriptors, void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, GpuMat& descriptors,
bool useProvidedKeypoints = false, bool calcOrientation = true); bool useProvidedKeypoints = false);
void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, std::vector<float>& descriptors, void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, std::vector<float>& descriptors,
bool useProvidedKeypoints = false, bool calcOrientation = true); bool useProvidedKeypoints = false);
//! max keypoints = keypointsRatio * img.size().area()
float keypointsRatio;
GpuMat sum; GpuMat sum, mask1, maskSum, intBuffer;
GpuMat sumf;
GpuMat mask1; GpuMat det, trace;
GpuMat maskSum;
GpuMat hessianBuffer;
GpuMat maxPosBuffer; GpuMat maxPosBuffer;
GpuMat featuresBuffer; GpuMat featuresBuffer;
GpuMat keypointsBuffer;
}; };
} }
......
...@@ -111,20 +111,20 @@ namespace cv ...@@ -111,20 +111,20 @@ namespace cv
{ {
float x; float x;
float y; float y;
float laplacian;
float size; float size;
float response; float dir;
float angle; float hessian;
float octave;
}; };
enum KeypointLayout enum KeypointLayout
{ {
SF_X, SF_X,
SF_Y, SF_Y,
SF_LAPLACIAN,
SF_SIZE, SF_SIZE,
SF_RESPONSE, SF_DIR,
SF_ANGLE, SF_HESSIAN,
SF_OCTAVE,
SF_FEATURE_STRIDE SF_FEATURE_STRIDE
}; };
} }
......
此差异已折叠。
此差异已折叠。
...@@ -48,7 +48,6 @@ using namespace std; ...@@ -48,7 +48,6 @@ using namespace std;
const string FEATURES2D_DIR = "features2d"; const string FEATURES2D_DIR = "features2d";
const string IMAGE_FILENAME = "aloe.png"; const string IMAGE_FILENAME = "aloe.png";
const string VALID_FILE_NAME = "surf.xml.gz";
class CV_GPU_SURFTest : public cvtest::BaseTest class CV_GPU_SURFTest : public cvtest::BaseTest
{ {
...@@ -59,17 +58,20 @@ public: ...@@ -59,17 +58,20 @@ public:
protected: protected:
bool isSimilarKeypoints(const KeyPoint& p1, const KeyPoint& p2); bool isSimilarKeypoints(const KeyPoint& p1, const KeyPoint& p2);
int getValidCount(const vector<KeyPoint>& keypoints1, const vector<KeyPoint>& keypoints2, const vector<DMatch>& matches);
void compareKeypointSets(const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints, void compareKeypointSets(const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints,
const Mat& validDescriptors, const Mat& calcDescriptors); const Mat& validDescriptors, const Mat& calcDescriptors);
void emptyDataTest(SURF_GPU& fdetector); void emptyDataTest();
void regressionTest(SURF_GPU& fdetector); void accuracyTest();
virtual void run(int); virtual void run(int);
}; };
void CV_GPU_SURFTest::emptyDataTest(SURF_GPU& fdetector) void CV_GPU_SURFTest::emptyDataTest()
{ {
SURF_GPU fdetector;
GpuMat image; GpuMat image;
vector<KeyPoint> keypoints; vector<KeyPoint> keypoints;
vector<float> descriptors; vector<float> descriptors;
...@@ -114,116 +116,80 @@ bool CV_GPU_SURFTest::isSimilarKeypoints(const KeyPoint& p1, const KeyPoint& p2) ...@@ -114,116 +116,80 @@ bool CV_GPU_SURFTest::isSimilarKeypoints(const KeyPoint& p1, const KeyPoint& p2)
p1.class_id == p2.class_id ); p1.class_id == p2.class_id );
} }
void CV_GPU_SURFTest::compareKeypointSets(const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints, int CV_GPU_SURFTest::getValidCount(const vector<KeyPoint>& keypoints1, const vector<KeyPoint>& keypoints2,
const Mat& validDescriptors, const Mat& calcDescriptors) const vector<DMatch>& matches)
{ {
if (validKeypoints.size() != calcKeypoints.size()) int count = 0;
for (size_t i = 0; i < matches.size(); ++i)
{ {
ts->printf(cvtest::TS::LOG, "Keypoints sizes doesn't equal (validCount = %d, calcCount = %d).\n", const DMatch& m = matches[i];
validKeypoints.size(), calcKeypoints.size());
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); const KeyPoint& kp1 = keypoints1[m.queryIdx];
return; const KeyPoint& kp2 = keypoints2[m.trainIdx];
if (isSimilarKeypoints(kp1, kp2))
++count;
} }
if (validDescriptors.size() != calcDescriptors.size())
return count;
}
void CV_GPU_SURFTest::compareKeypointSets(const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints,
const Mat& validDescriptors, const Mat& calcDescriptors)
{
BruteForceMatcher< L2<float> > matcher;
vector<DMatch> matches;
matcher.match(validDescriptors, calcDescriptors, matches);
int validCount = getValidCount(validKeypoints, calcKeypoints, matches);
float validRatio = (float)validCount / matches.size();
if (validRatio < 0.5f)
{ {
ts->printf(cvtest::TS::LOG, "Descriptors sizes doesn't equal.\n"); ts->printf(cvtest::TS::LOG, "Bad accuracy - %f.\n", validRatio);
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT); ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
return; return;
} }
for (size_t v = 0; v < validKeypoints.size(); v++)
{
int nearestIdx = -1;
float minDist = std::numeric_limits<float>::max();
for (size_t c = 0; c < calcKeypoints.size(); c++)
{
float curDist = (float)norm(calcKeypoints[c].pt - validKeypoints[v].pt);
if (curDist < minDist)
{
minDist = curDist;
nearestIdx = c;
}
}
assert(minDist >= 0);
if (!isSimilarKeypoints(validKeypoints[v], calcKeypoints[nearestIdx]))
{
ts->printf(cvtest::TS::LOG, "Bad keypoints accuracy.\n");
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
return;
}
if (norm(validDescriptors.row(v), calcDescriptors.row(nearestIdx), NORM_L2) > 1.5f)
{
ts->printf(cvtest::TS::LOG, "Bad descriptors accuracy.\n");
ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
return;
}
}
} }
void CV_GPU_SURFTest::regressionTest(SURF_GPU& fdetector) void CV_GPU_SURFTest::accuracyTest()
{ {
string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME; string imgFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + IMAGE_FILENAME;
string resFilename = string(ts->get_data_path()) + FEATURES2D_DIR + "/" + VALID_FILE_NAME;
// Read the test image. // Read the test image.
GpuMat image(imread(imgFilename, 0)); Mat image = imread(imgFilename, 0);
if (image.empty()) if (image.empty())
{ {
ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() ); ts->printf( cvtest::TS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
return; return;
} }
FileStorage fs(resFilename, FileStorage::READ); Mat mask(image.size(), CV_8UC1, Scalar::all(1));
mask(Range(0, image.rows / 2), Range(0, image.cols / 2)).setTo(Scalar::all(0));
// Compute keypoints. // Compute keypoints.
GpuMat mask(image.size(), CV_8UC1, Scalar::all(1));
mask(Range(0, image.rows / 2), Range(0, image.cols / 2)).setTo(Scalar::all(0));
vector<KeyPoint> calcKeypoints; vector<KeyPoint> calcKeypoints;
GpuMat calcDespcriptors; GpuMat calcDescriptors;
fdetector(image, mask, calcKeypoints, calcDespcriptors); SURF_GPU fdetector; fdetector.extended = false;
fdetector(GpuMat(image), GpuMat(mask), calcKeypoints, calcDescriptors);
if (fs.isOpened()) // Compare computed and valid keypoints.
{ // Calc validation keypoints set.
// Read validation keypoints set. vector<KeyPoint> validKeypoints;
vector<KeyPoint> validKeypoints; vector<float> validDescriptors;
Mat validDespcriptors; SURF fdetector_gold; fdetector_gold.extended = false;
read(fs["keypoints"], validKeypoints); fdetector_gold(image, mask, validKeypoints, validDescriptors);
read(fs["descriptors"], validDespcriptors);
if (validKeypoints.empty() || validDespcriptors.empty()) compareKeypointSets(validKeypoints, calcKeypoints,
{ Mat(validKeypoints.size(), fdetector_gold.descriptorSize(), CV_32F, &validDescriptors[0]), calcDescriptors);
ts->printf(cvtest::TS::LOG, "Validation file can not be read.\n");
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
return;
}
compareKeypointSets(validKeypoints, calcKeypoints, validDespcriptors, calcDespcriptors);
}
else // Write detector parameters and computed keypoints as validation data.
{
fs.open(resFilename, FileStorage::WRITE);
if (!fs.isOpened())
{
ts->printf(cvtest::TS::LOG, "File %s can not be opened to write.\n", resFilename.c_str());
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
return;
}
else
{
write(fs, "keypoints", calcKeypoints);
write(fs, "descriptors", (Mat)calcDespcriptors);
}
}
} }
void CV_GPU_SURFTest::run( int /*start_from*/ ) void CV_GPU_SURFTest::run( int /*start_from*/ )
{ {
SURF_GPU fdetector; emptyDataTest();
accuracyTest();
emptyDataTest(fdetector);
regressionTest(fdetector);
} }
TEST(SURF, empty_data_and_regression) { CV_GPU_SURFTest test; test.safe_run(); } TEST(SURF, empty_data_and_accuracy) { CV_GPU_SURFTest test; test.safe_run(); }
...@@ -264,10 +264,11 @@ TEST(SURF) ...@@ -264,10 +264,11 @@ TEST(SURF)
SURF surf; SURF surf;
vector<KeyPoint> keypoints1, keypoints2; vector<KeyPoint> keypoints1, keypoints2;
vector<float> descriptors1, descriptors2;
CPU_ON; CPU_ON;
surf(src1, Mat(), keypoints1); surf(src1, Mat(), keypoints1, descriptors1);
surf(src2, Mat(), keypoints2); surf(src2, Mat(), keypoints2, descriptors2);
CPU_OFF; CPU_OFF;
gpu::SURF_GPU d_surf; gpu::SURF_GPU d_surf;
...@@ -275,8 +276,8 @@ TEST(SURF) ...@@ -275,8 +276,8 @@ TEST(SURF)
gpu::GpuMat d_descriptors1, d_descriptors2; gpu::GpuMat d_descriptors1, d_descriptors2;
GPU_ON; GPU_ON;
d_surf(d_src1, gpu::GpuMat(), d_keypoints1); d_surf(d_src1, gpu::GpuMat(), d_keypoints1, d_descriptors1);
d_surf(d_src2, gpu::GpuMat(), d_keypoints2); d_surf(d_src2, gpu::GpuMat(), d_keypoints2, d_descriptors2);
GPU_OFF; GPU_OFF;
} }
......
...@@ -51,10 +51,10 @@ int main(int argc, char* argv[]) ...@@ -51,10 +51,10 @@ int main(int argc, char* argv[])
vector<KeyPoint> keypoints1, keypoints2; vector<KeyPoint> keypoints1, keypoints2;
vector<float> descriptors1, descriptors2; vector<float> descriptors1, descriptors2;
vector<DMatch> matches; vector<DMatch> matches;
SURF_GPU::downloadKeypoints(keypoints1GPU, keypoints1); surf.downloadKeypoints(keypoints1GPU, keypoints1);
SURF_GPU::downloadKeypoints(keypoints2GPU, keypoints2); surf.downloadKeypoints(keypoints2GPU, keypoints2);
SURF_GPU::downloadDescriptors(descriptors1GPU, descriptors1); surf.downloadDescriptors(descriptors1GPU, descriptors1);
SURF_GPU::downloadDescriptors(descriptors2GPU, descriptors2); surf.downloadDescriptors(descriptors2GPU, descriptors2);
BruteForceMatcher_GPU< L2<float> >::matchDownload(trainIdx, distance, matches); BruteForceMatcher_GPU< L2<float> >::matchDownload(trainIdx, distance, matches);
// drawing the results // drawing the results
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册