% ----------------------- README ------------------------------------------
% ------------------- 原创声明 -------------------------------------------
% -------------- 最后一次修改:2023/6/22 ------------------------------------
% ------------------- 欢迎关注₍^.^₎♡ -------------------------------------
% -------------- 项目:单阈值图像分割(一维KSW) --------------------------------
% -------------- B站:KAU的云实验台_s -------------------------------------
% -------------- CSDN:KAU的云实验台 ---------------------------------------
% -------------------------------------------------------------------------
%% 清空环境
clc
clear
%% 载入图像及其计算
I = imread('KimSoHyun.jpg');
% I=imread('Barbara.tif');
% I=imread('lena.bmp');
[~,~,n] = size(I);
if n==3 % 彩色灰度化
I = rgb2gray(I);
end
garynum = 255; % 灰度级为255
hist = imhist(I);
total = 0;
for i=0:255
total = total+hist(i+1);
end
hist1 = hist/total;
HT = 0;
for i=0:255
if hist1(i+1)==0
temp = 0;
else
temp = hist1(i+1)*log(1/hist1(i+1));
end
HT = HT+temp;
end
tic
%% 遗传算法参数
maxgen=500; %进化代数
sizepop=500; %种群规模
pcross=[0.9]; %交叉概率
pmutation=[0.2]; %变异概率
eps=1; % 二进制个数需要
bound=[0 255]; %变量范围
NC = size(bound,1);
num=max(ceil(log2((bound(2)-bound(1))/eps + 1)));
lenchrom=NC*num; %变量字串长度
trace=[];
%% 个体初始化
individuals=struct('fitness',zeros(1,sizepop), 'chrom',[]); %种群结构体
avgfitness=[]; %种群平均适应度
bestfitness=[]; %种群最佳适应度
bestchrom=[]; %适应度最好染色体
% 初始化种群
for i=1:sizepop
individuals.chrom(i,:)=Code(lenchrom,bound); %随机产生个体
x=individuals.chrom(i,:);
individuals.fitness(i)=fun(x,I,bound,lenchrom,hist1,HT); %个体适应度
end
%找最好的染色体
[bestfitness bestindex]=max(individuals.fitness);
bestchrom=individuals.chrom(bestindex,:); %最好的染色体
avgfitness=sum(individuals.fitness)/sizepop; %染色体的平均适应度
% 记录每一代进化中最好的适应度和平均适应度
chrom = [];
%% 进化开始
for i=1:maxgen
% 选择操作
individuals=Select(individuals,sizepop);
avgfitness=sum(individuals.fitness)/sizepop;
% 交叉操作
individuals.chrom=Cross(pcross,lenchrom,individuals.chrom,sizepop,bound);
% 变异操作
individuals.chrom=Mutation(pmutation,lenchrom,individuals.chrom,sizepop,bound);
% 计算适应度
for j=1:sizepop
x=individuals.chrom(j,:);
individuals.fitness(j)=fun(x,I,bound,lenchrom,hist1,HT);
end
%找到最小和最大适应度的染色体及它们在种群中的位置
[newbestfitness,newbestindex]=max(individuals.fitness);
[worestfitness,worestindex]=min(individuals.fitness);
% 代替上一次进化中最好的染色体
if bestfitness<newbestfitness
bestfitness=newbestfitness;
bestchrom=individuals.chrom(newbestindex,:);
end
individuals.chrom(worestindex,:)=bestchrom;
individuals.fitness(worestindex)=bestfitness;
avgfitness=sum(individuals.fitness)/sizepop;
chrom = [chrom;bestchrom];
% trace=[trace;avgfitness bestfitness]; %记录每一代进化中最好的适应度和平均适应度
trace(i) = bestfitness;
end
%进化结束
toc
%% 结果显示
[r ~]=size(trace);
gen = 1:maxgen;
plot(gen,trace(1,:),'Color','#1A3B30','LineWidth',3);
% hold on
% plot(gen,trace(2,:),'-.','Color','#509579','LineWidth',3);
% plot(gen,trace(3,:),'--','Color','#99BDBD','LineWidth',3);
% plot(gen,trace(4,:),':','Color','#C6E3D8','LineWidth',3);
xlabel('Generation','FontName','Times New Roman','FontSize',12);
ylabel('Fitness Value','FontName','Times New Roman','FontSize',12);
title('SGA','FontName','Times New Roman','FontSize',12);
% legend('1','2','3','4','FontName','Times New Roman','FontSize',11)
[x,y] = size(I);
img = zeros(x,y);
bestchrom=Dec1(bestchrom,bound(1),bound(2),lenchrom);
bestchrom
for i=1:x
for j=1:y
if I(i,j)<bestchrom
img(i,j) = 0;
else
img(i,j) = 255;
end
end
end
figure
subplot(121)
imshow(I);
subplot(122)
imshow(img)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MATLAB图像处理-GA优化的OTSU&KSWT&sallis图像分割及其MATLAB实现.rar (56个子文件)
MATLAB图像处理-GA优化的OTSU&KSWT&sallis图像分割及其MATLAB实现
Tsallis
testTsallisEntropyLinThresholding.m 1KB
subim2bw.m 452B
Select.m 850B
solo_th_SGA.m 4KB
Barbara.tif 263KB
KimSoHyun.jpg 141KB
lena.bmp 65KB
Code.m 460B
Cross.m 1KB
test.m 314B
tsallisEntropyThresholdingLin.m 2KB
fun.m 1KB
Dec1.m 574B
tsallisEntropyThresholding.m 2KB
Mutation.m 1KB
一维最大熵
KSW.m 1KB
Barbara.tif 263KB
KimSoHyun.jpg 141KB
lena.bmp 65KB
SGA
Select.m 850B
solo_th_SGA.m 5KB
Barbara.tif 263KB
KimSoHyun.jpg 141KB
lena.bmp 65KB
Code.m 460B
Cross.m 1KB
test.m 314B
fun.m 556B
Dec1.m 574B
Mutation.m 1KB
一维OTSU
OTSU.m 1KB
Barbara.tif 263KB
改进
KimSoHyun.jpg 141KB
road1.JPG 246KB
pro_pop.m 591B
self_crossover.m 2KB
self_mutation.m 1KB
test.m 314B
fun.m 897B
selection.m 1KB
Dec1.m 574B
Initial.m 513B
solo_th_AGA.m 4KB
KimSoHyun.jpg 141KB
lena.bmp 65KB
SGA
Select.m 850B
solo_th_SGA.m 4KB
Barbara.tif 263KB
KimSoHyun.jpg 141KB
lena.bmp 65KB
Code.m 460B
Cross.m 1KB
test.m 314B
fun.m 897B
Dec1.m 574B
Mutation.m 1KB
共 56 条
- 1
资源评论
林聪木
- 粉丝: 5238
- 资源: 54
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功