Compares two histograms using one of a few methods
| 2449 | |
| 2450 | // Compares two histograms using one of a few methods |
| 2451 | CV_IMPL double |
| 2452 | cvCompareHist( const CvHistogram* hist1, |
| 2453 | const CvHistogram* hist2, |
| 2454 | int method ) |
| 2455 | { |
| 2456 | int i; |
| 2457 | int size1[CV_MAX_DIM], size2[CV_MAX_DIM], total = 1; |
| 2458 | |
| 2459 | if( !CV_IS_HIST(hist1) || !CV_IS_HIST(hist2) ) |
| 2460 | CV_Error( CV_StsBadArg, "Invalid histogram header[s]" ); |
| 2461 | |
| 2462 | if( CV_IS_SPARSE_MAT(hist1->bins) != CV_IS_SPARSE_MAT(hist2->bins)) |
| 2463 | CV_Error(CV_StsUnmatchedFormats, "One of histograms is sparse and other is not"); |
| 2464 | |
| 2465 | if( !CV_IS_SPARSE_MAT(hist1->bins) ) |
| 2466 | { |
| 2467 | cv::Mat H1((const CvMatND*)hist1->bins), H2((const CvMatND*)hist2->bins); |
| 2468 | return cv::compareHist(H1, H2, method); |
| 2469 | } |
| 2470 | |
| 2471 | int dims1 = cvGetDims( hist1->bins, size1 ); |
| 2472 | int dims2 = cvGetDims( hist2->bins, size2 ); |
| 2473 | |
| 2474 | if( dims1 != dims2 ) |
| 2475 | CV_Error( CV_StsUnmatchedSizes, |
| 2476 | "The histograms have different numbers of dimensions" ); |
| 2477 | |
| 2478 | for( i = 0; i < dims1; i++ ) |
| 2479 | { |
| 2480 | if( size1[i] != size2[i] ) |
| 2481 | CV_Error( CV_StsUnmatchedSizes, "The histograms have different sizes" ); |
| 2482 | total *= size1[i]; |
| 2483 | } |
| 2484 | |
| 2485 | double result = 0; |
| 2486 | CvSparseMat* mat1 = (CvSparseMat*)(hist1->bins); |
| 2487 | CvSparseMat* mat2 = (CvSparseMat*)(hist2->bins); |
| 2488 | CvSparseMatIterator iterator; |
| 2489 | CvSparseNode *node1, *node2; |
| 2490 | |
| 2491 | if( mat1->heap->active_count > mat2->heap->active_count && method != CV_COMP_CHISQR ) |
| 2492 | { |
| 2493 | CvSparseMat* t; |
| 2494 | CV_SWAP( mat1, mat2, t ); |
| 2495 | } |
| 2496 | |
| 2497 | if( method == CV_COMP_CHISQR ) |
| 2498 | { |
| 2499 | for( node1 = cvInitSparseMatIterator( mat1, &iterator ); |
| 2500 | node1 != 0; node1 = cvGetNextSparseNode( &iterator )) |
| 2501 | { |
| 2502 | double v1 = *(float*)CV_NODE_VAL(mat1,node1); |
| 2503 | uchar* node2_data = cvPtrND( mat2, CV_NODE_IDX(mat1,node1), 0, 0, &node1->hashval ); |
| 2504 | double v2 = node2_data ? *(float*)node2_data : 0.f; |
| 2505 | double a = v1 - v2; |
| 2506 | double b = v1; |
| 2507 | if( fabs(b) > DBL_EPSILON ) |
| 2508 | result += a*a/b; |
no test coverage detected