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.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2008, Willow Garage Inc., 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 "precomp.hpp"
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
2011-05-31 22:24:45 +08:00
|
|
|
|
|
|
|
size_t KeyPoint::hash() const
|
|
|
|
{
|
|
|
|
size_t _Val = 2166136261U, scale = 16777619U;
|
|
|
|
Cv32suf u;
|
|
|
|
u.f = pt.x; _Val = (scale * _Val) ^ u.u;
|
|
|
|
u.f = pt.y; _Val = (scale * _Val) ^ u.u;
|
|
|
|
u.f = size; _Val = (scale * _Val) ^ u.u;
|
|
|
|
u.f = angle; _Val = (scale * _Val) ^ u.u;
|
|
|
|
u.f = response; _Val = (scale * _Val) ^ u.u;
|
|
|
|
_Val = (scale * _Val) ^ ((size_t) octave);
|
|
|
|
_Val = (scale * _Val) ^ ((size_t) class_id);
|
|
|
|
return _Val;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
void write(FileStorage& fs, const string& objname, const vector<KeyPoint>& keypoints)
|
|
|
|
{
|
|
|
|
WriteStructContext ws(fs, objname, CV_NODE_SEQ + CV_NODE_FLOW);
|
|
|
|
|
|
|
|
int i, npoints = (int)keypoints.size();
|
|
|
|
for( i = 0; i < npoints; i++ )
|
|
|
|
{
|
|
|
|
const KeyPoint& kpt = keypoints[i];
|
|
|
|
write(fs, kpt.pt.x);
|
|
|
|
write(fs, kpt.pt.y);
|
|
|
|
write(fs, kpt.size);
|
|
|
|
write(fs, kpt.angle);
|
|
|
|
write(fs, kpt.response);
|
|
|
|
write(fs, kpt.octave);
|
2010-09-17 20:32:57 +08:00
|
|
|
write(fs, kpt.class_id);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void read(const FileNode& node, vector<KeyPoint>& keypoints)
|
|
|
|
{
|
|
|
|
keypoints.resize(0);
|
|
|
|
FileNodeIterator it = node.begin(), it_end = node.end();
|
|
|
|
for( ; it != it_end; )
|
|
|
|
{
|
|
|
|
KeyPoint kpt;
|
2010-09-17 20:32:57 +08:00
|
|
|
it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave >> kpt.class_id;
|
2010-05-12 01:44:00 +08:00
|
|
|
keypoints.push_back(kpt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-15 23:19:11 +08:00
|
|
|
void KeyPoint::convert(const std::vector<KeyPoint>& keypoints, std::vector<Point2f>& points2f,
|
|
|
|
const vector<int>& keypointIndexes)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-06-15 23:19:11 +08:00
|
|
|
if( keypointIndexes.empty() )
|
|
|
|
{
|
|
|
|
points2f.resize( keypoints.size() );
|
|
|
|
for( size_t i = 0; i < keypoints.size(); i++ )
|
|
|
|
points2f[i] = keypoints[i].pt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
points2f.resize( keypointIndexes.size() );
|
|
|
|
for( size_t i = 0; i < keypointIndexes.size(); i++ )
|
|
|
|
{
|
|
|
|
int idx = keypointIndexes[i];
|
|
|
|
if( idx >= 0 )
|
|
|
|
points2f[i] = keypoints[idx].pt;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CV_Error( CV_StsBadArg, "keypointIndexes has element < 0. TODO: process this case" );
|
|
|
|
//points2f[i] = Point2f(-1, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-06-15 23:19:11 +08:00
|
|
|
void KeyPoint::convert( const std::vector<Point2f>& points2f, std::vector<KeyPoint>& keypoints,
|
2010-05-12 01:44:00 +08:00
|
|
|
float size, float response, int octave, int class_id )
|
|
|
|
{
|
2011-08-07 03:42:29 +08:00
|
|
|
keypoints.resize(points2f.size());
|
2010-06-15 23:19:11 +08:00
|
|
|
for( size_t i = 0; i < points2f.size(); i++ )
|
|
|
|
keypoints[i] = KeyPoint(points2f[i], size, -1, response, octave, class_id);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-07-28 18:47:48 +08:00
|
|
|
float KeyPoint::overlap( const KeyPoint& kp1, const KeyPoint& kp2 )
|
|
|
|
{
|
2010-07-30 22:21:55 +08:00
|
|
|
float a = kp1.size * 0.5f;
|
|
|
|
float b = kp2.size * 0.5f;
|
|
|
|
float a_2 = a * a;
|
|
|
|
float b_2 = b * b;
|
2010-07-28 18:47:48 +08:00
|
|
|
|
2010-07-30 22:21:55 +08:00
|
|
|
Point2f p1 = kp1.pt;
|
|
|
|
Point2f p2 = kp2.pt;
|
2010-08-05 21:29:43 +08:00
|
|
|
float c = (float)norm( p1 - p2 );
|
2010-07-28 18:47:48 +08:00
|
|
|
|
|
|
|
float ovrl = 0.f;
|
2010-07-30 22:21:55 +08:00
|
|
|
|
|
|
|
// one circle is completely encovered by the other => no intersection points!
|
|
|
|
if( min( a, b ) + c <= max( a, b ) )
|
|
|
|
return min( a_2, b_2 ) / max( a_2, b_2 );
|
|
|
|
|
|
|
|
if( c < a + b ) // circles intersect
|
2010-07-28 18:47:48 +08:00
|
|
|
{
|
2010-07-30 22:21:55 +08:00
|
|
|
float c_2 = c * c;
|
|
|
|
float cosAlpha = ( b_2 + c_2 - a_2 ) / ( kp2.size * c );
|
|
|
|
float cosBeta = ( a_2 + c_2 - b_2 ) / ( kp1.size * c );
|
|
|
|
float alpha = acos( cosAlpha );
|
|
|
|
float beta = acos( cosBeta );
|
|
|
|
float sinAlpha = sin(alpha);
|
|
|
|
float sinBeta = sin(beta);
|
2010-07-28 18:47:48 +08:00
|
|
|
|
2010-07-30 22:21:55 +08:00
|
|
|
float segmentAreaA = a_2 * beta;
|
|
|
|
float segmentAreaB = b_2 * alpha;
|
2010-07-28 18:47:48 +08:00
|
|
|
|
2010-07-30 22:21:55 +08:00
|
|
|
float triangleAreaA = a_2 * sinBeta * cosBeta;
|
|
|
|
float triangleAreaB = b_2 * sinAlpha * cosAlpha;
|
2010-07-28 18:47:48 +08:00
|
|
|
|
2010-07-30 22:21:55 +08:00
|
|
|
float intersectionArea = segmentAreaA + segmentAreaB - triangleAreaA - triangleAreaB;
|
2010-08-05 21:29:43 +08:00
|
|
|
float unionArea = (a_2 + b_2) * (float)CV_PI - intersectionArea;
|
2010-07-28 18:47:48 +08:00
|
|
|
|
2010-07-30 22:21:55 +08:00
|
|
|
ovrl = intersectionArea / unionArea;
|
2010-07-28 18:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ovrl;
|
|
|
|
}
|
2012-03-15 22:36:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
struct KeypointResponseGreaterThanThreshold
|
|
|
|
{
|
|
|
|
KeypointResponseGreaterThanThreshold(float _value) :
|
|
|
|
value(_value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
inline bool operator()(const KeyPoint& kpt) const
|
|
|
|
{
|
|
|
|
return kpt.response >= value;
|
|
|
|
}
|
|
|
|
float value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct KeypointResponseGreater
|
|
|
|
{
|
|
|
|
inline bool operator()(const KeyPoint& kp1, const KeyPoint& kp2) const
|
|
|
|
{
|
|
|
|
return kp1.response > kp2.response;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// takes keypoints and culls them by the response
|
|
|
|
void KeyPointsFilter::retainBest(vector<KeyPoint>& keypoints, int n_points)
|
|
|
|
{
|
|
|
|
//this is only necessary if the keypoints size is greater than the number of desired points.
|
|
|
|
if( n_points > 0 && keypoints.size() > (size_t)n_points )
|
|
|
|
{
|
|
|
|
if (n_points==0)
|
|
|
|
{
|
|
|
|
keypoints.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//first use nth element to partition the keypoints into the best and worst.
|
|
|
|
std::nth_element(keypoints.begin(), keypoints.begin() + n_points, keypoints.end(), KeypointResponseGreater());
|
|
|
|
//this is the boundary response, and in the case of FAST may be ambigous
|
|
|
|
float ambiguous_response = keypoints[n_points - 1].response;
|
|
|
|
//use std::partition to grab all of the keypoints with the boundary response.
|
|
|
|
vector<KeyPoint>::const_iterator new_end =
|
|
|
|
std::partition(keypoints.begin() + n_points, keypoints.end(),
|
|
|
|
KeypointResponseGreaterThanThreshold(ambiguous_response));
|
|
|
|
//resize the keypoints, given this new end point. nth_element and partition reordered the points inplace
|
|
|
|
keypoints.resize(new_end - keypoints.begin());
|
|
|
|
}
|
|
|
|
}
|
2010-07-28 18:47:48 +08:00
|
|
|
|
2011-05-02 01:38:52 +08:00
|
|
|
struct RoiPredicate
|
|
|
|
{
|
|
|
|
RoiPredicate( const Rect& _r ) : r(_r)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator()( const KeyPoint& keyPt ) const
|
|
|
|
{
|
|
|
|
return !r.contains( keyPt.pt );
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect r;
|
|
|
|
};
|
|
|
|
|
|
|
|
void KeyPointsFilter::runByImageBorder( vector<KeyPoint>& keypoints, Size imageSize, int borderSize )
|
|
|
|
{
|
|
|
|
if( borderSize > 0)
|
|
|
|
{
|
2011-10-21 16:38:47 +08:00
|
|
|
if (imageSize.height <= borderSize * 2 || imageSize.width <= borderSize * 2)
|
|
|
|
keypoints.clear();
|
|
|
|
else
|
|
|
|
keypoints.erase( remove_if(keypoints.begin(), keypoints.end(),
|
|
|
|
RoiPredicate(Rect(Point(borderSize, borderSize),
|
|
|
|
Point(imageSize.width - borderSize, imageSize.height - borderSize)))),
|
|
|
|
keypoints.end() );
|
2011-05-02 01:38:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SizePredicate
|
|
|
|
{
|
|
|
|
SizePredicate( float _minSize, float _maxSize ) : minSize(_minSize), maxSize(_maxSize)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator()( const KeyPoint& keyPt ) const
|
|
|
|
{
|
|
|
|
float size = keyPt.size;
|
|
|
|
return (size < minSize) || (size > maxSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
float minSize, maxSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
void KeyPointsFilter::runByKeypointSize( vector<KeyPoint>& keypoints, float minSize, float maxSize )
|
|
|
|
{
|
|
|
|
CV_Assert( minSize >= 0 );
|
|
|
|
CV_Assert( maxSize >= 0);
|
|
|
|
CV_Assert( minSize <= maxSize );
|
|
|
|
|
|
|
|
keypoints.erase( remove_if(keypoints.begin(), keypoints.end(), SizePredicate(minSize, maxSize)),
|
|
|
|
keypoints.end() );
|
|
|
|
}
|
|
|
|
|
2011-05-05 18:10:46 +08:00
|
|
|
class MaskPredicate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MaskPredicate( const Mat& _mask ) : mask(_mask) {}
|
|
|
|
bool operator() (const KeyPoint& key_pt) const
|
|
|
|
{
|
|
|
|
return mask.at<uchar>( (int)(key_pt.pt.y + 0.5f), (int)(key_pt.pt.x + 0.5f) ) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Mat mask;
|
2012-06-15 21:04:17 +08:00
|
|
|
MaskPredicate& operator=(const MaskPredicate&);
|
2011-05-05 18:10:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void KeyPointsFilter::runByPixelsMask( vector<KeyPoint>& keypoints, const Mat& mask )
|
|
|
|
{
|
|
|
|
if( mask.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
keypoints.erase(remove_if(keypoints.begin(), keypoints.end(), MaskPredicate(mask)), keypoints.end());
|
|
|
|
}
|
|
|
|
|
2011-06-01 02:18:02 +08:00
|
|
|
struct KeyPoint_LessThan
|
|
|
|
{
|
|
|
|
KeyPoint_LessThan(const vector<KeyPoint>& _kp) : kp(&_kp) {}
|
|
|
|
bool operator()(int i, int j) const
|
|
|
|
{
|
|
|
|
const KeyPoint& kp1 = (*kp)[i];
|
|
|
|
const KeyPoint& kp2 = (*kp)[j];
|
|
|
|
if( kp1.pt.x != kp2.pt.x )
|
|
|
|
return kp1.pt.x < kp2.pt.x;
|
|
|
|
if( kp1.pt.y != kp2.pt.y )
|
|
|
|
return kp1.pt.y < kp2.pt.y;
|
|
|
|
if( kp1.size != kp2.size )
|
|
|
|
return kp1.size > kp2.size;
|
|
|
|
if( kp1.angle != kp2.angle )
|
|
|
|
return kp1.angle < kp2.angle;
|
|
|
|
if( kp1.response != kp2.response )
|
|
|
|
return kp1.response > kp2.response;
|
|
|
|
if( kp1.octave != kp2.octave )
|
|
|
|
return kp1.octave > kp2.octave;
|
|
|
|
if( kp1.class_id != kp2.class_id )
|
|
|
|
return kp1.class_id > kp2.class_id;
|
|
|
|
|
|
|
|
return i < j;
|
|
|
|
}
|
|
|
|
const vector<KeyPoint>* kp;
|
|
|
|
};
|
|
|
|
|
|
|
|
void KeyPointsFilter::removeDuplicated( vector<KeyPoint>& keypoints )
|
|
|
|
{
|
|
|
|
int i, j, n = (int)keypoints.size();
|
|
|
|
vector<int> kpidx(n);
|
|
|
|
vector<uchar> mask(n, (uchar)1);
|
|
|
|
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
kpidx[i] = i;
|
|
|
|
std::sort(kpidx.begin(), kpidx.end(), KeyPoint_LessThan(keypoints));
|
|
|
|
for( i = 1, j = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
KeyPoint& kp1 = keypoints[kpidx[i]];
|
|
|
|
KeyPoint& kp2 = keypoints[kpidx[j]];
|
|
|
|
if( kp1.pt.x != kp2.pt.x || kp1.pt.y != kp2.pt.y ||
|
|
|
|
kp1.size != kp2.size || kp1.angle != kp2.angle )
|
|
|
|
j = i;
|
|
|
|
else
|
|
|
|
mask[kpidx[i]] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = j = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
if( mask[i] )
|
|
|
|
{
|
|
|
|
if( i != j )
|
|
|
|
keypoints[j] = keypoints[i];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keypoints.resize(j);
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|