16acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*M///////////////////////////////////////////////////////////////////////////////////////
26acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
36acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
46acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
56acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  By downloading, copying, installing or using the software you agree to this license.
66acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  If you do not agree to this license, do not download, install,
76acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  copy or use the software.
86acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
96acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                        Intel License Agreement
116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                For Open Source Computer Vision Library
126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Copyright (C) 2000, Intel Corporation, all rights reserved.
146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Third party copyrights are property of their respective owners.
156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Redistribution and use in source and binary forms, with or without modification,
176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// are permitted provided that the following conditions are met:
186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's of source code must retain the above copyright notice,
206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer.
216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's in binary form must reproduce the above copyright notice,
236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer in the documentation
246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     and/or other materials provided with the distribution.
256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * The name of Intel Corporation may not be used to endorse or promote products
276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     derived from this software without specific prior written permission.
286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// This software is provided by the copyright holders and contributors "as is" and
306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// any express or implied warranties, including, but not limited to, the implied
316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// warranties of merchantability and fitness for a particular purpose are disclaimed.
326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// In no event shall the Intel Corporation or contributors be liable for any direct,
336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// indirect, incidental, special, exemplary, or consequential damages
346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// (including, but not limited to, procurement of substitute goods or services;
356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// loss of use, data, or profits; or business interruption) however caused
366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// and on any theory of liability, whether in contract, strict liability,
376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// or tort (including negligence or otherwise) arising in any way out of
386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// the use of this software, even if advised of the possibility of such damage.
396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//M*/
416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "_cv.h"
426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*F///////////////////////////////////////////////////////////////////////////////////////
446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Name: cvMatchContours
456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Purpose:
466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      Calculates matching of the two contours
476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Context:
486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Parameters:
496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      contour_1 - pointer to the first input contour object.
506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      contour_2 - pointer to the second input contour object.
516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      method - method for the matching calculation
526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      (now CV_IPPI_CONTOURS_MATCH_I1, CV_CONTOURS_MATCH_I2 or
536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      CV_CONTOURS_MATCH_I3 only  )
546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      rezult - output calculated measure
556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//F*/
576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL  double
586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvMatchShapes( const void* contour1, const void* contour2,
596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn               int method, double /*parameter*/ )
606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMoments moments;
626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvHuMoments huMoments;
636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double ma[7], mb[7];
646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int i, sma, smb;
656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double eps = 1.e-5;
666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double mmm;
676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double result = 0;
686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_FUNCNAME( "cvMatchShapes" );
706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __BEGIN__;
726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !contour1 || !contour2 )
746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsNullPtr, "" );
756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*   first moments calculation */
776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( cvMoments( contour1, &moments ));
786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*  Hu moments calculation   */
806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( cvGetHuMoments( &moments, &huMoments ));
816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ma[0] = huMoments.hu1;
836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ma[1] = huMoments.hu2;
846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ma[2] = huMoments.hu3;
856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ma[3] = huMoments.hu4;
866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ma[4] = huMoments.hu5;
876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ma[5] = huMoments.hu6;
886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ma[6] = huMoments.hu7;
896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*   second moments calculation  */
926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( cvMoments( contour2, &moments ));
936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*  Hu moments calculation   */
956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( cvGetHuMoments( &moments, &huMoments ));
966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mb[0] = huMoments.hu1;
986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mb[1] = huMoments.hu2;
996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mb[2] = huMoments.hu3;
1006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mb[3] = huMoments.hu4;
1016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mb[4] = huMoments.hu5;
1026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mb[5] = huMoments.hu6;
1036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    mb[6] = huMoments.hu7;
1046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    switch (method)
1066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case 1:
1086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for( i = 0; i < 7; i++ )
1106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                double ama = fabs( ma[i] );
1126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                double amb = fabs( mb[i] );
1136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( ma[i] > 0 )
1156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = 1;
1166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else if( ma[i] < 0 )
1176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = -1;
1186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
1196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = 0;
1206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( mb[i] > 0 )
1216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = 1;
1226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else if( mb[i] < 0 )
1236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = -1;
1246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
1256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = 0;
1266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( ama > eps && amb > eps )
1286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
1296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ama = 1. / (sma * log10( ama ));
1306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    amb = 1. / (smb * log10( amb ));
1316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    result += fabs( -ama + amb );
1326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
1336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
1356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case 2:
1386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for( i = 0; i < 7; i++ )
1406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                double ama = fabs( ma[i] );
1426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                double amb = fabs( mb[i] );
1436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( ma[i] > 0 )
1456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = 1;
1466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else if( ma[i] < 0 )
1476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = -1;
1486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
1496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = 0;
1506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( mb[i] > 0 )
1516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = 1;
1526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else if( mb[i] < 0 )
1536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = -1;
1546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
1556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = 0;
1566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( ama > eps && amb > eps )
1586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
1596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ama = sma * log10( ama );
1606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    amb = smb * log10( amb );
1616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    result += fabs( -ama + amb );
1626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
1636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
1656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    case 3:
1686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            for( i = 0; i < 7; i++ )
1706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
1716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                double ama = fabs( ma[i] );
1726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                double amb = fabs( mb[i] );
1736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( ma[i] > 0 )
1756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = 1;
1766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else if( ma[i] < 0 )
1776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = -1;
1786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
1796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    sma = 0;
1806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( mb[i] > 0 )
1816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = 1;
1826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else if( mb[i] < 0 )
1836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = -1;
1846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
1856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    smb = 0;
1866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( ama > eps && amb > eps )
1886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
1896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ama = sma * log10( ama );
1906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    amb = smb * log10( amb );
1916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    mmm = fabs( (ama - amb) / ama );
1926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    if( result < mmm )
1936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        result = mmm;
1946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
1956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
1966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
1976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    default:
1996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR_FROM_STATUS( CV_BADCOEF_ERR );
2006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __END__;
2036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return result;
2056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*F///////////////////////////////////////////////////////////////////////////////////////
2106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Name: icvMatchContourTrees
2116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Purpose:
2126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      Calculates matching of the two contour trees
2136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Context:
2146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//    Parameters:
2156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      tree1 - pointer to the first input contour tree object.
2166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      tree2 - pointer to the second input contour tree object.
2176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      method - method for the matching calculation
2186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      (now CV_CONTOUR_TREES_MATCH_I1 only  )
2196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      threshold - threshold for the contour trees matching
2206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//      result - output calculated measure
2216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//F*/
2226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL  double
2236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvMatchContourTrees( const CvContourTree* tree1, const CvContourTree* tree2,
2246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                     int method, double threshold )
2256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
2266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    _CvTrianAttr **ptr_p1 = 0, **ptr_p2 = 0;    /*pointers to the pointer's buffer */
2276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    _CvTrianAttr **ptr_n1 = 0, **ptr_n2 = 0;    /*pointers to the pointer's buffer */
2286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    _CvTrianAttr **ptr11, **ptr12, **ptr21, **ptr22;
2296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int lpt1, lpt2, lpt, flag, flag_n, i, j, ibuf, ibuf1;
2316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double match_v, d12, area1, area2, r11, r12, r21, r22, w1, w2;
2326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double eps = 1.e-5;
2336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    char s1, s2;
2346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    _CvTrianAttr tree_1, tree_2;        /*current vertex 1 and 2 tree */
2356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvSeqReader reader1, reader2;
2366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    double result = 0;
2376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_FUNCNAME("cvMatchContourTrees");
2396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __BEGIN__;
2406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !tree1 || !tree2 )
2426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsNullPtr, "" );
2436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( method != CV_CONTOUR_TREES_MATCH_I1 )
2456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsBadArg, "Unknown/unsupported comparison method" );
2466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !CV_IS_SEQ_POLYGON_TREE( tree1 ))
2486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsBadArg, "The first argument is not a valid contour tree" );
2496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !CV_IS_SEQ_POLYGON_TREE( tree2 ))
2516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsBadArg, "The second argument is not a valid contour tree" );
2526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    lpt1 = tree1->total;
2546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    lpt2 = tree2->total;
2556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    lpt = lpt1 > lpt2 ? lpt1 : lpt2;
2566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ptr_p1 = ptr_n1 = ptr_p2 = ptr_n2 = NULL;
2586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( ptr_p1 = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )));
2596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( ptr_p2 = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )));
2606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( ptr_n1 = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )));
2626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( ptr_n2 = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )));
2636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvStartReadSeq( (CvSeq *) tree1, &reader1, 0 );
2656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvStartReadSeq( (CvSeq *) tree2, &reader2, 0 );
2666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*read the root of the first and second tree*/
2686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_READ_SEQ_ELEM( tree_1, reader1 );
2696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_READ_SEQ_ELEM( tree_2, reader2 );
2706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*write to buffer pointers to root's childs vertexs*/
2726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ptr_p1[0] = tree_1.next_v1;
2736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ptr_p1[1] = tree_1.next_v2;
2746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ptr_p2[0] = tree_2.next_v1;
2756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ptr_p2[1] = tree_2.next_v2;
2766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    i = 2;
2776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    match_v = 0.;
2786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    area1 = tree_1.area;
2796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    area2 = tree_2.area;
2806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( area1 < eps || area2 < eps || lpt < 4 )
2826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsBadSize, "" );
2836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    r11 = r12 = r21 = r22 = w1 = w2 = d12 = 0;
2856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    flag = 0;
2866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    s1 = s2 = 0;
2876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    do
2886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( flag == 0 )
2906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            ptr11 = ptr_p1;
2926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            ptr12 = ptr_n1;
2936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            ptr21 = ptr_p2;
2946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            ptr22 = ptr_n2;
2956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            flag = 1;
2966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
2986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            ptr11 = ptr_n1;
3006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            ptr12 = ptr_p1;
3016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            ptr21 = ptr_n2;
3026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            ptr22 = ptr_p2;
3036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            flag = 0;
3046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
3056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        ibuf = 0;
3066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        for( j = 0; j < i; j++ )
3076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
3086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            flag_n = 0;
3096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( ptr11[j] != NULL )
3106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
3116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                r11 = ptr11[j]->r1;
3126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                r12 = ptr11[j]->r2;
3136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                flag_n = 1;
3146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                w1 = ptr11[j]->area / area1;
3156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                s1 = ptr11[j]->sign;
3166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
3176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            else
3186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
3196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                r11 = r21 = 0;
3206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
3216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( ptr21[j] != NULL )
3226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
3236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                r21 = ptr21[j]->r1;
3246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                r22 = ptr21[j]->r2;
3256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                flag_n = 1;
3266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                w2 = ptr21[j]->area / area2;
3276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                s2 = ptr21[j]->sign;
3286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
3296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            else
3306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
3316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                r21 = r22 = 0;
3326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
3336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( flag_n != 0 )
3346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/* calculate node distance */
3356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            {
3366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                switch (method)
3376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
3386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                case 1:
3396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    {
3406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        double t0, t1;
3416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        if( s1 != s2 )
3426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        {
3436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                            t0 = fabs( r11 * w1 + r21 * w2 );
3446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                            t1 = fabs( r12 * w1 + r22 * w2 );
3456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        }
3466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        else
3476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        {
3486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                            t0 = fabs( r11 * w1 - r21 * w2 );
3496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                            t1 = fabs( r12 * w1 - r22 * w2 );
3506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        }
3516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        d12 = t0 + t1;
3526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        break;
3536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    }
3546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
3556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                match_v += d12;
3566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                ibuf1 = ibuf + 1;
3576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*write to buffer the pointer to child vertexes*/
3586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( ptr11[j] != NULL )
3596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
3606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ptr12[ibuf] = ptr11[j]->next_v1;
3616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ptr12[ibuf1] = ptr11[j]->next_v2;
3626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
3636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
3646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
3656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ptr12[ibuf] = NULL;
3666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ptr12[ibuf1] = NULL;
3676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
3686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                if( ptr21[j] != NULL )
3696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
3706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ptr22[ibuf] = ptr21[j]->next_v1;
3716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ptr22[ibuf1] = ptr21[j]->next_v2;
3726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
3736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                else
3746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                {
3756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ptr22[ibuf] = NULL;
3766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                    ptr22[ibuf1] = NULL;
3776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                }
3786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                ibuf += 2;
3796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            }
3806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
3816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        i = ibuf;
3826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    while( i > 0 && match_v < threshold );
3846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    result = match_v;
3866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __END__;
3886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree( &ptr_n2 );
3906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree( &ptr_n1 );
3916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree( &ptr_p2 );
3926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvFree( &ptr_p1 );
3936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return result;
3956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/* End of file. */
399