1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 #include "precomp.hpp"
45 
46 namespace cv
47 {
48 
hash() const49 size_t KeyPoint::hash() const
50 {
51     size_t _Val = 2166136261U, scale = 16777619U;
52     Cv32suf u;
53     u.f = pt.x; _Val = (scale * _Val) ^ u.u;
54     u.f = pt.y; _Val = (scale * _Val) ^ u.u;
55     u.f = size; _Val = (scale * _Val) ^ u.u;
56     u.f = angle; _Val = (scale * _Val) ^ u.u;
57     u.f = response; _Val = (scale * _Val) ^ u.u;
58     _Val = (scale * _Val) ^ ((size_t) octave);
59     _Val = (scale * _Val) ^ ((size_t) class_id);
60     return _Val;
61 }
62 
convert(const std::vector<KeyPoint> & keypoints,std::vector<Point2f> & points2f,const std::vector<int> & keypointIndexes)63 void KeyPoint::convert(const std::vector<KeyPoint>& keypoints, std::vector<Point2f>& points2f,
64                        const std::vector<int>& keypointIndexes)
65 {
66     if( keypointIndexes.empty() )
67     {
68         points2f.resize( keypoints.size() );
69         for( size_t i = 0; i < keypoints.size(); i++ )
70             points2f[i] = keypoints[i].pt;
71     }
72     else
73     {
74         points2f.resize( keypointIndexes.size() );
75         for( size_t i = 0; i < keypointIndexes.size(); i++ )
76         {
77             int idx = keypointIndexes[i];
78             if( idx >= 0 )
79                 points2f[i] = keypoints[idx].pt;
80             else
81             {
82                 CV_Error( CV_StsBadArg, "keypointIndexes has element < 0. TODO: process this case" );
83                 //points2f[i] = Point2f(-1, -1);
84             }
85         }
86     }
87 }
88 
convert(const std::vector<Point2f> & points2f,std::vector<KeyPoint> & keypoints,float size,float response,int octave,int class_id)89 void KeyPoint::convert( const std::vector<Point2f>& points2f, std::vector<KeyPoint>& keypoints,
90                         float size, float response, int octave, int class_id )
91 {
92     keypoints.resize(points2f.size());
93     for( size_t i = 0; i < points2f.size(); i++ )
94         keypoints[i] = KeyPoint(points2f[i], size, -1, response, octave, class_id);
95 }
96 
overlap(const KeyPoint & kp1,const KeyPoint & kp2)97 float KeyPoint::overlap( const KeyPoint& kp1, const KeyPoint& kp2 )
98 {
99     float a = kp1.size * 0.5f;
100     float b = kp2.size * 0.5f;
101     float a_2 = a * a;
102     float b_2 = b * b;
103 
104     Point2f p1 = kp1.pt;
105     Point2f p2 = kp2.pt;
106     float c = (float)norm( p1 - p2 );
107 
108     float ovrl = 0.f;
109 
110     // one circle is completely encovered by the other => no intersection points!
111     if( std::min( a, b ) + c <= std::max( a, b ) )
112         return std::min( a_2, b_2 ) / std::max( a_2, b_2 );
113 
114     if( c < a + b ) // circles intersect
115     {
116         float c_2 = c * c;
117         float cosAlpha = ( b_2 + c_2 - a_2 ) / ( kp2.size * c );
118         float cosBeta  = ( a_2 + c_2 - b_2 ) / ( kp1.size * c );
119         float alpha = acos( cosAlpha );
120         float beta = acos( cosBeta );
121         float sinAlpha = sin(alpha);
122         float sinBeta  = sin(beta);
123 
124         float segmentAreaA = a_2 * beta;
125         float segmentAreaB = b_2 * alpha;
126 
127         float triangleAreaA = a_2 * sinBeta * cosBeta;
128         float triangleAreaB = b_2 * sinAlpha * cosAlpha;
129 
130         float intersectionArea = segmentAreaA + segmentAreaB - triangleAreaA - triangleAreaB;
131         float unionArea = (a_2 + b_2) * (float)CV_PI - intersectionArea;
132 
133         ovrl = intersectionArea / unionArea;
134     }
135 
136     return ovrl;
137 }
138 
139 } // cv
140