The goal of this article is to experience a clustering image division algorithm. Let's target the camera man. (Everything runs on ipython --pylab.)
from skimage import data,io
image=data.camera()
io.imshow(image)
The clustering target is each pixel. When the number of data is n and the number of dimensions is d, an n × d matrix must be created to use the clustering algorithm. Since the target is each pixel this time, first create a 262144 × 1 matrix with n = 512 × 512 = 262144 and d = 1.
X=image.reshape((-1,1))
Now you are ready. Let's cluster. Use Mean-Shift clustering. Mean-Shift clustering is considered to be suitable for image division because it is not necessary to determine the number of clusters in advance. This is already implemented in scikit-learn. How convenient ... There are some parameters, but I'll stick to the references.
from sklearn.cluster import MeanShift,estimate_bandwidth
bandwidth=estimate_bandwidth(X,quantile=0.2,n_samples=500)
ms=MeanShift(bandwidth=bandwidth,bin_seeding=True)
ms.fit(X)
This completes clustering. Let's see the result.
segmented_image=ms.labels_
segmented_image.shape=image.shape
imshow(segmented_image)
#matshow(segmented_image)
Let's also look at the number of clusters.
print len(unique(ms.labels_))
3
The camera man has been split into three parts. You need the correct data to evaluate whether this is good or bad, but is it somewhere? The performance of Mean-Shift clustering depends largely on bandwidth. This time, sklearn had a function to estimate the bandwidth, so I tried using it, but I can't say anything because I haven't seen what I'm doing inside. For the time being, I am glad that I was able to experience image division using the scikit series.
Thank you very much.
Recommended Posts