image enhancement using PSO algorithm

조회 수: 10 (최근 30일)
SHILPI GUPTA
SHILPI GUPTA 2019년 7월 7일
댓글: Alexandra Kate Mariano 2023년 1월 22일
particle.m
clc;
clear all;
close all;
%Reading image
Img = imread('w.jpg');
I = imresize(Img,[256 256]);
I = rgb2gray(I);
[m,n] = size(I);
%PSO implementation
P = 5; %swarm size
iter = 6; %number of iterations
c1 = 2;
c2 = 2; %usually c1=c2=2
r1 = rand;
r2 = rand;
fit_val = []; %matrix for storing fitness values
P_best = []; %matrix for storing pbest values
pbest = 0;
gbest = [];
%particle initialization
for i = 1:P
%updating particle position
a(i) = (1.5).*rand(1,1);
b(i) = (0.5).*rand(1,1);
c(i) = rand(1,1);
k(i) = 0.5+1.*rand(1,1);
fprintf('particle %d:: a=%f ; b=%f ; c=%f ;k=%f \n',i,a(i),b(i),c(i),k(i));
%updating particle velocity
v1(i) = 0.15*a(i);
v2(i) = 0.5*b(i);
v3(i) = 0.15*c(i);
v4(i) = 0.5*k(i);
f_v = [];
P_best = [];
pbest = [];
end
%iterations
for it=1:iter
IG=I;
fprintf('\n');
fprintf('--> ITERATION %d <--\n',it)
for i=1:P
fprintf('\n.....PARTICLE (%d)....',i);
figure
imshow(IG);
pause(1);
IG = uint8(trans_fcn(IG,a(i),b(i),c(i),k(i)));
[fitness,out1] = f_fcn(m,n,IG);
f_v(it,i) = fitness;
pbest(i) = max(f_v(:,i));
P_best(it,i) = pbest(i);
fprintf('FITNESS VALUE of (%d) iteration :\t\t\n',it);
disp(f_v());
fprintf('p_best_values: \n');
disp(P_best(it,:));
gbest(it) = max(f_v(:)); %calculating gbest
v1(i) = (1/it).*v1(i) + c1.*r1.*(gbest(it)-a(i)) + c2.*r2.*(pbest(i)-a(i));
a(i) = a(i) + v1(i);
% a(i) = ((a(i)-0.1139724)/(7.7605725-0.113924))*1.5; % 0 to 1.5
fprintf('a = %d \n',a(i));
v2(i)= (1/it).*v2(i) + c1.*r1.*(gbest(it)-b(i)) + c2.*r2.*(pbest(i)-b(i));
b(i) = b(i) + v2(i);
% b(i) = ((b(i)-(0.31926))/(0.9866753-(0.31926)))*0.5; % 0 to 0.5
fprintf('b = %d \n',b(i));
v3(i) = (1/it).*v3(i) + c1.*r1.*(gbest(it)-c(i)) + c2.*r2.*(pbest(i)-c(i));
c(i) = c(i) + v3(i);
% c(i) = (c(i)- 0.1117058)/(2.78389-0.7249451); % 0 to 1
fprintf('c = %d \n',c(i));
v4(i) = (1/it).*v4(i) + c1.*r1.*(gbest(it)-k(i)) + c2.*r2.*(pbest(i)-k(i));
k(i) = k(i) + v4(i);
% k(i) = 0.5+(((k(i)-(0.193213))/(0.9783839-(0.193213)))* 1);
fprintf('k = %d',k(i)); % 0.5 to 1.5
fprintf('\n');
% if (out1.n_edgels<500||out1.E<100000||out1.H<0.2123)
% break;
% end
end
fprintf('gbest of iteration %d :-> %f', it, gbest(it));
fprintf('\n');
fprintf('.......Iteration %d stops......', it)
end
figure
x= [1:iter];
y= gbest ;
plot(x,y)
% figure
% bar(gbest)
% imshow(IG);
---------------------------------------------------------------------------------
trans_fcn.m
function IG = trans_fcn(IG,a,b,c,k)
% function to transform image to enhanced version
D = mean2(double(IG)); %global mean
s = stdfilt(IG); %local std dev
SE = ones(3,3);
SE = SE/sum(SE(:));
lm = conv2(double(IG),double(SE),'same');
lm = uint8(lm); %cast back to uint8, this will round values
x = (s+b);
w = (k*D);
K = (w./x);
IG = (K.*(double(IG)-(c*double(lm)))+(double(lm).^a));
------------------------------------------------------------------------------------
function [f,out] = f_fcn(m,n,IG)
n_edgels = 0;
T = [m,n];
E = 0;
Is = imgradient(IG,'sobel'); % it will return binary image Is where the function finds edges it will return 1 else 0
%Is = histeq(IG);
n_edgels = nnz(Is); % number of edge pixels
T = uint8(Is).*IG;
E= sum(T(:)); % sum of edge intensities
H=entropy(IG); % entropy of enhanced image
% fitness value
f = log(log(double(E))).*(n_edgels/(m*n)).*exp(H); % we can get the density of the sparse matrix by nnz(s)/prod(size(s));
out.E = E;
out.n_edgels = n_edgels;
out.H = H;
out.f = f;
fprintf('................................\n');
fprintf('Sum of pixel intensities %d\n',E);
fprintf('Number of edge pixels %d\n',n_edgels);
fprintf('Entropy of image %d\n', H);
fprintf('----- fitness value is %f ------ ',f);
fprintf('\n');
end
------------------------------------------------------------------------------------------
This is the code to enhance the digital images using particle swarm optimization algortithm, but the resultant images are not accurate. also there is some problem in the code. Any suggestions for some improvement?
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 7월 7일
편집: Walter Roberson 2019년 7월 7일
Is there an error message?
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 7일
but the resultant images are not accurate
Elaborate? Be specific please. Also requested you to do format of the code by clicking on Insert code button.
op.png

댓글을 달려면 로그인하십시오.

답변 (2개)

SHILPI GUPTA
SHILPI GUPTA 2019년 7월 7일
prticle.m
clc;
clear all;
close all;
%Reading image
Img = imread('w.jpg');
I = imresize(Img,[256 256]);
I = rgb2gray(I);
[m,n] = size(I);
%PSO implementation
P = 5; %swarm size
iter = 6; %number of iterations
c1 = 2;
c2 = 2; %usually c1=c2=2
r1 = rand;
r2 = rand;
fit_val = []; %matrix for storing fitness values
P_best = []; %matrix for storing pbest values
pbest = 0;
gbest = [];
%particle initialization
for i = 1:P
%updating particle position
a(i) = (1.5).*rand(1,1);
b(i) = (0.5).*rand(1,1);
c(i) = rand(1,1);
k(i) = 0.5+1.*rand(1,1);
fprintf('particle %d:: a=%f ; b=%f ; c=%f ;k=%f \n',i,a(i),b(i),c(i),k(i));
%updating particle velocity
v1(i) = 0.15*a(i);
v2(i) = 0.5*b(i);
v3(i) = 0.15*c(i);
v4(i) = 0.5*k(i);
f_v = [];
P_best = [];
pbest = [];
end
%iterations
for it=1:iter
IG=I;
fprintf('\n');
fprintf('--> ITERATION %d <--\n',it)
for i=1:P
fprintf('\n.....PARTICLE (%d)....',i);
figure
imshow(IG);
pause(1);
IG = uint8(trans_fcn(IG,a(i),b(i),c(i),k(i)));
[fitness,out1] = f_fcn(m,n,IG);
f_v(it,i) = fitness;
pbest(i) = max(f_v(:,i));
P_best(it,i) = pbest(i);
fprintf('FITNESS VALUE of (%d) iteration :\t\t\n',it);
disp(f_v());
fprintf('p_best_values: \n');
disp(P_best(it,:));
gbest(it) = max(f_v(:)); %calculating gbest
v1(i) = (1/it).*v1(i) + c1.*r1.*(gbest(it)-a(i)) + c2.*r2.*(pbest(i)-a(i));
a(i) = a(i) + v1(i);
fprintf('a = %d \n',a(i));
v2(i)= (1/it).*v2(i) + c1.*r1.*(gbest(it)-b(i)) + c2.*r2.*(pbest(i)-b(i));
b(i) = b(i) + v2(i);
fprintf('b = %d \n',b(i));
v3(i) = (1/it).*v3(i) + c1.*r1.*(gbest(it)-c(i)) + c2.*r2.*(pbest(i)-c(i));
c(i) = c(i) + v3(i);
fprintf('c = %d \n',c(i));
v4(i) = (1/it).*v4(i) + c1.*r1.*(gbest(it)-k(i)) + c2.*r2.*(pbest(i)-k(i));
k(i) = k(i) + v4(i);
fprintf('k = %d',k(i));
fprintf('\n');
end
fprintf('gbest of iteration %d :-> %f', it, gbest(it));
fprintf('\n');
fprintf('.......Iteration %d stops......', it)
end
figure
x= [1:iter];
y= gbest ;
plot(x,y)
.....................................................................
f_fcn.m
function [f,out] = f_fcn(m,n,IG)
n_edgels = 0;
T = [m,n];
E = 0;
Is = imgradient(IG,'sobel'); % it will return binary image Is where the function finds edges it will return 1 else 0
%Is = histeq(IG);
n_edgels = nnz(Is); % number of edge pixels
T = uint8(Is).*IG;
E= sum(T(:)); % sum of edge intensities
H=entropy(IG); % entropy of enhanced image
% fitness value
f = log(log(double(E))).*(n_edgels/(m*n)).*exp(H); % we can get the density of the sparse matrix by nnz(s)/prod(size(s));
out.E = E;
out.n_edgels = n_edgels;
out.H = H;
out.f = f;
fprintf('................................\n');
fprintf('Sum of pixel intensities %d\n',E);
fprintf('Number of edge pixels %d\n',n_edgels);
fprintf('Entropy of image %d\n', H);
fprintf('----- fitness value is %f ------ ',f);
fprintf('\n');
end
.................................................................
trans_fcn.m
function IG = trans_fcn(IG,a,b,c,k)
% function to transform image to enhanced version
D = mean2(double(IG)); %global mean
s = stdfilt(IG); %local std dev
SE = ones(3,3);
SE = SE/sum(SE(:));
lm = conv2(double(IG),double(SE),'same');
lm = uint8(lm); %cast back to uint8, this will round values
x = (s+b);
w = (k*D);
K = (w./x);
IG = (K.*(double(IG)-(c*double(lm)))+(double(lm).^a));
  댓글 수: 3
Image Analyst
Image Analyst 2019년 7월 7일
Not down here - up there in your original question. Oh never mind, I'll do that. You just concentrate on attaching 'w.jpg' to your original post at the top, NOT down here.
SHILPI GUPTA
SHILPI GUPTA 2019년 7월 8일
Did that.
I'm getting NAN values for pbest and a,b,c,k after 1 or 2 iterations. Any suggestions for the improvement?

댓글을 달려면 로그인하십시오.


Manisha Pandey
Manisha Pandey 2020년 1월 28일
Hi Shilpi
Did you get the NAN value problem resolved? Help me please if any update regarding the same.
  댓글 수: 3
Salem Mahdi
Salem Mahdi 2022년 8월 18일
I done kwon
Alexandra Kate Mariano
Alexandra Kate Mariano 2023년 1월 22일
Is there a new code that is working?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by