-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralGas Image Segmentation.py
83 lines (65 loc) · 2.61 KB
/
NeuralGas Image Segmentation.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 12 11:56:07 2024
@author: S.M.H Mousavi
"""
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, img_as_float
from sklearn.preprocessing import StandardScaler
from PIL import Image
class NeuralGas:
def __init__(self, n_units=10, max_iter=100, eta_start=0.5, eta_end=0.01, lambda_start=30, lambda_end=0.1):
self.n_units = n_units
self.max_iter = max_iter
self.eta_start = eta_start
self.eta_end = eta_end
self.lambda_start = lambda_start
self.lambda_end = lambda_end
self.units = None
def _update_learning_rate(self, i):
return self.eta_start * (self.eta_end / self.eta_start) ** (i / self.max_iter)
def _update_neighborhood_range(self, i):
return self.lambda_start * (self.lambda_end / self.lambda_start) ** (i / self.max_iter)
def train(self, data):
n_samples, n_features = data.shape
self.units = np.random.rand(self.n_units, n_features)
for i in range(self.max_iter):
eta = self._update_learning_rate(i)
lambd = self._update_neighborhood_range(i)
indices = np.random.permutation(n_samples)
for index in indices:
x = data[index]
dists = np.linalg.norm(self.units - x, axis=1)
ranking = np.argsort(dists)
for rank, idx in enumerate(ranking):
influence = np.exp(-rank / lambd)
self.units[idx] += eta * influence * (x - self.units[idx])
def assign_clusters(self, data):
dists = np.linalg.norm(self.units[:, np.newaxis, :] - data, axis=2)
return np.argmin(dists, axis=0)
# Load an image
image_path = 'scor2.jpg'
image = img_as_float(io.imread(image_path))
pixels = image.reshape(-1, 3) # Reshape image to a 2D array of pixels
# Normalize pixel data
scaler = StandardScaler()
pixels_normalized = scaler.fit_transform(pixels)
# Train Neural Gas
ng = NeuralGas(n_units=5, max_iter=20)
ng.train(pixels_normalized)
# Assign clusters to pixels
cluster_assignment = ng.assign_clusters(pixels_normalized)
# Reshape cluster assignments into the image shape
segmented_image = cluster_assignment.reshape(image.shape[0], image.shape[1])
# Plot the original and segmented images
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(segmented_image, cmap='viridis')
plt.title('NGN Segmented Image')
plt.axis('off')
plt.show()