# Image Segmentation with K-means and Watershed Algorithm with Python

https://miro.medium.com/max/1200/0*mazjRKEbSOTNYwRp

Original Source Here

## Code for Clustering

from skimage.color import rgb2gray
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inlinefrom scipy import ndimage
china = load_sample_image(“china.jpg”)ax = plt.axes(xticks=[], yticks=[])
ax.imshow(china);

#The image is stored in a 3Darray of size i.e height, width, RGB, and containing RGB contributions as integers from 0 to 255:

china.shape#output:
(427, 640, 3)

#One way to view this set of pixels is as a cloud of points in a three-dimensional color space. We are reshaping the data to [n_samples x n_features], and rescale the colors so that they lie between 0 and 1.

data = (china )/ 255.0data = data.reshape(427 * 640, 3)data.shape#output:
(273280, 3)

#These pixels we can visualize in this color space, using a subset of 10,000 pixels for getting efficiency:

def plot_pixels(data, title, colors=None, N=10000):
if colors is None:
colors = data

# choose a random subset
rng = np.random.RandomState(0)
i = rng.permutation(data.shape[0])[:N]
colors = colors[i]
R, G, B = data[i].T
fig, ax = plt.subplots(1, 2, figsize=(16, 6)) ax[0].scatter(R, G, color=colors, marker=’.’)
ax[0].set(xlabel=’Red’,ylabel=’Green’,xlim=(0, 1),ylim=(0, 1))
ax[1].scatter(R, B, color=colors, marker=’.’)
ax[1].set(xlabel=’Red’, ylabel=’Blue’, xlim=(0, 1), ylim=(0, 1))
fig.suptitle(title, size=20);plot_pixels(data, title=’Input color space: 16 million possible colors’)

#Now, we reduce these 16 million colors to 16 colors only, using a k-means clustering across the pixel space as we are dealing with a very large dataset and use the mini-batch k means which when operates on subsets of the data give much more fast result than standard k-means.

import warnings; warnings.simplefilter(‘ignore’) # Fix NumPy issues.from sklearn.cluster import MiniBatchKMeanskmeans = MiniBatchKMeans(16)kmeans.fit(data)new_colors = kmeans.cluster_centers_[kmeans.predict(data)]plot_pixels(data, colors=new_colors,title=”Reduced color space: 16
colors”)

#Our result at this step is a re-coloring of the original pixels, where each pixel is assigned the color of its closest cluster center. Plotting these new colors in the image space shows us the effect of this:

china_recolored = new_colors.reshape(china.shape)fig, ax = plt.subplots(1, 2, figsize=(16, 6),
subplot_kw=dict(xticks=[], yticks=[]))