Skip to content

Commit e6428ae

Browse files
Add files via upload
1 parent 2170355 commit e6428ae

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

GasNN.m

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
function NGNnetwork = GasNN(X, params)
2+
3+
%% Load
4+
nData = size(X,1);
5+
nDim = size(X,2);
6+
X = X(randperm(nData),:);
7+
Xmin = min(X);
8+
Xmax = max(X);
9+
10+
%% Params
11+
N = params.N;
12+
MaxIt = params.MaxIt;
13+
tmax = params.tmax;
14+
epsilon_initial = params.epsilon_initial;
15+
epsilon_final = params.epsilon_final;
16+
lambda_initial = params.lambda_initial;
17+
lambda_final = params.lambda_final;
18+
T_initial = params.T_initial;
19+
T_final = params.T_final;
20+
21+
%% Initialization
22+
w = zeros(N, nDim);
23+
for i = 1:N
24+
w(i,:) = unifrnd(Xmin, Xmax);
25+
end
26+
C = zeros(N, N);
27+
t = zeros(N, N);
28+
tt = 0;
29+
30+
%% Body
31+
for it = 1:MaxIt
32+
for l = 1:nData
33+
% Slect Input Vector
34+
x = X(l,:);
35+
% Competion and Ranking
36+
d = pdist2(x,w);
37+
[~, SortOrder] = sort(d);
38+
% Calculate Parameters
39+
epsilon = epsilon_initial*(epsilon_final/epsilon_initial)^(tt/tmax);
40+
lambda = lambda_initial*(lambda_final/lambda_initial)^(tt/tmax);
41+
T = T_initial*(T_final/T_initial)^(tt/tmax);
42+
% Adaptation
43+
for ki = 0:N-1
44+
i=SortOrder(ki+1);
45+
w(i,:) = w(i,:) + epsilon*exp(-ki/lambda)*(x-w(i,:));
46+
end
47+
tt = tt + 1;
48+
% Creating Links
49+
i = SortOrder(1);
50+
j = SortOrder(2);
51+
C(i,j) = 1;
52+
C(j,i) = 1;
53+
t(i,j) = 0;
54+
t(j,i) = 0;
55+
% Aging
56+
t(i,:) = t(i,:) + 1;
57+
t(:,i) = t(:,i) + 1;
58+
% Remove Old Links
59+
L = t(i,:)>T;
60+
C(i, L) = 0;
61+
C(L, i) = 0;
62+
end
63+
disp(['Iteration ' num2str(it)]);
64+
end
65+
%% Results
66+
NGNnetwork.w = w;
67+
NGNnetwork.C = C;
68+
NGNnetwork.t = t;
69+
end

NGNImageSegmentation.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
% Image Segmentation and Quantization by Neural Gas Network (NGN)
2+
% Define number of segments and iterations and get the output.
3+
% Org is image. You can use your image.
4+
% ParVal.N is Number of Segments
5+
% ParVal.MaxIt is Number of runs
6+
%----------------------------------------------------------------------
7+
clc;
8+
clear;
9+
close all;
10+
11+
%% Load Image
12+
Org=imread('Veg.jpg');
13+
X = rgb2gray(Org);
14+
X=double(X);
15+
img=X;
16+
X=X(:)';
17+
18+
%% Neural Gas Network (NGN) Parameters
19+
20+
ParVal.N = 16; % Number of Segments
21+
ParVal.MaxIt = 50; % Number of runs
22+
23+
ParVal.tmax = 100000;
24+
25+
ParVal.epsilon_initial = 0.3;
26+
ParVal.epsilon_final = 0.02;
27+
ParVal.lambda_initial = 2;
28+
ParVal.lambda_final = 0.1;
29+
ParVal.T_initial = 5;
30+
ParVal.T_final = 10;
31+
32+
%% Training Neural Gas Network
33+
NGNnetwok = GasNN(X, ParVal);
34+
35+
%% Vector to image and plot
36+
Weight=sum(round(rescale(NGNnetwok.w,1,ParVal.N)));
37+
Weight=round(rescale(Weight,1,ParVal.N));
38+
indexed=reshape(Weight(1,:),size(img));
39+
segmented = label2rgb(indexed);
40+
% Plot Res
41+
figure('units','normalized','outerposition',[0 0 1 1])
42+
subplot(2,2,1)
43+
imshow(Org,[]); title('Original');
44+
subplot(2,2,2)
45+
imshow(img,[]); title('Grey');
46+
subplot(2,2,3)
47+
imshow(segmented);
48+
title(['Segmented in [' num2str(ParVal.N) '] Segments']);
49+
subplot(2,2,4)
50+
imshow(indexed,[]);
51+
title(['Quantized in [' num2str(ParVal.N) '] Thresholds']);
52+
53+

Veg.jpg

143 KB
Loading

0 commit comments

Comments
 (0)