# 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.io import imreadfrom skimage.color import rgb2grayimport numpy as npimport matplotlib.pyplot as plt%matplotlib inlinefrom scipy import ndimagefrom sklearn.datasets import load_sample_imagechina = 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)[:N]    colors = colors[i]    R, G, B = data[i].T    fig, ax = plt.subplots(1, 2, figsize=(16, 6))    ax.scatter(R, G, color=colors, marker=’.’)    ax.set(xlabel=’Red’,ylabel=’Green’,xlim=(0, 1),ylim=(0, 1))    ax.scatter(R, B, color=colors, marker=’.’)    ax.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=[]))fig.subplots_adjust(wspace=0.05)ax.imshow(china)ax.set_title(‘Original Image’, size=16)ax.imshow(china_recolored)ax.set_title(‘16-color Image’, size=16);`

#Little bit of detail is lost in the rightmost image, but the overall image is still easily recognizable. Right side image achieves a compression factor of around 1 million, While this is an interesting application of k-means, there are certainly better ways to compress information in images. But our example shows thinking outside of the box with unsupervised techniques like k-means.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot