image_name=input('Enter the image name: ','s'); img = imread(image_name); image = double(rgb2gray(img)); k=input('What are the number of clusters? '); e=0.01; [row,col]=size(image); n=row*col; q=1; for i=1:row for j=1:col x(q)=image(i,j); q=q+1; end end % centers of clusters (created randomly) c=row*rand(1,k); % fuzzy c-means m=2; p=1; objective_function=0; % membership degrees u=zeros(n,k); for i=1:n for j=1:k w=0; for q=1:k d=((x(i)-c(j)).^2)/((x(i)-c(q)).^2); w=w+d.^(2/(m-1)); end u(i,j)=1/w; end objective_function=objective_function+((u(i,j)^2)*((x(i)-c(j)).^2)); end done=true; while done % find the new cluster centers for j=1:k w1=0; w2=0; for i=1:n w1=w1+(u(i,j)^2)*x(i); w2=w2+u(i,j)^2; end c(j)=w1/w2; end u1=u; % find new membership degrees for i=1:n for j=1:k w=0; for q=1:k d=((x(i)-c(j)).^2)/((x(i)-c(q)).^2); w=w+d.^(2/(m-1)); end u(i,j)=1/w; end objective_function=objective_function+((u(i,j)^2)*((x(i)-c(j)).^2)); end p=p+1; done=((u1-u).^2).^(1/2)=threshold im(j)=pos; non_ambiguous=non_ambiguous+x(j); nr=nr+1; elseif u(j,i)= thresh) = 0; bwImg(new_img < thresh) = 1; output_img= bwImg'; imwrite(output_img,'segmentation.png','png');