Image Color Clustering

requirement

  1. opencv
  2. matplotlib
  3. sklearn

short story

the purpose behind this experiment is to cluster image to n (cluster/area/segmentation)

code

import library

import cv2
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

load data

#import  data
im = cv2.imread('rainbow.jpg')

reshape data

#reshape data
reshape_data = im.reshape((im.shape[0]*im.shape[1],im.shape[2]))

As we know, original data from image is matrix. For example I have 3D matrix with shape (525,525,3), which means matrix has height 525, width 525, and dimension 3. Next transform into shape 2D matrix with dimension height= 525*525 and width = 3 as RGB and it looks like this.

import k means

#import kmeans
kmeans = KMeans(n_clusters=5, random_state=0)

fit k means

#data transform
data_transfrom = kmeans.predict(reshape_data).reshape((im.shape[0],im.shape[1]))

show it

#imshow 
plt.imshow(data_transfrom)

Result

6 cluster

2 cluster

3 cluster

4 cluster

5 cluster

--

--

data enthusiast, data scientist, data engineer, machine learning, deep learning, analytics, chef

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Aulia Rahmadi

Adam Aulia Rahmadi

data enthusiast, data scientist, data engineer, machine learning, deep learning, analytics, chef