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
44double cv::matchShapes(InputArray contour1, InputArray contour2, int method, double)
45{
46    double ma[7], mb[7];
47    int i, sma, smb;
48    double eps = 1.e-5;
49    double mmm;
50    double result = 0;
51
52    HuMoments( moments(contour1), ma );
53    HuMoments( moments(contour2), mb );
54
55    switch (method)
56    {
57    case 1:
58        for( i = 0; i < 7; i++ )
59        {
60            double ama = fabs( ma[i] );
61            double amb = fabs( mb[i] );
62
63            if( ma[i] > 0 )
64                sma = 1;
65            else if( ma[i] < 0 )
66                sma = -1;
67            else
68                sma = 0;
69            if( mb[i] > 0 )
70                smb = 1;
71            else if( mb[i] < 0 )
72                smb = -1;
73            else
74                smb = 0;
75
76            if( ama > eps && amb > eps )
77            {
78                ama = 1. / (sma * log10( ama ));
79                amb = 1. / (smb * log10( amb ));
80                result += fabs( -ama + amb );
81            }
82        }
83        break;
84
85    case 2:
86        for( i = 0; i < 7; i++ )
87        {
88            double ama = fabs( ma[i] );
89            double amb = fabs( mb[i] );
90
91            if( ma[i] > 0 )
92                sma = 1;
93            else if( ma[i] < 0 )
94                sma = -1;
95            else
96                sma = 0;
97            if( mb[i] > 0 )
98                smb = 1;
99            else if( mb[i] < 0 )
100                smb = -1;
101            else
102                smb = 0;
103
104            if( ama > eps && amb > eps )
105            {
106                ama = sma * log10( ama );
107                amb = smb * log10( amb );
108                result += fabs( -ama + amb );
109            }
110        }
111        break;
112
113    case 3:
114        for( i = 0; i < 7; i++ )
115        {
116            double ama = fabs( ma[i] );
117            double amb = fabs( mb[i] );
118
119            if( ma[i] > 0 )
120                sma = 1;
121            else if( ma[i] < 0 )
122                sma = -1;
123            else
124                sma = 0;
125            if( mb[i] > 0 )
126                smb = 1;
127            else if( mb[i] < 0 )
128                smb = -1;
129            else
130                smb = 0;
131
132            if( ama > eps && amb > eps )
133            {
134                ama = sma * log10( ama );
135                amb = smb * log10( amb );
136                mmm = fabs( (ama - amb) / ama );
137                if( result < mmm )
138                    result = mmm;
139            }
140        }
141        break;
142    default:
143        CV_Error( CV_StsBadArg, "Unknown comparison method" );
144    }
145
146    return result;
147}
148
149
150CV_IMPL  double
151cvMatchShapes( const void* _contour1, const void* _contour2,
152               int method, double parameter )
153{
154    cv::AutoBuffer<double> abuf1, abuf2;
155    cv::Mat contour1 = cv::cvarrToMat(_contour1, false, false, 0, &abuf1);
156    cv::Mat contour2 = cv::cvarrToMat(_contour2, false, false, 0, &abuf2);
157
158    return cv::matchShapes(contour1, contour2, method, parameter);
159}
160
161/* End of file. */
162