《FCM图像分割算法MATLAB源代码.docx》由会员分享,可在线阅读,更多相关《FCM图像分割算法MATLAB源代码.docx(6页珍藏版)》请在第一文库网上搜索。
1、FCM图像分割算法function fcmapp(file, cluster_n)% FCMAPP% fcmappffile, cluter_n) segments a image named file using the algorithm% FCM.% in% file: the path of the image to be clustered.% cluster_n: the number of cluster for FCM.eval(info=imfinfo(,/file/switch info.ColorTypecase ruecoloreval(,RGB=imread(,zfi
2、le,%X, map = rgb2ind(RGB, 256);I = rgb2gray(RGB);clear RGB;case indexedeval(X, maptimread(二file,I = ind2gray(X, map);clear X;case grayscale1eval(l=imread( file,););end;I = im2double(l);filename = file(l: find(file=.)-l);data = reshape(l, numel(l), 1);ticcenter, U, obj_fcn=fcm(data, cluster_n);elapse
3、dtime = toe;%eval(save(/ filename, int2str(cluster_n)/.mat, center, U, obj_fcnz elapsedtime);1);fprintf(elapsedtime = %d,elapsedtime);maxll=max(U);temp = sort(center, Ascend1);for n = l:cluster_n;eval(,cluster,int2str(n)/ Jndex = find(U(, int2str(n)z= maxU););index = find(temp = center(n);switch ind
4、excase 1color_class = 0;case cluster ncolor_class = 255;otherwisecolor_class = fix(255*(index-l)/(cluster_n-l);endeval(l(clusterJnt2str(n)z ,_ndex(:)=/ int2str(color_class)/;);end;filename = file(l:find(file=.)-l);I = mat2gray(l);%eval(imwrite(l/z filenameJ-Seg, int2str(cluster_n), .bmp););imwritefl
5、, ,temptu2_4.bmp,/bmp,);imview(l);function fcmapp(file, cluster_n)% FCMAPP% fcmappffile, cluter_n) segments a image named file using the algorithm%FCM.% in% file: the path of the image to be clustered.% cluster_n: the number of cluster for FCM.eval(info=imfinfo(,/file/ ););switch info.ColorTypecase
6、truecoloreval(RGB=imread(,/file/ ););%X, map = rgb2ind(RGB, 256);I = rgb2gray(RGB);clear RGB;case indexedeval(X, maptimread(二file,I = ind2gray(X, map);clear X;case grayscaleeval(l=imread( file,););end;I = im2double(l);filename = file(l: find(file=.)-l);data = reshape(l, numel(l), 1);ticcenter, U, ob
7、j_fcn=fcm(data, cluster_n);elapsedtime = toe;%eval(save(,filenamej int2str(cluster_n),mat”center: UJobj_fcnJelapsedtime););fprintf(elapsedtime = %d,elapsedtime);maxU=max(U);temp = sort(center, Ascend1);for n = l:cluster_n;eval(cluster/int2str(n)/ Jndex = find(U(,int2str(n),= maxU););index = find(tem
8、p = center(n);switch indexcase 1color_class = 0;case cluster_ncolor_class = 255;otherwisecolor_class = fix(255*(index-l)/(cluster_n-l);endeval(l(clusterJnt2str(n)z ,_ndex(:)=/ int2str(color_class)/;);end;filename = file(l:find(file=.)-l);I = mat2gray(l);%eval(imwrite(l/z filenameJ-Seg, int2str(clust
9、er_n), .bmp););imwrited r.bmp);imview(l);主程序1lmageDir=.;%directory containing the images%path(.);%cmpviapath(,.1);img=im2double(imresize(imread(lmageDir 12.png),2);figure(l); imagesc(img); axis imageny,nx,nc=size(img);imgc=applycform(img,makecform(,srgb2lab,);d=reshape(imgc(:/:,2:3),ny*nx,2);d(:,l)=
10、d(:/l)/max(d(:,l) ; d(:/2)=d(:/2)/max(d(:/2);%d=d ./ (repmat(sqrt(sum(d.A2,2),l,3)+eps();k=4; % number of clusters%IO c = kmeans(d, k/DisplayJiter/MaxiterlOO);10 c = kmeans(d, kjMaxiter,100);IO=reshape(IO,ny,nx);figure(2); imagesc(IO); axis image ;%c= 0.37 0.37 0.37; 0.77 0.73 0.66 ; 0.64 0.77 0.41;
11、 0.81 0.76 0.58 ;.%0.85 0.81 0.73 ;%c=0.99 0.76 0.15 ; 0.55 0.56 0.15 ;%c= 0.64 0.64 0.67 ; 0.27 0.45 0.14;%c=c./ (repmat(sqrt(sum(c.A2,2),l,3)+eps();% Data termDc=zeros(ny,nx,k);for i=l:k,dif=d-repmat(c(i,:),ny*nx,l);Dc(:,:,i)= reshape(sum(dif.A2Z,nynx);end;% Smoothness termSc=(ones(k)-eye(k);% Edg
12、e termsg = fspecialCgauss, 13 13, 2);dy = fspecial(sobel);vf = conv2(g, dy, valid);Vc = zeros(ny,nx);He = Vc;for b=l:nc,Vc = max(Vc, abs(imfilter(img(:,:,b), vf, Symmetric1);He = max(Hc, abs(imfilter(img匕:,b), vf1, symmetric1);endgch=char;gch = GraphCutCopen, l*Dc, Sc/exp(-5*Vc),exp(-5*Hc);gch I = G
13、raphCut(expand,gch);gch = GraphCutCclose1, gch);label=l(100,200);lb=(|=label);lb=imdilate(lb/strel(disk/l)-lb;figure(3); image(img); axis image ; hold on ;contour(lb,l l,r); hold off; title(no edges);figure(4); imagesc(l); axis image ; titlefno edges1);gch = GraphCutCopen, De, 5*Sc,exp(-10*Vc),exp(-
14、10*Hc);gch I = GraphCut(expand,gch);gch = GraphCutCclose, gch);Ib=(l=label);lb=imdilate(lb,strel(disk,/l)-lb;figure(5); image(img); axis image ; hold on ;contour(lb,l l/r); hold off; title(edges);figure(6); imagesc(l); axis image ; title(edges);主程序2I = imread( *12.png1);I = rgb2gray(l);subplot(5,3,l),imshow(l);k=medfilt2(l,5,5);subplot(5,3/2)Jmshow(k);title(5*5中值滤波图像)%f=imread(tuxiangl.tif,);%subplot(l,2,l)Jmshow(f);%title(原图像);gl=histeq(k,256);subplot(5,3,3),imshow(gl);titled直方图匹配%g2=histeq(k2,256);%subplot(2,2/2