Agglomeration algorithms for segmantation

Job ID: 34708770

Budget: $30 – $250 USD

This project consists of implementing 11 agglomeration algorithms and testing them to segment an image.

the first that i did was the k-means and it worked perfectly:

# K-Means 1
# source: https://www.kdnuggets.com/2019/08/introduction-image-segmentation-k-means-clustering.html

#Load all the required libraries
import numpy as np
import cv2
import matplotlib.pyplot as plt

#load the image in RGB color space
original_image = cv2.imread("/content/drive/MyDrive/UnB/Cibernetica/project3/testImages/Test image.JPG")

#convert our image from RGB Colours Space to HSV to work ahead
img=cv2.cvtColor(original_image,cv2.COLOR_BGR2RGB)

#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))

#convert the unit8 values to float as it is a requirement of the k-means method of OpenCV
vectorized = np.float32(vectorized)

#We are going to cluster with k = 3 because if you look at the image above it has 3 colors, green-colored grass and forest, blue sea and the greenish-blue seashore.
#Define criteria, number of clusters(K) and apply k-means()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

K = 4
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))

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()



#edges = cv2.Canny(img,150,200)
#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(edges,cmap = 'gray')
#plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
#plt.show()




the required techniques are:
10 techniques as described at https://machinelearningmastery.com/clustering-algorithms-withpython/
and k-medoids algorithm



I found that page in the web:
https://scikit-learn.org/stable/auto_examples/cluster/plot_cluster_comparison.html#sphx-glr-auto-examples-cluster-plot-cluster-comparison-py
that do the most of the work, but it doesn't get an image.
so i convert the image to dataset:
img = cv2.imread('/content/drive/MyDrive/UnB/Cibernetica/project3/testImages/Test image.JPG')
img = cv2.resize(img, (100,150))
image_2D = img.reshape(img.shape[0]*img.shape[1], img.shape[2])

dataset = [
(
image_2D,
{
"quantile": 0.3,
"eps": 0.3,
"damping": 0.9,
"preference": -200,
"n_neighbors": 4,
"n_clusters": 4,
"min_samples": 7,
"xi": 0.05,
"min_cluster_size": 0.1,
},
)
]

and now i need it to show the clustered image as i showed in the k-means (the atachments)
Related categories: Python Deep Learning