/*F/////////////////////////////////////////////////////////////////////////////////////// // Name: icvContourFromContourTree // Purpose: // reconstracts contour from binary tree representation // Context: // Parameters: // tree - pointer to the input binary tree representation // storage - pointer to the current storage block // contour - pointer to output contour object. // criteria - criteria for the definition threshold value // for the contour reconstracting (level or precision) //F*/ CV_IMPL CvSeq* cvContourFromContourTree( const CvContourTree* tree, CvMemStorage* storage, CvTermCriteria criteria ) { CvSeq* contour = 0; _CvTrianAttr **ptr_buf = 0; /* pointer to the pointer's buffer */ int *level_buf = 0; int i_buf; int lpt; double area_all; double threshold; int cur_level; int level; int seq_flags; char log_iter, log_eps; int out_hearder_size; _CvTrianAttr *tree_one = 0, tree_root; /* current vertex */ CvSeqReader reader; CvSeqWriter writer; CV_FUNCNAME("cvContourFromContourTree"); __BEGIN__; if( !tree ) CV_ERROR( CV_StsNullPtr, "" ); if( !CV_IS_SEQ_POLYGON_TREE( tree )) CV_ERROR_FROM_STATUS( CV_BADFLAG_ERR ); criteria = cvCheckTermCriteria( criteria, 0., 100 ); lpt = tree->total; ptr_buf = NULL; level_buf = NULL; i_buf = 0; cur_level = 0; log_iter = (char) (criteria.type == CV_TERMCRIT_ITER || (criteria.type == CV_TERMCRIT_ITER + CV_TERMCRIT_EPS)); log_eps = (char) (criteria.type == CV_TERMCRIT_EPS || (criteria.type == CV_TERMCRIT_ITER + CV_TERMCRIT_EPS)); cvStartReadSeq( (CvSeq *) tree, &reader, 0 ); out_hearder_size = sizeof( CvContour ); seq_flags = CV_SEQ_POLYGON; cvStartWriteSeq( seq_flags, out_hearder_size, sizeof( CvPoint ), storage, &writer ); ptr_buf = (_CvTrianAttr **) cvAlloc( lpt * sizeof( _CvTrianAttr * )); if( ptr_buf == NULL ) CV_ERROR_FROM_STATUS( CV_OUTOFMEM_ERR ); if( log_iter ) { level_buf = (int *) cvAlloc( lpt * (sizeof( int ))); if( level_buf == NULL ) CV_ERROR_FROM_STATUS( CV_OUTOFMEM_ERR ); } memset( ptr_buf, 0, lpt * sizeof( _CvTrianAttr * )); /* write the first tree root's point as a start point of the result contour */ CV_WRITE_SEQ_ELEM( tree->p1, writer ); /* write the second tree root"s point into buffer */ /* read the root of the tree */ CV_READ_SEQ_ELEM( tree_root, reader ); tree_one = &tree_root; area_all = tree_one->area; if( log_eps ) threshold = criteria.epsilon * area_all; else threshold = 10 * area_all; if( log_iter ) level = criteria.max_iter; else level = -1; /* contour from binary tree constraction */ while( i_buf >= 0 ) { if( tree_one != NULL && (cur_level <= level || tree_one->area >= threshold) ) /* go to left sub tree for the vertex and save pointer to the right vertex */ /* into the buffer */ { ptr_buf[i_buf] = tree_one; if( log_iter ) { level_buf[i_buf] = cur_level; cur_level++; } i_buf++; tree_one = tree_one->next_v1; } else { i_buf--; if( i_buf >= 0 ) { CvPoint pt = ptr_buf[i_buf]->pt; CV_WRITE_SEQ_ELEM( pt, writer ); tree_one = ptr_buf[i_buf]->next_v2; if( log_iter ) { cur_level = level_buf[i_buf] + 1; } } } } contour = cvEndWriteSeq( &writer ); cvBoundingRect( contour, 1 ); __CLEANUP__; __END__; cvFree( &level_buf ); cvFree( &ptr_buf ); return contour; }
/*F/////////////////////////////////////////////////////////////////////////////////////// // Name: icvMatchContourTrees // Purpose: // Calculates matching of the two contour trees // Context: // Parameters: // tree1 - pointer to the first input contour tree object. // tree2 - pointer to the second input contour tree object. // method - method for the matching calculation // (now CV_CONTOUR_TREES_MATCH_I1 only ) // threshold - threshold for the contour trees matching // result - output calculated measure //F*/ CV_IMPL double cvMatchContourTrees( const CvContourTree* tree1, const CvContourTree* tree2, int method, double threshold ) { cv::AutoBuffer<_CvTrianAttr*> buf; _CvTrianAttr **ptr_p1 = 0, **ptr_p2 = 0; /*pointers to the pointer's buffer */ _CvTrianAttr **ptr_n1 = 0, **ptr_n2 = 0; /*pointers to the pointer's buffer */ _CvTrianAttr **ptr11, **ptr12, **ptr21, **ptr22; int lpt1, lpt2, lpt, flag, flag_n, i, j, ibuf, ibuf1; double match_v, d12, area1, area2, r11, r12, r21, r22, w1, w2; double eps = 1.e-5; char s1, s2; _CvTrianAttr tree_1, tree_2; /*current vertex 1 and 2 tree */ CvSeqReader reader1, reader2; if( !tree1 || !tree2 ) CV_Error( CV_StsNullPtr, "" ); if( method != CV_CONTOUR_TREES_MATCH_I1 ) CV_Error( CV_StsBadArg, "Unknown/unsupported comparison method" ); if( !CV_IS_SEQ_POLYGON_TREE( tree1 )) CV_Error( CV_StsBadArg, "The first argument is not a valid contour tree" ); if( !CV_IS_SEQ_POLYGON_TREE( tree2 )) CV_Error( CV_StsBadArg, "The second argument is not a valid contour tree" ); lpt1 = tree1->total; lpt2 = tree2->total; lpt = lpt1 > lpt2 ? lpt1 : lpt2; ptr_p1 = ptr_n1 = ptr_p2 = ptr_n2 = NULL; buf.allocate(lpt*4); ptr_p1 = buf; ptr_p2 = ptr_p1 + lpt; ptr_n1 = ptr_p2 + lpt; ptr_n2 = ptr_n1 + lpt; cvStartReadSeq( (CvSeq *) tree1, &reader1, 0 ); cvStartReadSeq( (CvSeq *) tree2, &reader2, 0 ); /*read the root of the first and second tree*/ CV_READ_SEQ_ELEM( tree_1, reader1 ); CV_READ_SEQ_ELEM( tree_2, reader2 ); /*write to buffer pointers to root's childs vertexs*/ ptr_p1[0] = tree_1.next_v1; ptr_p1[1] = tree_1.next_v2; ptr_p2[0] = tree_2.next_v1; ptr_p2[1] = tree_2.next_v2; i = 2; match_v = 0.; area1 = tree_1.area; area2 = tree_2.area; if( area1 < eps || area2 < eps || lpt < 4 ) CV_Error( CV_StsBadSize, "" ); r11 = r12 = r21 = r22 = w1 = w2 = d12 = 0; flag = 0; s1 = s2 = 0; do { if( flag == 0 ) { ptr11 = ptr_p1; ptr12 = ptr_n1; ptr21 = ptr_p2; ptr22 = ptr_n2; flag = 1; } else { ptr11 = ptr_n1; ptr12 = ptr_p1; ptr21 = ptr_n2; ptr22 = ptr_p2; flag = 0; } ibuf = 0; for( j = 0; j < i; j++ ) { flag_n = 0; if( ptr11[j] != NULL ) { r11 = ptr11[j]->r1; r12 = ptr11[j]->r2; flag_n = 1; w1 = ptr11[j]->area / area1; s1 = ptr11[j]->sign; } else { r11 = r21 = 0; } if( ptr21[j] != NULL ) { r21 = ptr21[j]->r1; r22 = ptr21[j]->r2; flag_n = 1; w2 = ptr21[j]->area / area2; s2 = ptr21[j]->sign; } else { r21 = r22 = 0; } if( flag_n != 0 ) /* calculate node distance */ { switch (method) { case 1: { double t0, t1; if( s1 != s2 ) { t0 = fabs( r11 * w1 + r21 * w2 ); t1 = fabs( r12 * w1 + r22 * w2 ); } else { t0 = fabs( r11 * w1 - r21 * w2 ); t1 = fabs( r12 * w1 - r22 * w2 ); } d12 = t0 + t1; break; } } match_v += d12; ibuf1 = ibuf + 1; /*write to buffer the pointer to child vertexes*/ if( ptr11[j] != NULL ) { ptr12[ibuf] = ptr11[j]->next_v1; ptr12[ibuf1] = ptr11[j]->next_v2; } else { ptr12[ibuf] = NULL; ptr12[ibuf1] = NULL; } if( ptr21[j] != NULL ) { ptr22[ibuf] = ptr21[j]->next_v1; ptr22[ibuf1] = ptr21[j]->next_v2; } else { ptr22[ibuf] = NULL; ptr22[ibuf1] = NULL; } ibuf += 2; } } i = ibuf; } while( i > 0 && match_v < threshold ); return match_v; }