TP (5

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Université Kasdi Merbah-Ouargla

Faculté des Nouvelles Technologies de l'Information et de la


Communication

Département d'Electronique et de la Télécommunication

TP5

Nom et Prénom :

• Benesseddik Abdessaleme

G02
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from skimage import io, color
image_path = 'C:/Users/ Benesseddik Abdessaleme -Trd/.spyder-py3/Capture photo.PNG'
img = io.imread(image_path)
gray_img = color.rgb2gray(img)
flat_img = gray_img.flatten().reshape(-1, 1)
# Choose the number of clusters (K)
k=2
# Apply K-means algorithm
kmeans = KMeans(n_clusters=k)
kmeans.fit(flat_img)
# Get labels and cluster centers
labels = kmeans.labels_
centers = kmeans.cluster_centers_
segmented_img = labels.reshape(gray_img.shape)
# Display the original and segmented images
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(gray_img, cmap='gray')
plt.title('Original Image')
plt.subplot(1, 2, 2)
plt.imshow(segmented_img, cmap='viridis')
plt.title('Segmented Image (K={})'.format(k))
plt.show()

You might also like