利用最佳直方图熵法(KSW熵法)及传统遗传算法实现灰度图像二阈值分割
matlab代码如下:
1、main.m(主函数):
%%%利用最佳直方图熵法(KSW熵法)及传统遗传算法实现灰度图像二阈值分割
%%%主程序
%% 初始部分,读取图像及计算相关信息
clear;
close all;
clc;
I=imread('D:\MATLAB\work\2.21.jpg');
figure
figure(1),imshow(I);
I=rgb2gray(I);
% I=imread('Lenna.bmp');
hist=imhist(I); %显示图像数据柱状图
total=0;
for i=0:255
total=total+hist(i+1);
end
hist1=hist/total; %求每点的归一化//求像素为i的概率Pi
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 程序主干部分
%种群随机初始化,种群数取20,染色体二进制编码取16位
t0=clock;
population=20;
X00=round(rand(1,population)*255);
X01=round(rand(1,population)*255);
for i=1:population
X0(i,:)=[X00(i) X01(i)];
end
for i=1:population
if X0(i,1)>X0(i,2)
temp=X0(i,1);
X0(i,2)=temp;
X0(i,1)=temp;
else
end
adapt_value0(i)=ksw_2(X0(i,1),X0(i,2),0,255,hist1);
end
adapt_average0=mean(adapt_value0);
X1=X0;
adapt_value1=adapt_value0;
adapt_average1=adapt_average0;
%循环搜索,搜索代数取100
generation=100;
for k=1:generation
s1=select_2d(X1,adapt_value1);
s_code10=dec2bin(s1(:,1),8);
s_code11=dec2bin(s1(:,2),8);
[c10,c11]=cross_2d(s_code10,s_code11);
[v10,v11]=mutation_2d(c10,c11);
X20=(bin2dec(v10))';
X21=(bin2dec(v11))';
for i=1:population
X2(i,:)=[X20(i) X21(i)];
end
for i=1:population
adapt_value2(i)=ksw_2(X2(i,1),X2(i,2),0,255,hist1);
end
adapt_average2=mean(adapt_value2);
if abs(adapt_average2-adapt_average1)<=0.03
break;
else
X1=X2;
adapt_value1=adapt_value2;
adapt_average1=adapt_average2;
end
end
max_value=max(adapt_value2);
number=find(adapt_value2==max_value);
opt=X2(number(1),:);
t1=clock;
search_time=etime(t1,t0);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 阈值分割及显示部分
I_temp1=I;
[height,width]=size(I_temp1)
for i=1:height
for j=1:width
if I_temp1(i,j)1);
I_temp1(i,j)=0;
else if I_temp1(i,j)>opt(2);
I_temp1(i,j)=255;
else I_temp1(i,j)=180;
end
end
end
end
I1= I_temp1;
disp('灰度图像阈值分割的效果如图所示:');
disp('源图为:Fifure No.1');
disp('最佳直方图熵法及传统遗传算法阈二值分割后的图像为:Fifure No.2');
figure(2);
imshow(I);
title('源图');
figure(3);
imshow(I1);
title('最佳直方图熵法及传统遗传算法阈二值分割后的图像');
disp('最佳直方图熵法及传统遗传算法二阈值为(s,t):');
disp(opt(1));
disp(opt(2));
disp('最佳直方图熵法及传统遗传算法二阈值搜索所用时间(s):');
disp(search_time);
%% 程序结束
2、子函数
function s1=select_2d(X1,adapt_value1)
%选择算子
population=20;
total_adapt_value1=0;
for i=1:population
total_adapt_value1=total_adapt_value1+adapt_value1(i);
end
adapt_value1_new=adapt_value1/total_adapt_value1;
r=rand(1,population);
for i=1:population
temp=0;
for j=1:population
temp=temp+adapt_value1_new(j);
if temp>=r(i)
s1(i,:)=X1(j,:);
break;
end
end
end
function [c10,c11]=cross_2d(s_code10,s_code11)
%交叉算子
pc=0.8; %交叉概率取0.6
population=20;
%(1,2)/(3,4)/(5,6)进行交叉运算,(7,8)/(9,10)复制
ww0=s_code10;
ww1=s_code11;
for i=1:(pc*population/2)
r0=abs(round(rand(1)*10)-3);
r1=abs(round(rand(1)*10)-3);
for j=(r0+1):8
temp0=ww0(2*i-1,j);
ww0(2*i-1,j)=ww0(2*i,j);
ww0(2*i,j)=temp0;
end
for j=(r1+1):8
temp1=ww1(2*i-1,j);
ww1(2*i-1,j)=ww1(2*i,j);
ww1(2*i,j)=temp1;
end
end
c10=ww0;
c11=ww1;
function [v10,v11]=mutation_2d(c10,c11)
%变异算子
format long;
population=20;
pm=0.03;
for i=1:population
for j=1:8
r0=rand(1);
r1=rand(1);
if r0>pm
temp0(i,j)=c10(i,j);
else
tt=not(str2num(c10(i,j)));
temp0(i,j)=num2str(tt);
end
if r1>pm
temp1(i,j)=c11(i,j);
else
tt=not(str2num(c11(i,j)));
temp1(i,j)=num2str(tt);
end
end
end
v10=temp0;
v11=temp1;
function y=ksw_2(s,t,mingrayvalue,maxgrayvalue,hist1)
%计算最佳直方图熵(KSW熵)
Ps=0;%初始化
for i=mingrayvalue:s; %从0到s
Ps=Ps+hist1(i+1);%求和
end
Pt=0;%初始化
for i=s:t; %从s+1到t
Pt=Pt+hist1(i+1);%求和
end
Pn=0;%初始化
for i=t:maxgrayvalue; %从t+1到n
Pn=Pn+hist1(i+1);%求和
end
Hs=0;
for i=mingrayvalue:s
if hist1(i+1)==0%直方图值为零者赋零
temp=0;
else
temp=hist1(i+1)*log(1/hist1(i+1));%
end
Hs=Hs+temp;%
end
Ht=0;
for i=s:t
if hist1(i+1)==0%直方图值为零者赋零
temp=0;
else
temp=hist1(i+1)*log(1/hist1(i+1));%
end
Ht=Ht+temp;%
end
Hn=0;
for i=t:maxgrayvalue
if hist1(i+1)==0%直方图值为零者赋零
temp=0;
else
temp=hist1(i+1)*log(1/hist1(i+1));%
end
Hn=Hn+temp;%
end
if Ps==0 || Ps==1||Pt==0 || Pt==1||Pn==0 || Pn==1 % or(Pt==0,Pt==1)
temp1=0;
else
temp1=log(Ps)+log(Pt)+log(Pn)+Hs/Ps+Ht/Pt+Hn/Pn;%图像总熵
end
if temp1 < 0
H=0;
else
H=temp1;
end
y=H;