function gakmeansclc;close all; clear all;% sds - sampled dataset global sds;[im,map]=imread('ant.jpg');if ( size(im,3)==3) im=rgb2gray(im);endim=im2double(im);[row,col]=size(im);nc=5;L= row*col;rns=(10/100)*L; rp=randperm(L);sds=im(rp(1:rns));options=gaoptimset('PopulationSize',20,'PopulationType','doubleVector', ... 'CrossoverFraction',0.7, 'Generations',100,'EliteCount',2, ... 'SelectionFcn', @selectionroulette,'CrossoverFcn',@crossoverarithmetic,... 'MutationFcn',@mutationadaptfeasible,'PlotFcns',@gaplotbestf) ; % lower bound limit lb=zeros(nc,1);% upper bound limitub=ones(nc,1);[gics,gfv,exf] =ga(@objfunc,nc,[],[],[],[],lb,ub,[],options);[idx,cs,egr]=kmeanseg(im,gics);gsim=coloring(cs,idx);gsim=reshape(gsim,row,col,3);figure;imshow(gsim);title('GA-Kmeans');function egr=objfunc(cs)global sds nc = size(cs,2); [row,col]=size(sds); D=zeros(row,col,nc); % Euclidean distance between centroids and image's pixel for c=1: nc D(:,:,c)= (sds - cs(c)).^2 ; end % assign members (image pixels) to clusters [mv,~]=min(D,[],3); egr=sum(mv(:)); function gsim=coloring(cs,idx)nc = length(cs);[~,ind]=sort(cs);gidx=zeros(size(idx));for s=1: nc gidx(idx==ind(s)) =s; endcolors = hsv(nc);gsim= colors(gidx,:); function [idx,cs,egr]=kmeanseg(im,cs)%number of IterationT= 50; t=0; nc=length(cs);[row,col]=size(im);D=zeros(row,col,nc);pcs=cs;egr =[];eps=1.e-8; cmx=1;while ( teps ) % Euclidean distance between centroids and image's pixel for c=1: nc D(:,:,c)= (im - cs(c)).^2 ; end % assign members (image pixels) to clusters [mv,idx]=min(D,[],3); % cluster centroid updation for c=1: nc I = (idx==c); cs(c) = mean( mean(im(I)) ); end % find if any member label changes between current and previous iteration cmx = max( abs(cs-pcs) ); pcs = cs; t= t+1; %sum difference between centroid and their members % and store it for ploting energy minimization functions egr= [egr; sum(mv(:)) ]; end
往期回顾>>>>>>
【模式识别】Matlab指纹识别【优化求解】A*算法解决三维路径规划问题 matlab自动识别银行卡号【优化问题】基于栅格地图——遗传算法的机器人最优路径规划【优化求解】模拟退火遗传实现带时间窗的车辆路径规划问题【数学建模】Matlab实现SEIR模型
今天的文章遗传算法matlab_【图像处理】基于遗传算法的图像边缘检测分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/33242.html