Point cloud K-Means clustering algorithm (with c++ code)

1. K-Means algorithm

Among many clustering methods, the K-Means clustering method belongs to the "prototype-based clustering" (also called prototype clustering) method. Such methods assume that the clustering structure can be characterized by a set of prototypes. Very commonly used in real-world clustering. Normally, this type of algorithm will first initialize the prototype, and then iteratively update the prototype to solve it. Using different prototype representations and different solving methods will also produce different algorithms.

As a classic "prototype clustering" algorithm, the K-Means algorithm selects "K clustering centers" as its prototype. The iterative solution method is to use the "centroids" of two adjacent solutions (all points of the same category). This may also be the origin of the name of K-Means clustering: K cluster centers + center of mass (coordinate average).

2. K-Means algorithm steps

The process is actually relatively simple:

1. Initialize the prototype, that is, specify the K value and K cluster centers. The cluster centers can be specified manually, randomly selected, or in other ways. However, try to ensure that the distance between cluster centers is not too close. 2. Clustering. Traverse all data points, calculate the distance between each data point and the K cluster centers (I choose Euclidean distance here, you can also choose Mahalanobis distance or other distances), and assign each data point to the closest distance to the point. The category to which the cluster center belongs, up to the last data point. 3. Update the clustering center. That is to say, calculate the centroid of each category and compare the clustering center calculated this time with the last clustering center. If all the clustering centers have not changed, stop; if there is a change, the current clustering center will be changed. The next clustering center is used as the new clustering center, and the above-mentioned 2 and 3 processes are repeated until all clustering centers have no change and the process is stopped.

3. Code implementation effect

The code is as follows: main.cpp:

Guess you like

Origin blog.csdn.net/a394467238/article/details/132585298