码迷,mamicode.com
首页 > 编程语言 > 详细

【50】 【图像处理】基于遗传算法的图像边缘检测

时间:2021-01-02 11:11:33      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:diff   iter   led   cli   jpg   nim   ati   mic   size   

function gakmeans?clc;close all; clear all;?% sds  - sampled dataset  global sds;?[im,map]=imread(‘ant.jpg‘);if ( size(im,3)==3) im=rgb2gray(im);end?im=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;   end?colors = 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 ( t<T  && cmx>eps )                        % 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         

 

 技术图片

 

 

 

注:完整代码或者代写添加QQ1575304183

【50】 【图像处理】基于遗传算法的图像边缘检测

标签:diff   iter   led   cli   jpg   nim   ati   mic   size   

原文地址:https://www.cnblogs.com/homeofmatlab/p/14207441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!