When a cluster is empty for KMeans, it's better to give it the point from another...
authorPierre-Emmanuel Viel <p.emmanuel.viel@gmail.com>
Fri, 20 Dec 2013 00:00:55 +0000 (01:00 +0100)
committerPierre-Emmanuel Viel <p.emmanuel.viel@gmail.com>
Fri, 20 Dec 2013 00:00:55 +0000 (01:00 +0100)
modules/flann/include/opencv2/flann/kmeans_index.h

index 3fea956..489ed80 100644 (file)
@@ -759,10 +759,13 @@ private:
 
                     for (int k=0; k<indices_length; ++k) {
                         if (belongs_to[k]==j) {
-                            belongs_to[k] = i;
-                            count[j]--;
-                            count[i]++;
-                            break;
+                            // for cluster j, we move the furthest element from the center to the empty cluster i
+                            if ( distance_(dataset_[indices[k]], dcenters[j], veclen_) == radiuses[j] ) {
+                                belongs_to[k] = i;
+                                count[j]--;
+                                count[i]++;
+                                break;
+                            }
                         }
                     }
                     converged = false;