Image Color Clustering

Adam Aulia Rahmadi
2 min readDec 3, 2017

--

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

--

--

Adam Aulia Rahmadi

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