-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDE Image Quantization.m
More file actions
111 lines (103 loc) · 2.75 KB
/
DE Image Quantization.m
File metadata and controls
111 lines (103 loc) · 2.75 KB
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
%% Differential Evolution image color quantization using clustering
clear;
clc;
warning('off');
img=imread('r.jpg');
img=im2double(img);
% Separating color channels
R=img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);
% Reshaping each channel into a vector and combine all three channels
X=[R(:) G(:) B(:)];
%% Starting DE Clustering
k = 6; % Number of Colors (cluster centers)
%---------------------------------------------------
CostFunction=@(m) ClusterCost(m, X); % Cost Function
VarSize=[k size(X,2)]; % Decision Variables Matrix Size
nVar=prod(VarSize); % Number of Decision Variables
VarMin= repmat(min(X),k,1); % Lower Bound of Variables
VarMax= repmat(max(X),k,1); % Upper Bound of Variables
% DE Parameters
MaxIt=100; % Maximum Iterations
nPop=k*2; % Population Size
%
beta_min=0.2; % Lower Bound of Scaling Factor
beta_max=0.8; % Upper Bound of Scaling Factor
pCR=0.2; % Crossover Probability
% Start
empty_individual.Position=[];
empty_individual.Cost=[];
empty_individual.Out=[];
BestSol.Cost=inf;
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
[pop(i).Cost, pop(i).Out]=CostFunction(pop(i).Position);
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
BestRes=zeros(MaxIt,1);
% DE Body
for it=1:MaxIt
for i=1:nPop
x=pop(i).Position;
A=randperm(nPop);
A(A==i)=[];
a=A(1);
b=A(2);
c=A(3);
% Mutation
beta=unifrnd(beta_min,beta_max,VarSize);
y=pop(a).Position+beta.*(pop(b).Position-pop(c).Position);
y=max(y,VarMin);
y=min(y,VarMax);
% Crossover
z=zeros(size(x));
j0=randi([1 numel(x)]);
for j=1:numel(x)
if j==j0 || rand<=pCR
z(j)=y(j);
else
z(j)=x(j);
end
end
NewSol.Position=z;
[NewSol.Cost, NewSol.Out]=CostFunction(NewSol.Position);
if NewSol.Cost<pop(i).Cost
pop(i)=NewSol;
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
end
% Update Best Cost
BestRes(it)=BestSol.Cost;
% Iteration
disp(['In Iteration # ' num2str(it) ': Highest Cost IS = ' num2str(BestRes(it))]);
DECenters=Res(X, BestSol);
end
DElbl=BestSol.Out.ind;
% Plot DE Train
figure;
plot(BestRes,'--k','linewidth',2);
title('DE Train');
xlabel('DE Iteration Number');
ylabel('DE Best Cost Value');
%% Converting cluster centers and its indexes into image
Z=DECenters(DElbl',:);
R2=reshape(Z(:,1),size(R));
G2=reshape(Z(:,2),size(G));
B2=reshape(Z(:,3),size(B));
% Attaching color channels
quantized=zeros(size(img));
quantized(:,:,1)=R2;
quantized(:,:,2)=G2;
quantized(:,:,3)=B2;
% Plot Results
figure;
subplot(1,2,1);
imshow(img);title('Original');
subplot(1,2,2);
imshow(quantized);title('Quantized Image');