C++ Mean Shift算法
原理
每个样本点最终会移动到核概率密度的峰值,移动到相同峰值的样本点属于同一种颜色
关键代码
template <typename PointType> inline
typename MeanShift<PointType>::PointsVector MeanShift<PointType>::meanshift(const PointsVector &points,
double kernel_bandwidth) const{
const double EPSILON_SQR = _meanshift_epsilon*_meanshift_epsilon;
std::vector<bool> stop_moving(points.size(), false);
PointsVector shifted_points = points;
double max_shift_distance;
Point point_new;
do {
max_shift_distance = 0;
for(int i=0; i<shifted_points.size(); i++)
{
if (!stop_moving[i])
{
shift_point(shifted_points[i], points, kernel_bandwidth, point_new); // 带宽收缩后的值
double shift_distance_sqr = euclidean_distance_sqr(point_new, shifted_points[i]); // 取收缩后的峰值
if(shift_distance_sqr > max_shift_distance){
max_shift_distance = shift_distance_sqr;
}
if(shift_distance_sqr <= EPSILON_SQR) {
stop_moving[i] = true;
}
shifted_points[i] = point_new;
}
}
} while (max_shift_distance > EPSILON_SQR);
return shifted_points;
}
效果
参考
深入剖析Mean Shift聚类算法原理-腾讯云开发者社区-腾讯云
GitHub - facontidavide/Meanshift: Implementation of MeanShift algorithm (including Qt Demo)