資源簡介
螢火蟲算法的源代碼,函數文件,可直接運行,用來尋找圖像閾值分割的最優值(三閾值)。
代碼片段和文件信息
function?fireflies
global?bestsolution
%?parameters?[n?N_iteration?alpha?betamin?gamma]
para=[60?15?0.5?0.2?1];
%help?fa_xdim.m
%?Simple?bounds/limits?for?d-dimensional?problems
d=3;
Lb=zeros(13);
Ub=255*ones(13);
%?Initial?random?guess
u0=Lb+(Ub-Lb).*rand(1d);
[ufvalNumEval]=ffa_mincon(@costu0LbUbpara);
%?Display?results
bestsolution=fix(sort(u))
bestojb=fval
total_number_of_function_evaluations=NumEval
b=imread(‘cameraman.tif‘);
%image=imread(‘lena.bmp‘);
%figureimshow(image);title(‘?圖一?原圖像‘)
%b=rgb2gray(image);??%彩色圖轉換為灰度圖
figure;imshow(b);title(‘??灰度圖像‘)
th=fix(u);
count=imhist(b);?%計算灰度直方圖分布
figureimhist(b);
[mn]=size(b);
%for?i=1:m
???%?for?j=1:n
???????%?if?b(ij)>=th(13)
?????????%??b(ij)=(th(13)+255)/2;
????????%end
????????%if?th(13)>b(ij)>=th(12)
??????????%??b(ij)=(th(12)+th(13))/2;
????????%end
????????%if?th(12)>b(ij)>=th(11)
?????????%??b(ij)=(th(11)+th(12))/2;
????????%end
????????%if?b(ij) ????????????%b(ij)=th(11)/2;
????????%end
????%end
%end
%figureimshow(b);title(‘?圖五?閾值分割的圖像‘)
%%%?Put?your?own?cost/objective?function?here?--------%%%
%%?Cost?or?objective?function
function?z=cost(x)
%A=imread(‘E:\rice.png‘);
%A=imread(‘E:\rice.png‘);
%A=imread(‘E:\coins.bmp‘);
%A=imread(‘E:\cameraman.bmp‘);
%A=imread(‘E:\saturn.png‘);
A=imread(‘cameraman.tif‘);
%A=rgb2gray(A);??
%A=imresize(A0.5);
%A=imread(‘E:\lenna.bmp‘);
[rc]=size(A);
x=fix(x);
if?x<0
????x=0;
end
if?x>255
????x=255;
end
P=imhist(A)/(r*c);%每一個像素點出現的概率
P=P‘+0.0000000000001;%P‘為P的轉置矩陣
S1=0+0.0000000000001;
S2=0+0.0000000000001;
S3=0+0.0000000000001;
S4=0+0.0000000000001;
%------------------計算某個像素點出現的概率---------
x(1:)=sort(x(1:));
if?0 ????P1=sum(P(1:(x(11)+1)))+0.0000000000001;
????P2=sum(P((x(11)+2):(x(12)+1)))+0.0000000000001;
????P3=sum(P((x(12)+2):(x(13)+1)))+0.0000000000001;
????P4=sum(P((x(13)+2):256))+0.0000000000001;
????for?k=0:x(11)
????????S1=S1-P(k+1)/P1*log(P(k+1)/P1+0.0000000000001);%熵
????end
????for?k=(x(11)+1):x(12)
????????S2=S2-P(k+1)/P2*log(P(k+1)/P2+0.0000000000001);
????end
????for?k=(x(12)+1):x(13)
????????S3=S3-P(k+1)/P3*log(P(k+1)/P3+0.0000000000001);
????end
????for?k=(x(13)+1):255
????????S4=S4-P(k+1)/P4*log(P(k+1)/P4+0.0000000000001);
????end
end
z=10000/(S1+S2+S3+S4);%最大熵?
%%%?End?of?the?part?to?be?modified?-------------------%%%
%%%?--------------------------------------------------%%%
%%%?Do?not?modify?the?following?codes?unless?you?want?%%%
%%%?to?improve?its?performance?etc????????????????????%%%
%?-------------------------------------------------------
%?===Start?of?the?Firefly?Algorithm?Implementation?======
%?????????Lb?=?lower?bounds/limits
%?????????Ub?=?upper?bounds/limits
%???para?==?optional?(to?control?the?Firefly?algorithm)
%?Outputs:?nbest???=?the?best?solution?found?so?far
%??????????fbest???=?the?best?objective?value
%??????NumEval?=?number?of?e
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????7087??2013-05-10?14:56??fireflies.m
-----------?---------??----------?-----??----
?????????????????7087????????????????????1
評論
共有 條評論