Add OpenCV source code
[platform/upstream/opencv.git] / modules / imgproc / src / matchcontours.cpp
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 /*F///////////////////////////////////////////////////////////////////////////////////////
44 //    Name: cvMatchContours
45 //    Purpose:
46 //      Calculates matching of the two contours
47 //    Context:
48 //    Parameters:
49 //      contour_1 - pointer to the first input contour object.
50 //      contour_2 - pointer to the second input contour object.
51 //      method - method for the matching calculation
52 //      (now CV_IPPI_CONTOURS_MATCH_I1, CV_CONTOURS_MATCH_I2 or
53 //      CV_CONTOURS_MATCH_I3 only  )
54 //      rezult - output calculated measure
55 //
56 //F*/
57 CV_IMPL  double
58 cvMatchShapes( const void* contour1, const void* contour2,
59                int method, double /*parameter*/ )
60 {
61     CvMoments moments;
62     CvHuMoments huMoments;
63     double ma[7], mb[7];
64     int i, sma, smb;
65     double eps = 1.e-5;
66     double mmm;
67     double result = 0;
68
69     if( !contour1 || !contour2 )
70         CV_Error( CV_StsNullPtr, "" );
71
72     // calculate moments of the first shape
73     cvMoments( contour1, &moments );
74     cvGetHuMoments( &moments, &huMoments );
75
76     ma[0] = huMoments.hu1;
77     ma[1] = huMoments.hu2;
78     ma[2] = huMoments.hu3;
79     ma[3] = huMoments.hu4;
80     ma[4] = huMoments.hu5;
81     ma[5] = huMoments.hu6;
82     ma[6] = huMoments.hu7;
83
84
85     // calculate moments of the second shape
86     cvMoments( contour2, &moments );
87     cvGetHuMoments( &moments, &huMoments );
88
89     mb[0] = huMoments.hu1;
90     mb[1] = huMoments.hu2;
91     mb[2] = huMoments.hu3;
92     mb[3] = huMoments.hu4;
93     mb[4] = huMoments.hu5;
94     mb[5] = huMoments.hu6;
95     mb[6] = huMoments.hu7;
96
97     switch (method)
98     {
99     case 1:
100         {
101             for( i = 0; i < 7; i++ )
102             {
103                 double ama = fabs( ma[i] );
104                 double amb = fabs( mb[i] );
105
106                 if( ma[i] > 0 )
107                     sma = 1;
108                 else if( ma[i] < 0 )
109                     sma = -1;
110                 else
111                     sma = 0;
112                 if( mb[i] > 0 )
113                     smb = 1;
114                 else if( mb[i] < 0 )
115                     smb = -1;
116                 else
117                     smb = 0;
118
119                 if( ama > eps && amb > eps )
120                 {
121                     ama = 1. / (sma * log10( ama ));
122                     amb = 1. / (smb * log10( amb ));
123                     result += fabs( -ama + amb );
124                 }
125             }
126             break;
127         }
128
129     case 2:
130         {
131             for( i = 0; i < 7; i++ )
132             {
133                 double ama = fabs( ma[i] );
134                 double amb = fabs( mb[i] );
135
136                 if( ma[i] > 0 )
137                     sma = 1;
138                 else if( ma[i] < 0 )
139                     sma = -1;
140                 else
141                     sma = 0;
142                 if( mb[i] > 0 )
143                     smb = 1;
144                 else if( mb[i] < 0 )
145                     smb = -1;
146                 else
147                     smb = 0;
148
149                 if( ama > eps && amb > eps )
150                 {
151                     ama = sma * log10( ama );
152                     amb = smb * log10( amb );
153                     result += fabs( -ama + amb );
154                 }
155             }
156             break;
157         }
158
159     case 3:
160         {
161             for( i = 0; i < 7; i++ )
162             {
163                 double ama = fabs( ma[i] );
164                 double amb = fabs( mb[i] );
165
166                 if( ma[i] > 0 )
167                     sma = 1;
168                 else if( ma[i] < 0 )
169                     sma = -1;
170                 else
171                     sma = 0;
172                 if( mb[i] > 0 )
173                     smb = 1;
174                 else if( mb[i] < 0 )
175                     smb = -1;
176                 else
177                     smb = 0;
178
179                 if( ama > eps && amb > eps )
180                 {
181                     ama = sma * log10( ama );
182                     amb = smb * log10( amb );
183                     mmm = fabs( (ama - amb) / ama );
184                     if( result < mmm )
185                         result = mmm;
186                 }
187             }
188             break;
189         }
190     default:
191         CV_Error( CV_StsBadArg, "Unknown comparison method" );
192     }
193
194     return result;
195 }
196
197
198 /* End of file. */