1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
43 /*F///////////////////////////////////////////////////////////////////////////////////////
44 // Name: cvMatchContours
46 // Calculates matching of the two contours
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
58 cvMatchShapes( const void* contour1, const void* contour2,
59 int method, double /*parameter*/ )
62 CvHuMoments huMoments;
69 CV_FUNCNAME( "cvMatchShapes" );
73 if( !contour1 || !contour2 )
74 CV_ERROR( CV_StsNullPtr, "" );
76 /* first moments calculation */
77 CV_CALL( cvMoments( contour1, &moments ));
79 /* Hu moments calculation */
80 CV_CALL( cvGetHuMoments( &moments, &huMoments ));
82 ma[0] = huMoments.hu1;
83 ma[1] = huMoments.hu2;
84 ma[2] = huMoments.hu3;
85 ma[3] = huMoments.hu4;
86 ma[4] = huMoments.hu5;
87 ma[5] = huMoments.hu6;
88 ma[6] = huMoments.hu7;
91 /* second moments calculation */
92 CV_CALL( cvMoments( contour2, &moments ));
94 /* Hu moments calculation */
95 CV_CALL( cvGetHuMoments( &moments, &huMoments ));
97 mb[0] = huMoments.hu1;
98 mb[1] = huMoments.hu2;
99 mb[2] = huMoments.hu3;
100 mb[3] = huMoments.hu4;
101 mb[4] = huMoments.hu5;
102 mb[5] = huMoments.hu6;
103 mb[6] = huMoments.hu7;
109 for( i = 0; i < 7; i++ )
111 double ama = fabs( ma[i] );
112 double amb = fabs( mb[i] );
127 if( ama > eps && amb > eps )
129 ama = 1. / (sma * log10( ama ));
130 amb = 1. / (smb * log10( amb ));
131 result += fabs( -ama + amb );
139 for( i = 0; i < 7; i++ )
141 double ama = fabs( ma[i] );
142 double amb = fabs( mb[i] );
157 if( ama > eps && amb > eps )
159 ama = sma * log10( ama );
160 amb = smb * log10( amb );
161 result += fabs( -ama + amb );
169 for( i = 0; i < 7; i++ )
171 double ama = fabs( ma[i] );
172 double amb = fabs( mb[i] );
187 if( ama > eps && amb > eps )
189 ama = sma * log10( ama );
190 amb = smb * log10( amb );
191 mmm = fabs( (ama - amb) / ama );
199 CV_ERROR_FROM_STATUS( CV_BADCOEF_ERR );
209 /*F///////////////////////////////////////////////////////////////////////////////////////
210 // Name: icvMatchContourTrees
212 // Calculates matching of the two contour trees
215 // tree1 - pointer to the first input contour tree object.
216 // tree2 - pointer to the second input contour tree object.
217 // method - method for the matching calculation
218 // (now CV_CONTOUR_TREES_MATCH_I1 only )
219 // threshold - threshold for the contour trees matching
220 // result - output calculated measure
223 cvMatchContourTrees( const CvContourTree* tree1, const CvContourTree* tree2,
224 int method, double threshold )
226 _CvTrianAttr **ptr_p1 = 0, **ptr_p2 = 0; /*pointers to the pointer's buffer */
227 _CvTrianAttr **ptr_n1 = 0, **ptr_n2 = 0; /*pointers to the pointer's buffer */
228 _CvTrianAttr **ptr11, **ptr12, **ptr21, **ptr22;
230 int lpt1, lpt2, lpt, flag, flag_n, i, j, ibuf, ibuf1;
231 double match_v, d12, area1, area2, r11, r12, r21, r22, w1, w2;
234 _CvTrianAttr tree_1, tree_2; /*current vertex 1 and 2 tree */
235 CvSeqReader reader1, reader2;
238 CV_FUNCNAME("cvMatchContourTrees");
241 if( !tree1 || !tree2 )
242 CV_ERROR( CV_StsNullPtr, "" );
244 if( method != CV_CONTOUR_TREES_MATCH_I1 )
245 CV_ERROR( CV_StsBadArg, "Unknown/unsupported comparison method" );
247 if( !CV_IS_SEQ_POLYGON_TREE( tree1 ))
248 CV_ERROR( CV_StsBadArg, "The first argument is not a valid contour tree" );
250 if( !CV_IS_SEQ_POLYGON_TREE( tree2 ))
251 CV_ERROR( CV_StsBadArg, "The second argument is not a valid contour tree" );
255 lpt = lpt1 > lpt2 ? lpt1 : lpt2;
257 ptr_p1 = ptr_n1 = ptr_p2 = ptr_n2 = NULL;
258 CV_CALL( ptr_p1 = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )));
259 CV_CALL( ptr_p2 = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )));
261 CV_CALL( ptr_n1 = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )));
262 CV_CALL( ptr_n2 = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )));
264 cvStartReadSeq( (CvSeq *) tree1, &reader1, 0 );
265 cvStartReadSeq( (CvSeq *) tree2, &reader2, 0 );
267 /*read the root of the first and second tree*/
268 CV_READ_SEQ_ELEM( tree_1, reader1 );
269 CV_READ_SEQ_ELEM( tree_2, reader2 );
271 /*write to buffer pointers to root's childs vertexs*/
272 ptr_p1[0] = tree_1.next_v1;
273 ptr_p1[1] = tree_1.next_v2;
274 ptr_p2[0] = tree_2.next_v1;
275 ptr_p2[1] = tree_2.next_v2;
281 if( area1 < eps || area2 < eps || lpt < 4 )
282 CV_ERROR( CV_StsBadSize, "" );
284 r11 = r12 = r21 = r22 = w1 = w2 = d12 = 0;
306 for( j = 0; j < i; j++ )
309 if( ptr11[j] != NULL )
314 w1 = ptr11[j]->area / area1;
321 if( ptr21[j] != NULL )
326 w2 = ptr21[j]->area / area2;
334 /* calculate node distance */
343 t0 = fabs( r11 * w1 + r21 * w2 );
344 t1 = fabs( r12 * w1 + r22 * w2 );
348 t0 = fabs( r11 * w1 - r21 * w2 );
349 t1 = fabs( r12 * w1 - r22 * w2 );
357 /*write to buffer the pointer to child vertexes*/
358 if( ptr11[j] != NULL )
360 ptr12[ibuf] = ptr11[j]->next_v1;
361 ptr12[ibuf1] = ptr11[j]->next_v2;
368 if( ptr21[j] != NULL )
370 ptr22[ibuf] = ptr21[j]->next_v1;
371 ptr22[ibuf1] = ptr21[j]->next_v2;
383 while( i > 0 && match_v < threshold );