FCM图像分割算法MATLAB源代码.docx
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 ruecolor'eval(,RGB=imread(",zfile,%X, map = rgb2ind(RGB, 256);I = rgb2gray(RGB);clear RGB;case 'indexed'eval('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);elapsedtime = toe;%eval('save('/ filename, int2str(cluster_n)/.mat", ''center", "U", "obj_fcn"z "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 indexcase 1color_class = 0;case cluster ncolor_class = 255;otherwisecolor_class = fix(255*(index-l)/(cluster_n-l);endeval('l(cluster'Jnt2str(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, ,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 'truecolor'eval('RGB=imread(",/file/ '");');%X, map = rgb2ind(RGB, 256);I = rgb2gray(RGB);clear RGB;case 'indexed'eval('X, maptimread(二file,I = ind2gray(X, map);clear X;case 'grayscale'eval(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);elapsedtime = toe;%eval(save(',filenamej int2str(cluster_n),mat”center: "U"Jobj_fcn"Jelapsedtime");');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(temp = center(n);switch indexcase 1color_class = 0;case cluster_ncolor_class = 255;otherwisecolor_class = fix(255*(index-l)/(cluster_n-l);endeval('l(cluster'Jnt2str(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");');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)=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/Display'Jiter'/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; 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);% Edge 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 = GraphCut('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(-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