|
PSO算法的最大熵阈值图像分割:http://www.docin.com/p-603535257.html
具体的二分割程序:
- % 最大熵法分割
- % http://www.docin.com/p-603535257.html
- p1 = log( sum( probR(1:xR) ) );
- p2 = log( sum( probR(xR+1:Lmax) ) );
- p3 = sum( probR(1:xR).*log(probR(1:xR) + eps) ) ./ sum( probR(1:xR) );
- p4 = sum( probR(xR+1:Lmax).*log(probR(xR+1:Lmax) + eps) ) ./ sum( probR(xR+1:Lmax) );
-
- fitness = p1+p2-p3-p4; % 适应度值
复制代码 多分割的话,这篇文章,还是有些坑啊。- p1 = log( sum( p(1:t(1)) ) );
- p2 = log( sum( p(t(1)+1:t(2)) ) );
- p3 = log( sum( p(t(2)+1:Lmax) ) );
- 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 );
- fitness = p1+p2+p3-p4-p5-p6; % 适应度值
复制代码
也可以参考:https://wenku.baidu.com/view/3fa4312ffd0a79563c1e72f9.html
二维最大熵法:- function fitness = fun(im, t)
- % 输入:
- % I是图像
- % t是阈值
- % 首先以原始灰度图像(L个灰度级)中各象素及其4邻域的4个象素为一个区域,
- % 计算出区域灰度均值图像(L个灰度级),
- % 这样原始图像中的每个象素都对应一个点灰度-区域灰度均值对,
- % 这样的数据对存在L×L种可能的取值
- h = fspecial('average',[2,2]); % 2x2均值模板
- meanI = imfilter(im, h); % 均值图像
- Lmax = 256;
- % pi,j为点灰度-区域灰度均值对(i,j)发生的概率
- pij = zeros(Lmax,Lmax);
- for i=0:Lmax-1
- [x,y] = find(im==i); % 原图像寻找
- data=[];
- % for j=1:length(x)
- % data(j) = double( meanI(x(j),y(j)) );
- % end
- data = meanI( sub2ind(size(meanI),x, y) );
- n_countR = imhist(uint8(data)); % 直方图
- % pi,j为点灰度-区域灰度均值对(i,j)发生的概率
- pij(i+1, :) = n_countR;
- end
- pij = pij./(Lmax*Lmax);
- PA = sum( sum( pij(1:t(1), 1:t(2)) ) );
- % PB = sum( sum( pij(t(1)+1:Lmax, t(2)+1:Lmax) ) );
- HA = -sum( sum( pij(1:t(1),1:t(2)).*log(pij(1:t(1),1:t(2))+eps) ) );
- HL = -sum( sum( pij(1:Lmax,1:Lmax).*log(pij(1:Lmax,1:Lmax)+eps) ) );
- fitness = log( PA*(1-PA)+eps ) + HA./(PA+eps) + (HL-HA)./(1-PA+eps);
复制代码
|
|