-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.py
112 lines (87 loc) · 2.72 KB
/
algo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import cv2
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans
# Load the image
image = cv2.imread('peacock.jpeg')
# Convert BGR to RGB for proper visualization
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the original image
plt.figure(figsize=(8, 6))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(rgb)
plt.axis('off')
# Reshape and prepare data for clustering
p_val = rgb.reshape((-1, 3))
p_val = np.float32(p_val)
# Get the number of clusters from user input
k = int(input("Enter the number of clusters (k): "))
# Perform K-means clustering
kmeans = KMeans(n_clusters=k, random_state=0)
kmeans.fit(p_val)
# Get the labels and cluster centers
labels = kmeans.predict(p_val)
centers = kmeans.cluster_centers_
centers = np.uint8(centers)
# Reshape the image based on the labels
img = centers[labels].reshape(rgb.shape)
# Display the segmented image
plt.subplot(1, 2, 2)
plt.title(f'Segmented Image (k={k})')
plt.imshow(img)
plt.axis('off')
# Show the figure with the original and segmented images
plt.tight_layout()
plt.show()
import cv2
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans
# Load the image
image = cv2.imread('peacock.jpeg')
# Convert BGR to RGB for proper visualization
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the original image
plt.figure(figsize=(10, 6))
plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(rgb)
plt.axis('off')
# Reshape and prepare data for clustering
p_val = rgb.reshape((-1, 3))
p_val = np.float32(p_val)
# Get the number of clusters from user input
k = int(input("Enter the number of clusters (k): "))
# Perform K-means clustering
kmeans = KMeans(n_clusters=k, random_state=0)
kmeans.fit(p_val)
# Get the labels and cluster centers
labels = kmeans.predict(p_val)
centers = kmeans.cluster_centers_
centers = np.uint8(centers)
# Calculate the percentage of segmentation achieved for each cluster
segmentation_percentages = []
total_pixels = p_val.shape[0]
for cluster in range(k):
cluster_pixels = np.sum(labels == cluster)
percentage = (cluster_pixels / total_pixels) * 100
segmentation_percentages.append(percentage)
# Reshape the image based on the labels
img = centers[labels].reshape(rgb.shape)
# Display the segmented image
plt.subplot(1, 2, 2)
plt.title(f'Segmented Image (k={k})')
plt.imshow(img)
plt.axis('off')
# Show the figure with the original and segmented images
plt.tight_layout()
plt.show()
# Plot the percentage of segmentation achieved for each cluster
plt.figure(figsize=(8, 4))
plt.bar(range(k), segmentation_percentages, color='blue')
plt.xlabel('Cluster')
plt.ylabel('Segmentation Percentage')
plt.title('Segmentation Percentage per Cluster')
plt.xticks(range(k))
plt.show()