|
PSO的一维最大熵法阈值分割
链接:https://pan.baidu.com/s/1ChO4xGObCiTMqRT_s_qDpg&shfl=sharepset 提取码:zoad
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1\
- clc,clear,close all
- warning off
- format longG
- tic;
- % 2个阈值,实现3分类分割
- I = imread('car.bmp');
- % I = imread('screen.jpg');
- I = imresize(I, [256,256]);
- if(size(I,3)>1)
- im = rgb2gray(I);
- else
- im = I;
- end
- figure('color',[1,1,1]), imshow(im,[])
- %% PSO 参数
- c1 = 1.4995; % 学习因子
- c2 = 1.4995; % 学习因子
- Vmin = -5; % 最小速度
- Vmax = 5; % 最大速度
- maxiter = 30; % 迭代次数
- sizepop = 20; % 种群数量
- popmin = 1; popmax = 255; % x1
- Lmax = 256;
- % 2个阈值,实现3分割
- level = 3;
- nvar = level-1;
- %% 初始化种群
- for i=1:sizepop
- x1 = fix( popmin + (popmax-popmin)*rand );
- x2 = fix( popmin + (popmax-popmin)*rand );
- pop(i,1) = x1;
- pop(i,2) = x2;
- pop(i,:)=sort(pop(i,:));
- fitness(i) = H1_segFun2( im, pop(i,:) );
- V(i,1) = 0;
- V(i,2) = 0;
- end
- % 记录一组最优值
- [bestfitness,bestindex]=max(fitness);
- zbest=pop(bestindex,:); % 全局最佳
- gbest=pop; % 个体最佳
- fitnessgbest=fitness; % 个体最佳适应度值
- fitnesszbest=bestfitness; % 全局最佳适应度值
- wmax = 0.9; wmin = 0.4;
- % 迭代寻优
- for i=1:maxiter
- for j=1:sizepop
- % 自适应权重1
- % w = wmin + (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( max(fitness)-min(fitness) );
- % 自适应权重2
- % w = wmin - (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( mean(fitness)-min(fitness) );
- % 自适应权重3
- if fitnessgbest(j)<=mean(fitness)
- w = wmin - (wmax-wmin)*(fitnessgbest(j)-min(fitness))/( mean(fitness)-min(fitness) );
- else
- w = wmax;
- end
- % 速度更新
- V(j,:) = w*V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));
- % V--范围约束
- V(j, find(V(j,:)>Vmax)) = Vmax;
- V(j, find(V(j,:)<Vmin)) = Vmin;
-
- % 个体更新
- pop(j,:) = fix( pop(j,:) + 0.5 * V(j,:) );
-
- % pop 越界限制
- pop(j, find(pop(j,:)>popmax)) = popmax;
- pop(j, find(pop(j,:)<popmin)) = popmin;
-
- pop(j,:) = sort(pop(j, :));
-
- % 适应度更新
- fitness(j) = H1_segFun2(im, pop(j,:));
-
- % 比较 个体间比较
- if fitness(j)>fitnessgbest(j)
- fitnessgbest(j) = fitness(j);
- gbest(j,:) = pop(j,:);
- end
- if fitness(j)>bestfitness
- bestfitness = fitness(j);
- zbest = pop(j,:);
- end
-
- end
- fitness_iter(i) = bestfitness;
-
- end
- toc ;
- times = toc;
- fprintf('\n')
- disp(['计算时间 Time = ', num2str(times) ])
- fprintf('\n')
- zbest = sort(zbest);
- disp(['最优解 ', num2str(zbest)])
- fprintf('\n')
- figure('color',[1,1,1])
- plot(fitness_iter,'ro-','linewidth',2)
- % 显示
- Iout = imageGray( im, zbest );
- figure('color',[1,1,1])
- imshow(Iout,[])
- colormap jet;
- axis equal
复制代码 适应度函数:- function maxH = H1_segFun2(I, t)
- % 最原始的一维最大熵法,进行图像的3分割,level=3,将图像分为3部分
- [n_count, x_value] = imhist(I(:,:,1)); % 第一通道来分割
- Nt = size(I,1)*size(I,2);
- Lmax = 256; % 最大灰阶, uin8
- for i=1:Lmax
- p(i) = n_count(i)/Nt; % 概率分布
- end
- % H0 = -1* sum( p(1:t).*log(p(1:t) + eps) );
- % HB = -1* sum( p(t+1:Lmax).*log(p(t+1:Lmax) + eps) );
- % maxH = H0+HB; % 一维最大熵
- % 改进
- % p1 = log( sum(p(1:t)) + eps );
- % H0 = sum( p(1:t).*log(p(1:t) + eps) )./ ( sum(p(1:t)) + eps );
- % p2 = log( sum(p(t+1:Lmax)) + eps );
- % HB = sum( p(t+1:Lmax).*log(p(t+1:Lmax) + eps) )./ ( sum(p(t+1:Lmax)) + eps );
- % maxH = p1+p2-H0-HB; % 一维最大熵
- % % 3分割
- % t1 = t(1);
- % t2 = t(2);
- % p1 = log( sum(p(1:t1)) + eps );
- % p2 = log( sum(p(t1+1:t2)) + eps );
- % p3 = log( sum(p(t2+1:Lmax)) + eps );
- % p4 = sum( p(1:t1).*log(p(1:t1) + eps) )./ ( sum(p(1:t1)) + eps );
- % p5 = sum( p(t1+1:t2).*log(p(t1+1:t2) + eps) )./ ( sum(p(t1+1:t2)) + eps );
- % p6 = sum( p(t2+1:Lmax).*log(p(t2+1:Lmax) + eps) )./ ( sum(p(t2+1:Lmax)) + eps );
- % maxH = p1+p2+p3-p4-p5-p6; % 一维最大熵
- % 3分割
- % t1 = t(1);
- % t2 = t(2);
- p1 = log( sum(p(1:t(1))) + eps );
- p2 = log( sum(p(t(1)+1:t(2))) + eps );
- p3 = log( sum(p(t(2)+1:Lmax)) + eps );
- p4 = sum( p(1:t(1)).*log(p(1:t(1)) + eps) )./ ( sum(p(1:t(1))) + eps );
- p5 = sum( p(t(1)+1:t(2)).*log(p(t(1)+1:t(2)) + eps) )./ ( sum(p(t(1)+1:t(2))) + eps );
- p6 = sum( p(t(2)+1:Lmax).*log(p(t(2)+1:Lmax) + eps) )./ ( sum(p(t(2)+1:Lmax)) + eps );
- maxH = p1+p2+p3-p4-p5-p6; % 一维最大熵
复制代码
参考:【32】一维最大熵法进行2分割和3分割(MATLAB)
|
|