From: Pierre-Emmanuel Viel
Date: Tue, 17 Dec 2013 12:34:20 +0000 (+0100)
Subject: Move templates in dist.h in order to share them between KMeansIndex and HierarchicalC...
X-Git-Tag: submit/tizen_ivi/20141117.190038~2^2~198^2~7^2~60^2
X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0d19685f9544ddf2668fa899ce74580fd9d1039f;p=profile%2Fivi%2Fopencv.git
Move templates in dist.h in order to share them between KMeansIndex and HierarchicalClusteringIndex classes.
---
diff --git a/modules/flann/include/opencv2/flann/dist.h b/modules/flann/include/opencv2/flann/dist.h
index 80ae2dc..2afceb8 100644
--- a/modules/flann/include/opencv2/flann/dist.h
+++ b/modules/flann/include/opencv2/flann/dist.h
@@ -812,6 +812,66 @@ struct ZeroIterator
};
+
+/*
+ * Depending on processed distances, some of them are already squared (e.g. L2)
+ * and some are not (e.g.Hamming). In KMeans++ for instance we want to be sure
+ * we are working on ^2 distances, thus following templates to ensure that.
+ */
+template
+struct squareDistance
+{
+ typedef typename Distance::ResultType ResultType;
+ ResultType operator()( ResultType dist ) { return dist*dist; }
+};
+
+
+template
+struct squareDistance, ElementType>
+{
+ typedef typename L2_Simple::ResultType ResultType;
+ ResultType operator()( ResultType dist ) { return dist; }
+};
+
+template
+struct squareDistance, ElementType>
+{
+ typedef typename L2::ResultType ResultType;
+ ResultType operator()( ResultType dist ) { return dist; }
+};
+
+
+template
+struct squareDistance, ElementType>
+{
+ typedef typename MinkowskiDistance::ResultType ResultType;
+ ResultType operator()( ResultType dist ) { return dist; }
+};
+
+template
+struct squareDistance, ElementType>
+{
+ typedef typename HellingerDistance::ResultType ResultType;
+ ResultType operator()( ResultType dist ) { return dist; }
+};
+
+template
+struct squareDistance, ElementType>
+{
+ typedef typename ChiSquareDistance::ResultType ResultType;
+ ResultType operator()( ResultType dist ) { return dist; }
+};
+
+
+template
+typename Distance::ResultType ensureSquareDistance( typename Distance::ResultType dist )
+{
+ typedef typename Distance::ElementType ElementType;
+
+ squareDistance dummy;
+ return dummy( dist );
+}
+
}
#endif //OPENCV_FLANN_DIST_H_
diff --git a/modules/flann/include/opencv2/flann/hierarchical_clustering_index.h b/modules/flann/include/opencv2/flann/hierarchical_clustering_index.h
index 02fc278..3ccfa55 100644
--- a/modules/flann/include/opencv2/flann/hierarchical_clustering_index.h
+++ b/modules/flann/include/opencv2/flann/hierarchical_clustering_index.h
@@ -214,7 +214,7 @@ private:
// far from previous centers (and this complies to "k-means++: the advantages of careful seeding" article)
for (int i = 0; i < n; i++) {
closestDistSq[i] = distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols);
- closestDistSq[i] *= closestDistSq[i];
+ closestDistSq[i] = ensureSquareDistance( closestDistSq[i] );
currentPot += closestDistSq[i];
}
@@ -242,7 +242,7 @@ private:
double newPot = 0;
for (int i = 0; i < n; i++) {
DistanceType dist = distance(dataset[dsindices[i]], dataset[dsindices[index]], dataset.cols);
- newPot += std::min( dist*dist, closestDistSq[i] );
+ newPot += std::min( ensureSquareDistance(dist), closestDistSq[i] );
}
// Store the best result
@@ -257,7 +257,7 @@ private:
currentPot = bestNewPot;
for (int i = 0; i < n; i++) {
DistanceType dist = distance(dataset[dsindices[i]], dataset[dsindices[bestNewIndex]], dataset.cols);
- closestDistSq[i] = std::min( dist*dist, closestDistSq[i] );
+ closestDistSq[i] = std::min( ensureSquareDistance(dist), closestDistSq[i] );
}
}
diff --git a/modules/flann/include/opencv2/flann/kmeans_index.h b/modules/flann/include/opencv2/flann/kmeans_index.h
index 460dc64..3cbee24 100644
--- a/modules/flann/include/opencv2/flann/kmeans_index.h
+++ b/modules/flann/include/opencv2/flann/kmeans_index.h
@@ -53,62 +53,6 @@
namespace cvflann
{
-template
-struct squareDistance
-{
- typedef typename Distance::ResultType ResultType;
- ResultType operator()( ResultType dist ) { return dist*dist; }
-};
-
-
-template
-struct squareDistance, ElementType>
-{
- typedef typename L2_Simple::ResultType ResultType;
- ResultType operator()( ResultType dist ) { return dist; }
-};
-
-template
-struct squareDistance, ElementType>
-{
- typedef typename L2::ResultType ResultType;
- ResultType operator()( ResultType dist ) { return dist; }
-};
-
-
-template
-struct squareDistance, ElementType>
-{
- typedef typename MinkowskiDistance::ResultType ResultType;
- ResultType operator()( ResultType dist ) { return dist; }
-};
-
-template
-struct squareDistance, ElementType>
-{
- typedef typename HellingerDistance::ResultType ResultType;
- ResultType operator()( ResultType dist ) { return dist; }
-};
-
-template
-struct squareDistance, ElementType>
-{
- typedef typename ChiSquareDistance::ResultType ResultType;
- ResultType operator()( ResultType dist ) { return dist; }
-};
-
-
-template
-typename Distance::ResultType ensureSquareDistance( typename Distance::ResultType dist )
-{
- typedef typename Distance::ElementType ElementType;
-
- squareDistance dummy;
- return dummy( dist );
-}
-
-
-
struct KMeansIndexParams : public IndexParams
{
KMeansIndexParams(int branching = 32, int iterations = 11,