Mean Shift 3
Mean Shift 3
Dependencies
Description
The mean_shift.py module defines a class called MeanShift. The MeanShift class constructor takes in an
optional kernel parameter. If no kernel is specified, a default Gaussian kernel is used.
The cluster method requires an array of points and a kernel bandwidth value. A optional
iteration_callback function can also be passed in that will be called back at the end of each mean shift
iteration with the current state of the algorithm (e.g., where the points are currently at, along with an
iteration number).
After the clustering finishes, a MeanShiftResult object is returned, containing three arrays:
Usage
import mean_shift as ms
data = get_data_from_somewhere()
mean_shifter = ms.MeanShift()
original_points = mean_shift_result.original_points
shifted_points = mean_shift_result.shifted_points
cluster_assignments = mean_shift_result.cluster_ids
mean_shifter = ms.MeanShift(kernel='multivariate_gaussian')
Example
import mean_shift as ms
import numpy as np
original_points = mean_shift_result.original_points
shifted_points = mean_shift_result.shifted_points
cluster_assignments = mean_shift_result.cluster_ids
x = original_points[:,0]
y = original_points[:,1]
Cluster = cluster_assignments
centers = shifted_points
fig = plt.figure()
ax = fig.add_subplot(111)
scatter = ax.scatter(x,y,c=Cluster,s=50)
ax.scatter(i,j,s=50,c='red',marker='+')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.colorbar(scatter)
fig.savefig("mean_shift_result")
Image Segmentation
Mean shift can be used for image segmentation. Below is an example of an image being mean shift
clustered in 3D RGB space, resulting in 7 clusters.