image clustering technique using Kmeans

 Kmeans algorithm is widely used to cluster image i.e grouping the image as per the color. 

Kmeans basically use the technique to form a cluster by making decision boundry

the code below will help you to get the proper grip on the idea.

code


import numpy as np

import cv2

import matplotlib.pyplot as plt

original_image = cv2.imread("/content/sample_data/ocen.png")

original_image


this few lines is importing essential lib. and uploading the image to see the cluster image of it.




img=cv2.cvtColor(original_image,cv2.COLOR_BGR2RGB)
#Next, converts the MxNx3 image into a Kx3 matrix where K=MxN and each row is now a vector in the 3-D space of RGB.

vectorized = img.reshape((-1,3))
#We convert the unit8 values to float as it is a requirement of the k-means method of OpenCV.

vectorized = np.float32(vectorized)

criteria is used for specific comand


criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 101.0)


K = 6
attempts=10
ret,label,center=cv2.kmeans(vectorized,K,None,criteria,attempts,cv2.KMEANS_PP_CENTERS)


center = np.uint8(center)




res = center[label.flatten()]
result_image = res.reshape((img.shape))


#result_image is the result of the frame which has undergone k-means clustering.

#Now let us visualize the output result with K=3

figure_size = 15
plt.figure(figsize=(figure_size,figure_size))
plt.subplot(1,2,1),plt.imshow(img)
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(1,2,2),plt.imshow(result_image)
plt.title('Segmented Image when K = %i' % K), plt.xticks([]), plt.yticks([])
plt.show()


- this technique is used for picture editing and also to give cartoon effect 

-used in medical dignose image







Comments

Popular posts from this blog

spealized the work. Be ready for the future

lest just create a basic bot operation in python

scatterplot/ violon plot /histogram /boxplot