clear;
clc;
data=randn(310,2);
[center,U,obj_fcn] = fcm(data,2);
plot(data(:,1), data(:,2),'o');
hold on;
maxU = max(U);
% Find the data points with highest grade of membership in cluster 1
index1 = find(U(1,:) == maxU);
% Find the data points with highest grade of membership in cluster 2
index2 = find(U(2,:) == maxU);
%line(data(index1,1),data(index1,2),'marker','*','color','g');
%line(data(index2,1),data(index2,2),'marker','*','color','r');
% Plot the cluster centers
plot([center([1 2],1)],[center([1 2],2)],'*','color','k');
hold off;
% I = imread('lena.jpg');
%I = imread('eight.tif');
% I = imread('romantic.jpg');
I = imread('bacteria.jpg');
I=rgb2gray(I);
figure,imshow(I,[]);
title('原图');
g=imnoise(I,'salt & pepper',0.02); % 给图像添加高斯噪声
figure,imshow(g,[]);
title('添加高斯噪声后的图像');
g = double(g);
[m,n] = size(g);
k=2;
% ---------fcm----------------
fcm_label=zeros(m*n,1);
[O,U,obj_fcn1] =fcm(g(:), k);
maxU = max(U);
for j=1:k
index = find(U(j, :) == maxU);
fcm_label(index) = j;
end
fcm_result=reshape(fcm_label,[m n]);
figure, imshow(fcm_result,[]);
title('fcm分割结果');
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.