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 imread
from skimage.color import rgb2gray
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inlinefrom scipy import ndimage
from sklearn.datasets import load_sample_image
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)
Image by Author

#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’)
Image by Author

#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”)
Image by Author

#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[0].imshow(china)
ax[0].set_title(‘Original Image’, size=16)
ax[1].imshow(china_recolored)
ax[1].set_title(‘16-color Image’, size=16);
Image by Author

#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

%d bloggers like this: