Commit 6f5d9835 authored by Alexander Alekhin's avatar Alexander Alekhin

Merge remote-tracking branch 'upstream/3.4' into merge-3.4

parents debdc26b 9e190bcd
......@@ -628,208 +628,6 @@ class CV_EXPORTS_W BinaryDescriptor : public Algorithm
cv::Mat_<float> tempVecLineFit; //the vector used in line fit function;
/** Compare doubles by relative error.
The resulting rounding error after floating point computations
depend on the specific operations done. The same number computed by
different algorithms could present different rounding errors. For a
useful comparison, an estimation of the relative rounding error
should be considered and compared to a factor times EPS. The factor
should be related to the accumulated rounding error in the chain of
computation. Here, as a simplification, a fixed factor is used.
*/
static int double_equal( double a, double b )
{
double abs_diff, aa, bb, abs_max;
/* trivial case */
if( a == b )
return true;
abs_diff = fabs( a - b );
aa = fabs( a );
bb = fabs( b );
abs_max = aa > bb ? aa : bb;
/* DBL_MIN is the smallest normalized number, thus, the smallest
number whose relative error is bounded by DBL_EPSILON. For
smaller numbers, the same quantization steps as for DBL_MIN
are used. Then, for smaller numbers, a meaningful "relative"
error should be computed by dividing the difference by DBL_MIN. */
if( abs_max < DBL_MIN )
abs_max = DBL_MIN;
/* equal if relative error <= factor x eps */
return ( abs_diff / abs_max ) <= ( RELATIVE_ERROR_FACTOR * DBL_EPSILON );
}
/** Computes the natural logarithm of the absolute value of
the gamma function of x using the Lanczos approximation.
See http://www.rskey.org/gamma.htm
The formula used is
@f[
\Gamma(x) = \frac{ \sum_{n=0}^{N} q_n x^n }{ \Pi_{n=0}^{N} (x+n) }
(x+5.5)^{x+0.5} e^{-(x+5.5)}
@f]
so
@f[
\log\Gamma(x) = \log\left( \sum_{n=0}^{N} q_n x^n \right)
+ (x+0.5) \log(x+5.5) - (x+5.5) - \sum_{n=0}^{N} \log(x+n)
@f]
and
q0 = 75122.6331530,
q1 = 80916.6278952,
q2 = 36308.2951477,
q3 = 8687.24529705,
q4 = 1168.92649479,
q5 = 83.8676043424,
q6 = 2.50662827511.
*/
static double log_gamma_lanczos( double x )
{
static double q[7] =
{ 75122.6331530, 80916.6278952, 36308.2951477, 8687.24529705, 1168.92649479, 83.8676043424, 2.50662827511 };
double a = ( x + 0.5 ) * log( x + 5.5 ) - ( x + 5.5 );
double b = 0.0;
int n;
for ( n = 0; n < 7; n++ )
{
a -= log( x + (double) n );
b += q[n] * pow( x, (double) n );
}
return a + log( b );
}
/** Computes the natural logarithm of the absolute value of
the gamma function of x using Windschitl method.
See http://www.rskey.org/gamma.htm
The formula used is
@f[
\Gamma(x) = \sqrt{\frac{2\pi}{x}} \left( \frac{x}{e}
\sqrt{ x\sinh(1/x) + \frac{1}{810x^6} } \right)^x
@f]
so
@f[
\log\Gamma(x) = 0.5\log(2\pi) + (x-0.5)\log(x) - x
+ 0.5x\log\left( x\sinh(1/x) + \frac{1}{810x^6} \right).
@f]
This formula is a good approximation when x > 15.
*/
static double log_gamma_windschitl( double x )
{
return 0.918938533204673 + ( x - 0.5 ) * log( x ) - x + 0.5 * x * log( x * sinh( 1 / x ) + 1 / ( 810.0 * pow( x, 6.0 ) ) );
}
/** Computes -log10(NFA).
NFA stands for Number of False Alarms:
@f[
\mathrm{NFA} = NT \cdot B(n,k,p)
@f]
- NT - number of tests
- B(n,k,p) - tail of binomial distribution with parameters n,k and p:
@f[
B(n,k,p) = \sum_{j=k}^n
\left(\begin{array}{c}n\\j\end{array}\right)
p^{j} (1-p)^{n-j}
@f]
The value -log10(NFA) is equivalent but more intuitive than NFA:
- -1 corresponds to 10 mean false alarms
- 0 corresponds to 1 mean false alarm
- 1 corresponds to 0.1 mean false alarms
- 2 corresponds to 0.01 mean false alarms
- ...
Used this way, the bigger the value, better the detection,
and a logarithmic scale is used.
@param n,k,p binomial parameters.
@param logNT logarithm of Number of Tests
The computation is based in the gamma function by the following
relation:
@f[
\left(\begin{array}{c}n\\k\end{array}\right)
= \frac{ \Gamma(n+1) }{ \Gamma(k+1) \cdot \Gamma(n-k+1) }.
@f]
We use efficient algorithms to compute the logarithm of
the gamma function.
To make the computation faster, not all the sum is computed, part
of the terms are neglected based on a bound to the error obtained
(an error of 10% in the result is accepted).
*/
static double nfa( int n, int k, double p, double logNT )
{
double tolerance = 0.1; /* an error of 10% in the result is accepted */
double log1term, term, bin_term, mult_term, bin_tail, err, p_term;
int i;
/* check parameters */
if( n < 0 || k < 0 || k > n || p <= 0.0 || p >= 1.0 )
CV_Error(Error::StsBadArg, "nfa: wrong n, k or p values.\n");
/* trivial cases */
if( n == 0 || k == 0 )
return -logNT;
if( n == k )
return -logNT - (double) n * log10( p );
/* probability term */
p_term = p / ( 1.0 - p );
/* compute the first term of the series */
/*
binomial_tail(n,k,p) = sum_{i=k}^n bincoef(n,i) * p^i * (1-p)^{n-i}
where bincoef(n,i) are the binomial coefficients.
But
bincoef(n,k) = gamma(n+1) / ( gamma(k+1) * gamma(n-k+1) ).
We use this to compute the first term. Actually the log of it.
*/
log1term = log_gamma( (double) n + 1.0 )- log_gamma( (double ) k + 1.0 )- log_gamma( (double ) ( n - k ) + 1.0 )
+ (double) k * log( p )
+ (double) ( n - k ) * log( 1.0 - p );
term = exp( log1term );
/* in some cases no more computations are needed */
if( double_equal( term, 0.0 ) )
{ /* the first term is almost zero */
if( (double) k > (double) n * p ) /* at begin or end of the tail? */
return -log1term / MLN10 - logNT; /* end: use just the first term */
else
return -logNT; /* begin: the tail is roughly 1 */
}
/* compute more terms if needed */
bin_tail = term;
for ( i = k + 1; i <= n; i++ )
{
/* As
term_i = bincoef(n,i) * p^i * (1-p)^(n-i)
and
bincoef(n,i)/bincoef(n,i-1) = n-i+1 / i,
then,
term_i / term_i-1 = (n-i+1)/i * p/(1-p)
and
term_i = term_i-1 * (n-i+1)/i * p/(1-p).
p/(1-p) is computed only once and stored in 'p_term'.
*/
bin_term = (double) ( n - i + 1 ) / (double) i;
mult_term = bin_term * p_term;
term *= mult_term;
bin_tail += term;
if( bin_term < 1.0 )
{
/* When bin_term<1 then mult_term_j<mult_term_i for j>i.
Then, the error on the binomial tail when truncated at
the i term can be bounded by a geometric series of form
term_i * sum mult_term_i^j. */
err = term * ( ( 1.0 - pow( mult_term, (double) ( n - i + 1 ) ) ) / ( 1.0 - mult_term ) - 1.0 );
/* One wants an error at most of tolerance*final_result, or:
tolerance * abs(-log10(bin_tail)-logNT).
Now, the error that can be accepted on bin_tail is
given by tolerance*final_result divided by the derivative
of -log10(x) when x=bin_tail. that is:
tolerance * abs(-log10(bin_tail)-logNT) / (1/bin_tail)
Finally, we truncate the tail if the error is less than:
tolerance * abs(-log10(bin_tail)-logNT) * bin_tail */
if( err < tolerance * fabs( -log10( bin_tail ) - logNT ) * bin_tail )
break;
}
}
return -log10( bin_tail ) - logNT;
}
};
// Specifies a vector of lines.
......
/*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.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2014, Biagio Montesano, 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 the copyright holders 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 "perf_precomp.hpp"
namespace opencv_test { namespace {
typedef perf::TestBaseWithParam<std::string> file_str;
#define IMAGES \
"cv/line_descriptor/cameraman.jpg", "cv/shared/lena.png"
PERF_TEST_P(file_str, descriptors, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat descriptors;
std::vector<KeyLine> keylines;
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
TEST_CYCLE()
{
bd->detect( frame, keylines );
bd->compute( frame, keylines, descriptors );
}
SANITY_CHECK_NOTHING();
}
}} // namespace
/*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.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2014, Biagio Montesano, 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 the copyright holders 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 "perf_precomp.hpp"
namespace opencv_test { namespace {
typedef perf::TestBaseWithParam<std::string> file_str;
#define IMAGES \
"cv/line_descriptor/cameraman.jpg", "cv/shared/lena.png"
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output );
void createMatFromVec( const std::vector<KeyLine>& linesVec, Mat& output )
{
output = Mat( (int) linesVec.size(), 17, CV_32FC1 );
for ( int i = 0; i < (int) linesVec.size(); i++ )
{
std::vector<float> klData;
KeyLine kl = linesVec[i];
klData.push_back( kl.angle );
klData.push_back( (float) kl.class_id );
klData.push_back( kl.ePointInOctaveX );
klData.push_back( kl.ePointInOctaveY );
klData.push_back( kl.endPointX );
klData.push_back( kl.endPointY );
klData.push_back( kl.lineLength );
klData.push_back( (float) kl.numOfPixels );
klData.push_back( (float) kl.octave );
klData.push_back( kl.pt.x );
klData.push_back( kl.pt.y );
klData.push_back( kl.response );
klData.push_back( kl.sPointInOctaveX );
klData.push_back( kl.sPointInOctaveY );
klData.push_back( kl.size );
klData.push_back( kl.startPointX );
klData.push_back( kl.startPointY );
float* pointerToRow = output.ptr<float>( i );
for ( int j = 0; j < 17; j++ )
{
*pointerToRow = klData[j];
pointerToRow++;
}
}
}
PERF_TEST_P(file_str, detect, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat lines;
std::vector<KeyLine> keylines;
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
TEST_CYCLE()
{
bd->detect( frame, keylines );
createMatFromVec( keylines, lines );
}
SANITY_CHECK_NOTHING();
}
PERF_TEST_P(file_str, detect_lsd, testing::Values(IMAGES))
{
std::string filename = getDataPath( GetParam() );
std::cout << filename.c_str() << std::endl;
Mat frame = imread( filename, 1 );
if( frame.empty() )
FAIL()<< "Unable to load source image " << filename;
Mat lines;
std::vector<KeyLine> keylines;
Ptr<LSDDetector> lsd = LSDDetector::createLSDDetector();
TEST_CYCLE()
{
lsd->detect( frame, keylines, 2, 1 );
createMatFromVec( keylines, lines );
}
SANITY_CHECK_NOTHING();
}
}} // namespace
......@@ -42,6 +42,8 @@
#include "precomp.hpp"
#ifdef _MSC_VER
#pragma warning(disable:4702) // unreachable code
#if (_MSC_VER <= 1700)
/* This function rounds x to the nearest integer, but rounds halfway cases away from zero. */
static inline double round(double x)
......@@ -690,7 +692,7 @@ void BinaryDescriptor::computeImpl( const Mat& imageSrc, std::vector<KeyLine>& k
int BinaryDescriptor::OctaveKeyLines( cv::Mat& image, ScaleLines &keyLines )
{
#if 0
/* final number of extracted lines */
unsigned int numOfFinalLine = 0;
......@@ -1023,6 +1025,10 @@ int BinaryDescriptor::OctaveKeyLines( cv::Mat& image, ScaleLines &keyLines )
delete[] scale;
return 1;
#else
CV_UNUSED(image); CV_UNUSED(keyLines);
CV_Error(Error::StsNotImplemented, "Implementation has been removed due original code license issues");
#endif
}
int BinaryDescriptor::computeLBD( ScaleLines &keyLines, bool useDetectionData )
......@@ -2237,7 +2243,7 @@ int BinaryDescriptor::EDLineDetector::EdgeDrawing( cv::Mat &image, EdgeChains &e
int BinaryDescriptor::EDLineDetector::EDline( cv::Mat &image, LineChains &lines )
{
#if 0
//first, call EdgeDrawing function to extract edges
EdgeChains edges;
if( ( EdgeDrawing( image, edges ) ) != 1 )
......@@ -2475,6 +2481,10 @@ int BinaryDescriptor::EDLineDetector::EDline( cv::Mat &image, LineChains &lines
lines.numOfLines = numOfLines;
return 1;
#else
CV_UNUSED(image); CV_UNUSED(lines);
CV_Error(Error::StsNotImplemented, "Implementation has been removed due original code license issues");
#endif
}
double BinaryDescriptor::EDLineDetector::LeastSquaresLineFit_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS,
......@@ -2641,88 +2651,14 @@ double BinaryDescriptor::EDLineDetector::LeastSquaresLineFit_( unsigned int *xCo
bool BinaryDescriptor::EDLineDetector::LineValidation_( unsigned int *xCors, unsigned int *yCors, unsigned int offsetS, unsigned int offsetE,
std::vector<double> &lineEquation, float &direction )
{
if( bValidate_ )
{
int n = offsetE - offsetS;
/*first compute the direction of line, make sure that the dark side always be the
*left side of a line.*/
int meanGradientX = 0, meanGradientY = 0;
short *pdxImg = dxImg_.ptr<short>();
short *pdyImg = dyImg_.ptr<short>();
double dx, dy;
std::vector<double> pointDirection;
int index;
for ( int i = 0; i < n; i++ )
{
index = yCors[offsetS] * imageWidth + xCors[offsetS];
offsetS++;
meanGradientX += pdxImg[index];
meanGradientY += pdyImg[index];
dx = (double) pdxImg[index];
dy = (double) pdyImg[index];
pointDirection.push_back( atan2( -dx, dy ) );
}
dx = fabs( lineEquation[1] );
dy = fabs( lineEquation[0] );
if( meanGradientX == 0 && meanGradientY == 0 )
{ //not possible, if happens, it must be a wrong line,
return false;
}
if( meanGradientX > 0 && meanGradientY >= 0 )
{ //first quadrant, and positive direction of X axis.
direction = (float) atan2( -dy, dx ); //line direction is in fourth quadrant
}
if( meanGradientX <= 0 && meanGradientY > 0 )
{ //second quadrant, and positive direction of Y axis.
direction = (float) atan2( dy, dx ); //line direction is in first quadrant
}
if( meanGradientX < 0 && meanGradientY <= 0 )
{ //third quadrant, and negative direction of X axis.
direction = (float) atan2( dy, -dx ); //line direction is in second quadrant
}
if( meanGradientX >= 0 && meanGradientY < 0 )
{ //fourth quadrant, and negative direction of Y axis.
direction = (float) atan2( -dy, -dx ); //line direction is in third quadrant
}
/*then check whether the line is on the border of the image. We don't keep the border line.*/
if( fabs( direction ) < 0.15 || M_PI - fabs( direction ) < 0.15 )
{ //Horizontal line
if( fabs( lineEquation[2] ) < 10 || fabs( imageHeight - fabs( lineEquation[2] ) ) < 10 )
{ //upper border or lower border
return false;
}
}
if( fabs( fabs( direction ) - M_PI * 0.5 ) < 0.15 )
{ //Vertical line
if( fabs( lineEquation[2] ) < 10 || fabs( imageWidth - fabs( lineEquation[2] ) ) < 10 )
{ //left border or right border
return false;
}
}
//count the aligned points on the line which have the same direction as the line.
double disDirection;
int k = 0;
for ( int i = 0; i < n; i++ )
{
disDirection = fabs( direction - pointDirection[i] );
if( fabs( 2 * M_PI - disDirection ) < 0.392699 || disDirection < 0.392699 )
{ //same direction, pi/8 = 0.392699081698724
k++;
}
}
//now compute NFA(Number of False Alarms)
double ret = nfa( n, k, 0.125, logNT_ );
return ( ret > 0 ); //0 corresponds to 1 mean false alarm
}
else
{
return true;
}
CV_UNUSED(xCors); CV_UNUSED(yCors); CV_UNUSED(offsetS); CV_UNUSED(offsetE);
CV_UNUSED(lineEquation); CV_UNUSED(direction);
CV_Error(Error::StsNotImplemented, "Implementation has been removed due original code license issues");
}
int BinaryDescriptor::EDLineDetector::EDline( cv::Mat &image )
{
#if 0
if( ( EDline( image, lines_/*, smoothed*/) ) != 1 )
{
return -1;
......@@ -2744,6 +2680,10 @@ int BinaryDescriptor::EDLineDetector::EDline( cv::Mat &image )
lineSalience_[i] = (float) salience;
}
return 1;
#else
CV_UNUSED(image);
CV_Error(Error::StsNotImplemented, "Implementation has been removed due original code license issues");
#endif
}
}
......
......@@ -376,7 +376,7 @@ TEST( BinaryDescriptor_Descriptors, regression )
* Other tests *
\****************************************************************************************/
TEST( BinaryDescriptor, no_lines_found )
TEST( BinaryDescriptor, DISABLED_no_lines_found )
{
Mat Image = Mat::zeros(100, 100, CV_8U);
Ptr<line_descriptor::BinaryDescriptor> binDescriptor =
......
......@@ -331,7 +331,7 @@ void CV_BinaryDescriptorDetectorTest::run( int )
* Tests registrations *
\****************************************************************************************/
TEST( BinaryDescriptor_Detector, regression )
TEST( BinaryDescriptor_Detector, DISABLED_regression )
{
CV_BinaryDescriptorDetectorTest test( std::string( "edl_detector_keylines_cameraman" ) );
test.safe_run();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment