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 | // Intel License Agreement |
11 | // For Open Source Computer Vision Library |
12 | // |
13 | // Copyright (C) 2000, Intel Corporation, all rights reserved. |
14 | // Third party copyrights are property of their respective owners. |
15 | // |
16 | // Redistribution and use in source and binary forms, with or without modification, |
17 | // are permitted provided that the following conditions are met: |
18 | // |
19 | // * Redistribution's of source code must retain the above copyright notice, |
20 | // this list of conditions and the following disclaimer. |
21 | // |
22 | // * Redistribution's in binary form must reproduce the above copyright notice, |
23 | // this list of conditions and the following disclaimer in the documentation |
24 | // and/or other materials provided with the distribution. |
25 | // |
26 | // * The name of Intel Corporation may not be used to endorse or promote products |
27 | // derived from this software without specific prior written permission. |
28 | // |
29 | // This software is provided by the copyright holders and contributors "as is" and |
30 | // any express or implied warranties, including, but not limited to, the implied |
31 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
32 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
33 | // indirect, incidental, special, exemplary, or consequential damages |
34 | // (including, but not limited to, procurement of substitute goods or services; |
35 | // loss of use, data, or profits; or business interruption) however caused |
36 | // and on any theory of liability, whether in contract, strict liability, |
37 | // or tort (including negligence or otherwise) arising in any way out of |
38 | // the use of this software, even if advised of the possibility of such damage. |
39 | // |
40 | //M*/ |
41 | #include "precomp.hpp" |
42 | |
43 | |
44 | double cv::matchShapes(InputArray contour1, InputArray contour2, int method, double) |
45 | { |
46 | CV_INSTRUMENT_REGION(); |
47 | |
48 | double ma[7], mb[7]; |
49 | int i, sma, smb; |
50 | double eps = 1.e-5; |
51 | double mmm; |
52 | double result = 0; |
53 | bool anyA = false, anyB = false; |
54 | |
55 | HuMoments( moments: moments(array: contour1), hu: ma ); |
56 | HuMoments( moments: moments(array: contour2), hu: mb ); |
57 | |
58 | switch (method) |
59 | { |
60 | case 1: |
61 | for( i = 0; i < 7; i++ ) |
62 | { |
63 | double ama = fabs( x: ma[i] ); |
64 | double amb = fabs( x: mb[i] ); |
65 | |
66 | if (ama > 0) |
67 | anyA = true; |
68 | if (amb > 0) |
69 | anyB = true; |
70 | |
71 | if( ma[i] > 0 ) |
72 | sma = 1; |
73 | else if( ma[i] < 0 ) |
74 | sma = -1; |
75 | else |
76 | sma = 0; |
77 | if( mb[i] > 0 ) |
78 | smb = 1; |
79 | else if( mb[i] < 0 ) |
80 | smb = -1; |
81 | else |
82 | smb = 0; |
83 | |
84 | if( ama > eps && amb > eps ) |
85 | { |
86 | ama = 1. / (sma * log10( x: ama )); |
87 | amb = 1. / (smb * log10( x: amb )); |
88 | result += fabs( x: -ama + amb ); |
89 | } |
90 | } |
91 | break; |
92 | |
93 | case 2: |
94 | for( i = 0; i < 7; i++ ) |
95 | { |
96 | double ama = fabs( x: ma[i] ); |
97 | double amb = fabs( x: mb[i] ); |
98 | |
99 | if (ama > 0) |
100 | anyA = true; |
101 | if (amb > 0) |
102 | anyB = true; |
103 | |
104 | if( ma[i] > 0 ) |
105 | sma = 1; |
106 | else if( ma[i] < 0 ) |
107 | sma = -1; |
108 | else |
109 | sma = 0; |
110 | if( mb[i] > 0 ) |
111 | smb = 1; |
112 | else if( mb[i] < 0 ) |
113 | smb = -1; |
114 | else |
115 | smb = 0; |
116 | |
117 | if( ama > eps && amb > eps ) |
118 | { |
119 | ama = sma * log10( x: ama ); |
120 | amb = smb * log10( x: amb ); |
121 | result += fabs( x: -ama + amb ); |
122 | } |
123 | } |
124 | break; |
125 | |
126 | case 3: |
127 | for( i = 0; i < 7; i++ ) |
128 | { |
129 | double ama = fabs( x: ma[i] ); |
130 | double amb = fabs( x: mb[i] ); |
131 | |
132 | if (ama > 0) |
133 | anyA = true; |
134 | if (amb > 0) |
135 | anyB = true; |
136 | |
137 | if( ma[i] > 0 ) |
138 | sma = 1; |
139 | else if( ma[i] < 0 ) |
140 | sma = -1; |
141 | else |
142 | sma = 0; |
143 | if( mb[i] > 0 ) |
144 | smb = 1; |
145 | else if( mb[i] < 0 ) |
146 | smb = -1; |
147 | else |
148 | smb = 0; |
149 | |
150 | if( ama > eps && amb > eps ) |
151 | { |
152 | ama = sma * log10( x: ama ); |
153 | amb = smb * log10( x: amb ); |
154 | mmm = fabs( x: (ama - amb) / ama ); |
155 | if( result < mmm ) |
156 | result = mmm; |
157 | } |
158 | } |
159 | break; |
160 | default: |
161 | CV_Error( cv::Error::StsBadArg, "Unknown comparison method" ); |
162 | } |
163 | |
164 | //If anyA and anyB are both true, the result is correct. |
165 | //If anyA and anyB are both false, the distance is 0, perfect match. |
166 | //If only one is true, then it's a false 0 and return large error. |
167 | if (anyA != anyB) |
168 | result = DBL_MAX; |
169 | |
170 | return result; |
171 | } |
172 | |
173 | |
174 | CV_IMPL double |
175 | cvMatchShapes( const void* _contour1, const void* _contour2, |
176 | int method, double parameter ) |
177 | { |
178 | cv::AutoBuffer<double> abuf1, abuf2; |
179 | cv::Mat contour1 = cv::cvarrToMat(arr: _contour1, copyData: false, allowND: false, coiMode: 0, buf: &abuf1); |
180 | cv::Mat contour2 = cv::cvarrToMat(arr: _contour2, copyData: false, allowND: false, coiMode: 0, buf: &abuf2); |
181 | |
182 | return cv::matchShapes(contour1, contour2, method, parameter); |
183 | } |
184 | |
185 | /* End of file. */ |
186 | |