基于差分进化算法的图像分割matlab代码
时间: 2023-12-06 16:05:03 浏览: 21
以下是基于差分进化算法的图像分割matlab代码示例:
```matlab
clc;
clear all;
close all;
warning off;
I=imread('lena.bmp');
figure(1);
imshow(I);
title('Original Image');
I=double(I);
[N M]=size(I);
f=3;
for i=1:N-2
for j=1:M-2
b1(i,j)=I(i,j)+I(i,j+1)+I(i,j+2)+I(i+1,j)+I(i+1,j+1)+I(i+1,j+2)+I(i+2,j)+I(i+2,j+1)+I(i+2,j+2);
b(i,j)=b1(i,j)/f;
end
end
B=imresize(b,[256,256]);
figure(2);
imshow(uint8(B));
title('Smoothed Image');
I=B;
K=3;
pop_size=20*K;
max_gen=200;
F=0.5;
CR=0.8;
for i=1:pop_size
X(i,1:K)=randperm(K);
end
for gen=1:max_gen
for i=1:pop_size
for j=1:K
if X(i,j)==1
Y(i,j)=randi(I(1,1));
elseif X(i,j)==2
Y(i,j)=randi(I(1,end));
elseif X(i,j)==3
Y(i,j)=randi(I(end,1));
elseif X(i,j)==4
Y(i,j)=randi(I(end,end));
end
end
[f1(i),f2(i)]=fitness(Y(i,:),I,X(i,:),K);
F1(i)=f1(i)/(f1(i)+f2(i));
F2(i)=f2(i)/(f1(i)+f2(i));
end
[F1_sort,F1_index]=sort(F1,'descend');
[F2_sort,F2_index]=sort(F2,'descend');
for i=1:pop_size
for j=1:K
w(i,j)=F1_sort(i)*F2_sort(j);
end
end
for i=1:pop_size
r1=randi(pop_size);
while r1==i
r1=randi(pop_size);
end
r2=randi(pop_size);
while r2==r1 | r2==i
r2=randi(pop_size);
end
r3=randi(pop_size);
while r3==r2 | r3==r1 | r3==i
r3=randi(pop_size);
end
v(i,:)=X(i,:)+(F.*w(i,:).*((X(r1,:)-X(r2,:))+(X(r3,:)-X(i,:))));
for j=1:K
if v(i,j)<=0
v(i,j)=1;
elseif v(i,j)>K
v(i,j)=K;
end
end
if rand<CR
U(i,:)=v(i,:);
else
U(i,:)=X(i,:);
end
[f1_U(i),f2_U(i)]=fitness(Y(i,:),I,U(i,:),K);
F1_U(i)=f1_U(i)/(f1_U(i)+f2_U(i));
F2_U(i)=f2_U(i)/(f1_U(i)+f2_U(i));
if F1_U(i)>=F1(i) & F2_U(i)>=F2(i)
X(i,:)=U(i,:);
f1(i)=f1_U(i);
f2(i)=f2_U(i);
end
end
[bestFit,g]=min(f1);
BestX=X(g,:);
BestY=Y(g,:);
BestX=BestX-1;
for i=1:N
for j=1:M
if BestX(1)==1 & BestX(2)==2 & BestX(3)==3
if I(i,j)<=BestY(1)
FIm(i,j)=255;
elseif I(i,j)>BestY(1) & I(i,j)<=BestY(2)
FIm(i,j)=128;
elseif I(i,j)>BestY(2) & I(i,j)<=BestY(3)
FIm(i,j)=0;
elseif I(i,j)>BestY(3)
FIm(i,j)=255;
end
elseif BestX(1)==1 & BestX(2)==3 & BestX(3)==2
if I(i,j)<=BestY(1)
FIm(i,j)=255;
elseif I(i,j)>BestY(1) & I(i,j)<=BestY(3)
FIm(i,j)=128;
elseif I(i,j)>BestY(3) & I(i,j)<=BestY(2)
FIm(i,j)=0;
elseif I(i,j)>BestY(2)
FIm(i,j)=255;
end
elseif BestX(1)==2 & BestX(2)==1 & BestX(3)==3
if I(i,j)<=BestY(2)
FIm(i,j)=255;
elseif I(i,j)>BestY(2) & I(i,j)<=BestY(1)
FIm(i,j)=128;
elseif I(i,j)>BestY(1) & I(i,j)<=BestY(3)
FIm(i,j)=0;
elseif I(i,j)>BestY(3)
FIm(i,j)=255;
end
elseif BestX(1)==2 & BestX(2)==3 & BestX(3)==1
if I(i,j)<=BestY(2)
FIm(i,j)=255;
elseif I(i,j)>BestY(2) & I(i,j)<=BestY(3)
FIm(i,j)=128;
elseif I(i,j)>BestY(3) & I(i,j)<=BestY(1)
FIm(i,j)=0;
elseif I(i,j)>BestY(1)
FIm(i,j)=255;
end
elseif BestX(1)==3 & BestX(2)==1 & BestX(3)==2
if I(i,j)<=BestY(3)
FIm(i,j)=255;
elseif I(i,j)>BestY(3) & I(i,j)<=BestY(1)
FIm(i,j)=128;
elseif I(i,j)>BestY(1) & I(i,j)<=BestY(2)
FIm(i,j)=0;
elseif I(i,j)>BestY(2)
FIm(i,j)=255;
end
elseif BestX(1)==3 & BestX(2)==2 & BestX(3)==1
if I(i,j)<=BestY(3)
FIm(i,j)=255;
elseif I(i,j)>BestY(3) & I(i,j)<=BestY(2)
FIm(i,j)=128;
elseif I(i,j)>BestY(2) & I(i,j)<=BestY(1)
FIm(i,j)=0;
elseif I(i,j)>BestY(1)
FIm(i,j)=255;
end
end
end
end
figure(3);
imshow(uint8(FIm));
title(['Segmented Image in Generation ',num2str(gen)]);
end
```
注意:上述代码只是一个示例,具体使用时需要根据实际场景和需求进行修改和优化。