1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

fixed SURF_GPU (fails on empty data)

added test for SURF_GPU and reprojectImageTo3D
This commit is contained in:
Vladislav Vinogradov
2011-02-07 10:12:04 +00:00
parent f42a449df9
commit 5cd06d6a36
6 changed files with 356 additions and 55 deletions
+2 -2
View File
@@ -51,10 +51,10 @@ set_target_properties(${the_target} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/"
)
add_dependencies(${the_target} opencv_ts opencv_gpu opencv_highgui opencv_imgproc)
add_dependencies(${the_target} opencv_ts opencv_gpu opencv_highgui opencv_imgproc opencv_calib3d)
# Add the required libraries for linking:
target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} opencv_ts opencv_gpu opencv_highgui opencv_imgproc)
target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS} opencv_ts opencv_gpu opencv_highgui opencv_imgproc opencv_calib3d)
enable_testing()
get_target_property(LOC ${the_target} LOCATION)
+228
View File
@@ -0,0 +1,228 @@
/*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*/
#include "gputest.hpp"
#include <string>
using namespace cv;
using namespace cv::gpu;
using namespace std;
const string FEATURES2D_DIR = "features2d";
const string IMAGE_FILENAME = "aloe.png";
const string VALID_FILE_NAME = "surf.xml.gz";
class CV_GPU_SURFTest : public CvTest
{
public:
CV_GPU_SURFTest() :
CvTest( "GPU-SURF", "SURF_GPU")
{
}
protected:
bool isSimilarKeypoints(const KeyPoint& p1, const KeyPoint& p2);
void compareKeypointSets(const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints,
const Mat& validDescriptors, const Mat& calcDescriptors);
void emptyDataTest(SURF_GPU& fdetector);
void regressionTest(SURF_GPU& fdetector);
virtual void run(int);
};
void CV_GPU_SURFTest::emptyDataTest(SURF_GPU& fdetector)
{
GpuMat image;
vector<KeyPoint> keypoints;
vector<float> descriptors;
try
{
fdetector(image, GpuMat(), keypoints, descriptors);
}
catch(...)
{
ts->printf( CvTS::LOG, "detect() on empty image must not generate exception (1).\n" );
ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );
}
if( !keypoints.empty() )
{
ts->printf( CvTS::LOG, "detect() on empty image must return empty keypoints vector (1).\n" );
ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );
return;
}
if( !descriptors.empty() )
{
ts->printf( CvTS::LOG, "detect() on empty image must return empty descriptors vector (1).\n" );
ts->set_failed_test_info( CvTS::FAIL_INVALID_OUTPUT );
return;
}
}
bool CV_GPU_SURFTest::isSimilarKeypoints(const KeyPoint& p1, const KeyPoint& p2)
{
const float maxPtDif = 1.f;
const float maxSizeDif = 1.f;
const float maxAngleDif = 2.f;
const float maxResponseDif = 0.1f;
float dist = (float)norm( p1.pt - p2.pt );
return (dist < maxPtDif &&
fabs(p1.size - p2.size) < maxSizeDif &&
abs(p1.angle - p2.angle) < maxAngleDif &&
abs(p1.response - p2.response) < maxResponseDif &&
p1.octave == p2.octave &&
p1.class_id == p2.class_id );
}
void CV_GPU_SURFTest::compareKeypointSets(const vector<KeyPoint>& validKeypoints, const vector<KeyPoint>& calcKeypoints,
const Mat& validDescriptors, const Mat& calcDescriptors)
{
if (validKeypoints.size() != calcKeypoints.size())
{
ts->printf(CvTS::LOG, "Keypoints sizes doesn't equal (validCount = %d, calcCount = %d).\n",
validKeypoints.size(), calcKeypoints.size());
ts->set_failed_test_info(CvTS::FAIL_INVALID_OUTPUT);
return;
}
if (validDescriptors.size() != calcDescriptors.size())
{
ts->printf(CvTS::LOG, "Descriptors sizes doesn't equal.\n");
ts->set_failed_test_info(CvTS::FAIL_INVALID_OUTPUT);
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->set_failed_test_info( CvTS::FAIL_BAD_ACCURACY );
return;
}
if (norm(validDescriptors.row(v), calcDescriptors.row(nearestIdx), NORM_L2) > 1.0f)
{
ts->set_failed_test_info( CvTS::FAIL_BAD_ACCURACY );
return;
}
}
}
void CV_GPU_SURFTest::regressionTest(SURF_GPU& fdetector)
{
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.
GpuMat image(imread(imgFilename, 0));
if (image.empty())
{
ts->printf( CvTS::LOG, "Image %s can not be read.\n", imgFilename.c_str() );
ts->set_failed_test_info( CvTS::FAIL_INVALID_TEST_DATA );
return;
}
FileStorage fs(resFilename, FileStorage::READ);
// 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;
GpuMat calcDespcriptors;
fdetector(image, mask, calcKeypoints, calcDespcriptors);
if (fs.isOpened()) // Compare computed and valid keypoints.
{
// Read validation keypoints set.
vector<KeyPoint> validKeypoints;
Mat validDespcriptors;
read(fs["keypoints"], validKeypoints);
read(fs["descriptors"], validDespcriptors);
if (validKeypoints.empty() || validDespcriptors.empty())
{
ts->printf(CvTS::LOG, "Validation file can not be read.\n");
ts->set_failed_test_info(CvTS::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(CvTS::LOG, "File %s can not be opened to write.\n", resFilename.c_str());
ts->set_failed_test_info(CvTS::FAIL_INVALID_TEST_DATA);
return;
}
else
{
write(fs, "keypoints", calcKeypoints);
write(fs, "descriptors", (Mat)calcDespcriptors);
}
}
}
void CV_GPU_SURFTest::run( int /*start_from*/ )
{
SURF_GPU fdetector;
emptyDataTest(fdetector);
regressionTest(fdetector);
}
CV_GPU_SURFTest CV_GPU_SURF_test;
+1
View File
@@ -52,6 +52,7 @@
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include "cxts.h"
/****************************************************************************************/
+46
View File
@@ -912,6 +912,51 @@ struct CV_GpuNormTest : CvTest
}
};
////////////////////////////////////////////////////////////////////////////////
// reprojectImageTo3D
class CV_GpuReprojectImageTo3DTest : public CvTest
{
public:
CV_GpuReprojectImageTo3DTest() : CvTest("GPU-ReprojectImageTo3D", "reprojectImageTo3D") {}
protected:
void run(int)
{
Mat disp(320, 240, CV_8UC1);
RNG rng(*ts->get_rng());
rng.fill(disp, RNG::UNIFORM, Scalar(5), Scalar(30));
Mat Q(4, 4, CV_32FC1);
rng.fill(Q, RNG::UNIFORM, Scalar(0.1), Scalar(1));
Mat cpures;
GpuMat gpures;
reprojectImageTo3D(disp, cpures, Q, false);
reprojectImageTo3D(GpuMat(disp), gpures, Q);
Mat temp = gpures;
for (int y = 0; y < cpures.rows; ++y)
{
const Vec3f* cpu_row = cpures.ptr<Vec3f>(y);
const Vec4f* gpu_row = temp.ptr<Vec4f>(y);
for (int x = 0; x < cpures.cols; ++x)
{
Vec3f a = cpu_row[x];
Vec4f b = gpu_row[x];
if (fabs(a[0] - b[0]) > 1e-5 || fabs(a[1] - b[1]) > 1e-5 || fabs(a[2] - b[2]) > 1e-5)
{
ts->set_failed_test_info(CvTS::FAIL_INVALID_OUTPUT);
return;
}
}
}
}
};
/////////////////////////////////////////////////////////////////////////////
/////////////////// tests registration /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
@@ -933,3 +978,4 @@ CV_GpuCornerHarrisTest CV_GpuCornerHarris_test;
CV_GpuCornerMinEigenValTest CV_GpuCornerMinEigenVal_test;
CV_GpuColumnSumTest CV_GpuColumnSum_test;
CV_GpuNormTest CV_GpuNormTest_test;
CV_GpuReprojectImageTo3DTest CV_GpuReprojectImageTo3D_test;