@@ -642,7 +642,7 @@ struct HellingerDistance
642
642
typedef typename Accumulator<T>::Type ResultType;
643
643
644
644
/* *
645
- * Compute the histogram intersection distance
645
+ * Compute the Hellinger distance
646
646
*/
647
647
template <typename Iterator1, typename Iterator2>
648
648
ResultType operator ()(Iterator1 a, Iterator2 b, size_t size, ResultType /* worst_dist*/ = -1 ) const
@@ -675,7 +675,8 @@ struct HellingerDistance
675
675
template <typename U, typename V>
676
676
inline ResultType accum_dist (const U& a, const V& b, int ) const
677
677
{
678
- return sqrt (static_cast <ResultType>(a)) - sqrt (static_cast <ResultType>(b));
678
+ ResultType dist = sqrt (static_cast <ResultType>(a)) - sqrt (static_cast <ResultType>(b));
679
+ return dist * dist;
679
680
}
680
681
};
681
682
@@ -751,7 +752,7 @@ struct KL_Divergence
751
752
Iterator1 last = a + size;
752
753
753
754
while (a < last) {
754
- if (* a != 0 ) {
755
+ if ( * a != 0 && *b != 0 ) {
755
756
ResultType ratio = (ResultType)(*a / *b);
756
757
if (ratio>0 ) {
757
758
result += *a * log (ratio);
@@ -774,10 +775,12 @@ struct KL_Divergence
774
775
inline ResultType accum_dist (const U& a, const V& b, int ) const
775
776
{
776
777
ResultType result = ResultType ();
777
- ResultType ratio = (ResultType)(a / b);
778
- if (ratio>0 ) {
779
- result = a * log (ratio);
780
- }
778
+ if ( a != 0 && b != 0 ) {
779
+ ResultType ratio = (ResultType)(a / b);
780
+ if (ratio>0 ) {
781
+ result = a * log (ratio);
782
+ }
783
+ }
781
784
return result;
782
785
}
783
786
};
0 commit comments