├── fcm_m1 ├── testpic.m ├── wheel.png ├── udinitfcm.m ├── uddistfcm.m ├── test.m ├── udstepfcm.m └── udfcm.m ├── fcm_s1 ├── testpic.m ├── wheel.png ├── padmatrix.m ├── udinitfcm_s1.m ├── uddistfcm_s1.m ├── test.m ├── obj_mat.m ├── udstepfcm_s1.m └── udfcm_s1.m ├── Enfcm ├── wheel.png ├── dist_enfcm.m ├── padmatrix.m ├── enfcm_init.m ├── test.m ├── stepenfcm.m └── enfcm.m ├── FGfcm ├── wheel.png ├── dist_fgfcm.m ├── padmatrix.m ├── fgfcm_init.m ├── test.m ├── stepfgfcm.m └── fgfcm.m ├── FLIcm ├── wheel.png ├── padmatrix.m ├── udinitfcm.m ├── test.m ├── test.m~ ├── stepflicm.m ├── stepflicm.m~ ├── uddistfcm.m ├── uddistfcm.m~ ├── flicm.m └── flicm.m~ ├── fcm_s2 ├── wheel.png ├── padmatrix.m ├── udinitfcm_s2.m ├── uddistfcm_s2.m ├── test.m ├── obj_mat.m ├── udstepfcm_s2.m └── udfcm_s2.m ├── FGfcm_s1 ├── wheel.png ├── dist_fgfcm.m ├── padmatrix.m ├── fgfcm_init.m ├── test.m ├── stepfgfcm.m └── fgfcm.m ├── FGfcm_s2 ├── wheel.png ├── dist_fgfcm.m ├── padmatrix.m ├── fgfcm_init.m ├── test.m ├── stepfgfcm.m └── fgfcm.m └── README.md /fcm_m1/testpic.m: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fcm_s1/testpic.m: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Enfcm/wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marearth/fcm_m/HEAD/Enfcm/wheel.png -------------------------------------------------------------------------------- /FGfcm/wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marearth/fcm_m/HEAD/FGfcm/wheel.png -------------------------------------------------------------------------------- /FLIcm/wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marearth/fcm_m/HEAD/FLIcm/wheel.png -------------------------------------------------------------------------------- /fcm_m1/wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marearth/fcm_m/HEAD/fcm_m1/wheel.png -------------------------------------------------------------------------------- /fcm_s1/wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marearth/fcm_m/HEAD/fcm_s1/wheel.png -------------------------------------------------------------------------------- /fcm_s2/wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marearth/fcm_m/HEAD/fcm_s2/wheel.png -------------------------------------------------------------------------------- /FGfcm_s1/wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marearth/fcm_m/HEAD/FGfcm_s1/wheel.png -------------------------------------------------------------------------------- /FGfcm_s2/wheel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marearth/fcm_m/HEAD/FGfcm_s2/wheel.png -------------------------------------------------------------------------------- /Enfcm/dist_enfcm.m: -------------------------------------------------------------------------------- 1 | function out=dist_enfcm(center,data) 2 | out = zeros(size(center, 1), size(data, 1)); 3 | for k = 1:size(center, 1), 4 | out(k, :) = abs(center(k)-data)'; 5 | end -------------------------------------------------------------------------------- /FGfcm/dist_fgfcm.m: -------------------------------------------------------------------------------- 1 | function out=dist_enfcm(center,data) 2 | out = zeros(size(center, 1), size(data, 1)); 3 | for k = 1:size(center, 1), 4 | out(k, :) = abs(center(k)-data)'; 5 | end -------------------------------------------------------------------------------- /FGfcm_s1/dist_fgfcm.m: -------------------------------------------------------------------------------- 1 | function out=dist_enfcm(center,data) 2 | out = zeros(size(center, 1), size(data, 1)); 3 | for k = 1:size(center, 1), 4 | out(k, :) = abs(center(k)-data)'; 5 | end -------------------------------------------------------------------------------- /FGfcm_s2/dist_fgfcm.m: -------------------------------------------------------------------------------- 1 | function out=dist_enfcm(center,data) 2 | out = zeros(size(center, 1), size(data, 1)); 3 | for k = 1:size(center, 1), 4 | out(k, :) = abs(center(k)-data)'; 5 | end -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fcm_m 2 | Here are some variants of FCM clustering methods in matlab code. 3 | You can refer to the following paper for further understanding: 4 | "A robust fuzzy local information C-means clustering algorithm" 5 | -------------------------------------------------------------------------------- /Enfcm/padmatrix.m: -------------------------------------------------------------------------------- 1 | function out=padmatrix(m,n) 2 | out=m; 3 | for i=1:n, 4 | out=padmatrix1(out); 5 | end 6 | function out=padmatrix1(m) 7 | m=[m(:,1) m]; 8 | m=[m m(:,size(m,2))]; 9 | m=[m(1,:);m]; 10 | m=[m;m(size(m,1),:)]; 11 | out=m; -------------------------------------------------------------------------------- /FGfcm/padmatrix.m: -------------------------------------------------------------------------------- 1 | function out=padmatrix(m,n) 2 | out=m; 3 | for i=1:n, 4 | out=padmatrix1(out); 5 | end 6 | function out=padmatrix1(m) 7 | m=[m(:,1) m]; 8 | m=[m m(:,size(m,2))]; 9 | m=[m(1,:);m]; 10 | m=[m;m(size(m,1),:)]; 11 | out=m; -------------------------------------------------------------------------------- /FLIcm/padmatrix.m: -------------------------------------------------------------------------------- 1 | function out=padmatrix(m,n) 2 | out=m; 3 | for i=1:n, 4 | out=padmatrix1(out); 5 | end 6 | function out=padmatrix1(m) 7 | m=[m(:,1) m]; 8 | m=[m m(:,size(m,2))]; 9 | m=[m(1,:);m]; 10 | m=[m;m(size(m,1),:)]; 11 | out=m; -------------------------------------------------------------------------------- /fcm_s1/padmatrix.m: -------------------------------------------------------------------------------- 1 | function out=padmatrix(m,n) 2 | out=m; 3 | for i=1:n, 4 | out=padmatrix1(out); 5 | end 6 | function out=padmatrix1(m) 7 | m=[m(:,1) m]; 8 | m=[m m(:,size(m,2))]; 9 | m=[m(1,:);m]; 10 | m=[m;m(size(m,1),:)]; 11 | out=m; -------------------------------------------------------------------------------- /fcm_s2/padmatrix.m: -------------------------------------------------------------------------------- 1 | function out=padmatrix(m,n) 2 | out=m; 3 | for i=1:n, 4 | out=padmatrix1(out); 5 | end 6 | function out=padmatrix1(m) 7 | m=[m(:,1) m]; 8 | m=[m m(:,size(m,2))]; 9 | m=[m(1,:);m]; 10 | m=[m;m(size(m,1),:)]; 11 | out=m; -------------------------------------------------------------------------------- /FGfcm_s1/padmatrix.m: -------------------------------------------------------------------------------- 1 | function out=padmatrix(m,n) 2 | out=m; 3 | for i=1:n, 4 | out=padmatrix1(out); 5 | end 6 | function out=padmatrix1(m) 7 | m=[m(:,1) m]; 8 | m=[m m(:,size(m,2))]; 9 | m=[m(1,:);m]; 10 | m=[m;m(size(m,1),:)]; 11 | out=m; -------------------------------------------------------------------------------- /FGfcm_s2/padmatrix.m: -------------------------------------------------------------------------------- 1 | function out=padmatrix(m,n) 2 | out=m; 3 | for i=1:n, 4 | out=padmatrix1(out); 5 | end 6 | function out=padmatrix1(m) 7 | m=[m(:,1) m]; 8 | m=[m m(:,size(m,2))]; 9 | m=[m(1,:);m]; 10 | m=[m;m(size(m,1),:)]; 11 | out=m; -------------------------------------------------------------------------------- /Enfcm/enfcm_init.m: -------------------------------------------------------------------------------- 1 | function U =enfcm_init(cluster_n, data_n) 2 | %INITFCM Generate initial fuzzy partition matrix for fuzzy c-means clustering. 3 | % U = INITFCM(CLUSTER_N, DATA_N) randomly generates a fuzzy partition 4 | % matrix U that is CLUSTER_N by DATA_N, where CLUSTER_N is number of 5 | % clusters and DATA_N is number of data points. The summation of each 6 | % column of the generated U is equal to unity, as required by fuzzy 7 | % c-means clustering. 8 | % 9 | % See also DISTFCM, FCMDEMO, IRISFCM, STEPFCM, FCM. 10 | 11 | % Roger Jang, 12-1-94. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | U = rand(cluster_n, data_n); 15 | col_sum = sum(U); 16 | U = U./col_sum(ones(cluster_n, 1), :); 17 | -------------------------------------------------------------------------------- /FGfcm/fgfcm_init.m: -------------------------------------------------------------------------------- 1 | function U =enfcm_init(cluster_n, data_n) 2 | %INITFCM Generate initial fuzzy partition matrix for fuzzy c-means clustering. 3 | % U = INITFCM(CLUSTER_N, DATA_N) randomly generates a fuzzy partition 4 | % matrix U that is CLUSTER_N by DATA_N, where CLUSTER_N is number of 5 | % clusters and DATA_N is number of data points. The summation of each 6 | % column of the generated U is equal to unity, as required by fuzzy 7 | % c-means clustering. 8 | % 9 | % See also DISTFCM, FCMDEMO, IRISFCM, STEPFCM, FCM. 10 | 11 | % Roger Jang, 12-1-94. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | U = rand(cluster_n, data_n); 15 | col_sum = sum(U); 16 | U = U./col_sum(ones(cluster_n, 1), :); 17 | -------------------------------------------------------------------------------- /FLIcm/udinitfcm.m: -------------------------------------------------------------------------------- 1 | function U = udinitfcm(cluster_n, data_n) 2 | %INITFCM Generate initial fuzzy partition matrix for fuzzy c-means clustering. 3 | % U = INITFCM(CLUSTER_N, DATA_N) randomly generates a fuzzy partition 4 | % matrix U that is CLUSTER_N by DATA_N, where CLUSTER_N is number of 5 | % clusters and DATA_N is number of data points. The summation of each 6 | % column of the generated U is equal to unity, as required by fuzzy 7 | % c-means clustering. 8 | % 9 | % See also DISTFCM, FCMDEMO, IRISFCM, STEPFCM, FCM. 10 | 11 | % Roger Jang, 12-1-94. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | U = rand(cluster_n, data_n); 15 | col_sum = sum(U); 16 | U = U./col_sum(ones(cluster_n, 1), :); 17 | -------------------------------------------------------------------------------- /fcm_m1/udinitfcm.m: -------------------------------------------------------------------------------- 1 | function U = udinitfcm(cluster_n, data_n) 2 | %INITFCM Generate initial fuzzy partition matrix for fuzzy c-means clustering. 3 | % U = INITFCM(CLUSTER_N, DATA_N) randomly generates a fuzzy partition 4 | % matrix U that is CLUSTER_N by DATA_N, where CLUSTER_N is number of 5 | % clusters and DATA_N is number of data points. The summation of each 6 | % column of the generated U is equal to unity, as required by fuzzy 7 | % c-means clustering. 8 | % 9 | % See also DISTFCM, FCMDEMO, IRISFCM, STEPFCM, FCM. 10 | 11 | % Roger Jang, 12-1-94. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | U = rand(cluster_n, data_n); 15 | col_sum = sum(U); 16 | U = U./col_sum(ones(cluster_n, 1), :); 17 | -------------------------------------------------------------------------------- /FGfcm_s1/fgfcm_init.m: -------------------------------------------------------------------------------- 1 | function U =enfcm_init(cluster_n, data_n) 2 | %INITFCM Generate initial fuzzy partition matrix for fuzzy c-means clustering. 3 | % U = INITFCM(CLUSTER_N, DATA_N) randomly generates a fuzzy partition 4 | % matrix U that is CLUSTER_N by DATA_N, where CLUSTER_N is number of 5 | % clusters and DATA_N is number of data points. The summation of each 6 | % column of the generated U is equal to unity, as required by fuzzy 7 | % c-means clustering. 8 | % 9 | % See also DISTFCM, FCMDEMO, IRISFCM, STEPFCM, FCM. 10 | 11 | % Roger Jang, 12-1-94. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | U = rand(cluster_n, data_n); 15 | col_sum = sum(U); 16 | U = U./col_sum(ones(cluster_n, 1), :); 17 | -------------------------------------------------------------------------------- /FGfcm_s2/fgfcm_init.m: -------------------------------------------------------------------------------- 1 | function U =enfcm_init(cluster_n, data_n) 2 | %INITFCM Generate initial fuzzy partition matrix for fuzzy c-means clustering. 3 | % U = INITFCM(CLUSTER_N, DATA_N) randomly generates a fuzzy partition 4 | % matrix U that is CLUSTER_N by DATA_N, where CLUSTER_N is number of 5 | % clusters and DATA_N is number of data points. The summation of each 6 | % column of the generated U is equal to unity, as required by fuzzy 7 | % c-means clustering. 8 | % 9 | % See also DISTFCM, FCMDEMO, IRISFCM, STEPFCM, FCM. 10 | 11 | % Roger Jang, 12-1-94. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | U = rand(cluster_n, data_n); 15 | col_sum = sum(U); 16 | U = U./col_sum(ones(cluster_n, 1), :); 17 | -------------------------------------------------------------------------------- /fcm_s1/udinitfcm_s1.m: -------------------------------------------------------------------------------- 1 | function U = udinitfcm_s1(cluster_n, data_n) 2 | %INITFCM Generate initial fuzzy partition matrix for fuzzy c-means clustering. 3 | % U = INITFCM(CLUSTER_N, DATA_N) randomly generates a fuzzy partition 4 | % matrix U that is CLUSTER_N by DATA_N, where CLUSTER_N is number of 5 | % clusters and DATA_N is number of data points. The summation of each 6 | % column of the generated U is equal to unity, as required by fuzzy 7 | % c-means clustering. 8 | % 9 | % See also DISTFCM, FCMDEMO, IRISFCM, STEPFCM, FCM. 10 | 11 | % Roger Jang, 12-1-94. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | U = rand(cluster_n, data_n); 15 | col_sum = sum(U); 16 | U = U./col_sum(ones(cluster_n, 1), :); 17 | -------------------------------------------------------------------------------- /fcm_s2/udinitfcm_s2.m: -------------------------------------------------------------------------------- 1 | function U = udinitfcm_s2(cluster_n, data_n) 2 | %INITFCM Generate initial fuzzy partition matrix for fuzzy c-means clustering. 3 | % U = INITFCM(CLUSTER_N, DATA_N) randomly generates a fuzzy partition 4 | % matrix U that is CLUSTER_N by DATA_N, where CLUSTER_N is number of 5 | % clusters and DATA_N is number of data points. The summation of each 6 | % column of the generated U is equal to unity, as required by fuzzy 7 | % c-means clustering. 8 | % 9 | % See also DISTFCM, FCMDEMO, IRISFCM, STEPFCM, FCM. 10 | 11 | % Roger Jang, 12-1-94. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | U = rand(cluster_n, data_n); 15 | col_sum = sum(U); 16 | U = U./col_sum(ones(cluster_n, 1), :); 17 | -------------------------------------------------------------------------------- /fcm_m1/uddistfcm.m: -------------------------------------------------------------------------------- 1 | function out = uddistfcm(center, data) 2 | %DISTFCM Distance measure in fuzzy c-mean clustering. 3 | % OUT = DISTFCM(CENTER, DATA) calculates the Euclidean distance 4 | % between each row in CENTER and each row in DATA, and returns a 5 | % distance matrix OUT of size M by N, where M and N are row 6 | % dimensions of CENTER and DATA, respectively, and OUT(I, J) is 7 | % the distance between CENTER(I,:) and DATA(J,:). 8 | % 9 | % See also FCMDEMO, INITFCM, IRISFCM, STEPFCM, and FCM. 10 | 11 | % Roger Jang, 11-22-94, 6-27-95. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | 14 | out = zeros(size(center, 1), size(data, 1)); 15 | 16 | % fill the output matrix 17 | 18 | if size(center, 2) > 1, 19 | for k = 1:size(center, 1), 20 | out(k, :) = sqrt(sum(((data-ones(size(data, 1), 1)*center(k, :)).^2)')); 21 | end 22 | else % 1-D data 23 | for k = 1:size(center, 1), 24 | out(k, :) = abs(center(k)-data)'; 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /fcm_s1/uddistfcm_s1.m: -------------------------------------------------------------------------------- 1 | function out = uddistfcm_s1(center, data,dm1,a) 2 | %DISTFCM Distance measure in fuzzy c-mean clustering. 3 | % OUT = DISTFCM(CENTER, DATA) calculates the Euclidean distance 4 | % between each row in CENTER and each row in DATA, and returns a 5 | % distance matrix OUT of size M by N, where M and N are row 6 | % dimensions of CENTER and DATA, respectively, and OUT(I, J) is 7 | % the distance between CENTER(I,:) and DATA(J,:). 8 | % 9 | % See also FCMDEMO, INITFCM, IRISFCM, STEPFCM, and FCM. 10 | 11 | % Roger Jang, 11-22-94, 6-27-95. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | out = zeros(size(center, 1), size(data, 1)); 14 | 15 | % fill the output matrix 16 | 17 | % if size(center, 2) > 1, 18 | % for k = 1:size(center, 1), 19 | % out(k, :) = sqrt(sum(((data-ones(size(data, 1), 1)*center(k, :)).^2)')); 20 | % end 21 | % else % 1-D data 22 | % for k = 1:size(center, 1), 23 | % out(k, :) = abs(center(k)-data)'; 24 | % end 25 | % end 26 | 27 | for k=1:size(center,1), 28 | out(k,:)=((((abs(center(k)-data)).^2)+a*(abs(center(k)-dm1)).^2))'; 29 | end -------------------------------------------------------------------------------- /fcm_s2/uddistfcm_s2.m: -------------------------------------------------------------------------------- 1 | function out = uddistfcm_s2(center, data,dm1,a) 2 | %DISTFCM Distance measure in fuzzy c-mean clustering. 3 | % OUT = DISTFCM(CENTER, DATA) calculates the Euclidean distance 4 | % between each row in CENTER and each row in DATA, and returns a 5 | % distance matrix OUT of size M by N, where M and N are row 6 | % dimensions of CENTER and DATA, respectively, and OUT(I, J) is 7 | % the distance between CENTER(I,:) and DATA(J,:). 8 | % 9 | % See also FCMDEMO, INITFCM, IRISFCM, STEPFCM, and FCM. 10 | 11 | % Roger Jang, 11-22-94, 6-27-95. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | out = zeros(size(center, 1), size(data, 1)); 14 | 15 | % fill the output matrix 16 | 17 | % if size(center, 2) > 1, 18 | % for k = 1:size(center, 1), 19 | % out(k, :) = sqrt(sum(((data-ones(size(data, 1), 1)*center(k, :)).^2)')); 20 | % end 21 | % else % 1-D data 22 | % for k = 1:size(center, 1), 23 | % out(k, :) = abs(center(k)-data)'; 24 | % end 25 | % end 26 | 27 | for k=1:size(center,1), 28 | out(k,:)=((((abs(center(k)-data)).^2)+a*(abs(center(k)-dm1)).^2))'; 29 | end -------------------------------------------------------------------------------- /fcm_m1/test.m: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | clc;clear 16 | d1=imread('wheel.png'); 17 | d2=rgb2gray(d1); 18 | subplot(1,2,1); 19 | imshow(d2); 20 | d3=im2double(d2)*255; 21 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 22 | [center,U,obj_fcn]=udfcm(d4,4); 23 | maxU=max(U); 24 | index1=find(U(1,:)==maxU); 25 | index2=find(U(2,:)==maxU); 26 | index3=find(U(3,:)==maxU); 27 | index4=find(U(4,:)==maxU); 28 | r=round(center); 29 | d5=d4; 30 | d5(index1,1)=r(1); 31 | d5(index2,1)=r(2); 32 | d5(index3,1)=r(3); 33 | d5(index4,1)=r(4); 34 | d6=reshape(d5,size(d3,1),size(d3,2)); 35 | d7=mat2gray(d6,[0 255]); 36 | subplot(1,2,2); 37 | imshow(d7); -------------------------------------------------------------------------------- /Enfcm/test.m: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | clc;clear 16 | tic 17 | d1=imread('wheel.png'); 18 | d2=rgb2gray(d1); 19 | subplot(1,3,1); 20 | imshow(d2); 21 | dnsp=imnoise(d2,'salt & pepper',0.02); 22 | subplot(1,3,2); 23 | imshow(dnsp); 24 | d3=im2double(dnsp); 25 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 26 | [center,U,obj_fcn]=enfcm(d3,4,4.2); 27 | maxU=max(U); 28 | index1=find(U(1,:)==maxU); 29 | index2=find(U(2,:)==maxU); 30 | index3=find(U(3,:)==maxU); 31 | index4=find(U(4,:)==maxU); 32 | r=round(center); 33 | d5=d4; 34 | d5(index1,1)=r(1); 35 | d5(index2,1)=r(2); 36 | d5(index3,1)=r(3); 37 | d5(index4,1)=r(4); 38 | d6=reshape(d5,size(d3,1),size(d3,2)); 39 | d7=mat2gray(d6,[0 255]); 40 | subplot(1,3,3); 41 | imshow(d7); 42 | toc -------------------------------------------------------------------------------- /FGfcm/test.m: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | clc;clear 16 | tic 17 | d1=imread('wheel.png'); 18 | d2=rgb2gray(d1); 19 | subplot(1,3,1); 20 | imshow(d2); 21 | dnsp=imnoise(d2,'salt & pepper',0.02); 22 | subplot(1,3,2); 23 | imshow(dnsp); 24 | d3=im2double(dnsp); 25 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 26 | [center,U,obj_fcn]=fgfcm(d3,4,6.2); 27 | maxU=max(U); 28 | index1=find(U(1,:)==maxU); 29 | index2=find(U(2,:)==maxU); 30 | index3=find(U(3,:)==maxU); 31 | index4=find(U(4,:)==maxU); 32 | r=round(center); 33 | d5=d4; 34 | d5(index1,1)=r(1); 35 | d5(index2,1)=r(2); 36 | d5(index3,1)=r(3); 37 | d5(index4,1)=r(4); 38 | d6=reshape(d5,size(d3,1),size(d3,2)); 39 | d7=mat2gray(d6,[0 255]); 40 | subplot(1,3,3); 41 | imshow(d7); 42 | toc -------------------------------------------------------------------------------- /FGfcm_s1/test.m: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | clc;clear 16 | tic 17 | d1=imread('wheel.png'); 18 | d2=rgb2gray(d1); 19 | subplot(1,3,1); 20 | imshow(d2); 21 | dnsp=imnoise(d2,'salt & pepper',0.02); 22 | subplot(1,3,2); 23 | imshow(dnsp); 24 | d3=im2double(dnsp); 25 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 26 | [center,U,obj_fcn]=fgfcm(d3,4,6.2); 27 | maxU=max(U); 28 | index1=find(U(1,:)==maxU); 29 | index2=find(U(2,:)==maxU); 30 | index3=find(U(3,:)==maxU); 31 | index4=find(U(4,:)==maxU); 32 | r=round(center); 33 | d5=d4; 34 | d5(index1,1)=r(1); 35 | d5(index2,1)=r(2); 36 | d5(index3,1)=r(3); 37 | d5(index4,1)=r(4); 38 | d6=reshape(d5,size(d3,1),size(d3,2)); 39 | d7=mat2gray(d6,[0 255]); 40 | subplot(1,3,3); 41 | imshow(d7); 42 | toc -------------------------------------------------------------------------------- /FGfcm_s2/test.m: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | clc;clear 16 | tic 17 | d1=imread('wheel.png'); 18 | d2=rgb2gray(d1); 19 | subplot(1,3,1); 20 | imshow(d2); 21 | dnsp=imnoise(d2,'salt & pepper',0.02); 22 | subplot(1,3,2); 23 | imshow(dnsp); 24 | d3=im2double(dnsp); 25 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 26 | [center,U,obj_fcn]=fgfcm(d3,4,6.2); 27 | maxU=max(U); 28 | index1=find(U(1,:)==maxU); 29 | index2=find(U(2,:)==maxU); 30 | index3=find(U(3,:)==maxU); 31 | index4=find(U(4,:)==maxU); 32 | r=round(center); 33 | d5=d4; 34 | d5(index1,1)=r(1); 35 | d5(index2,1)=r(2); 36 | d5(index3,1)=r(3); 37 | d5(index4,1)=r(4); 38 | d6=reshape(d5,size(d3,1),size(d3,2)); 39 | d7=mat2gray(d6,[0 255]); 40 | subplot(1,3,3); 41 | imshow(d7); 42 | toc -------------------------------------------------------------------------------- /FLIcm/test.m: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % lin e(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | tic 16 | clc;clear 17 | d1=imread('wheel.png'); 18 | d2=rgb2gray(d1); 19 | subplot(1,3,1); 20 | imshow(d2); 21 | dnsp=imnoise(d2,'salt & pepper',0.02); 22 | subplot(1,3,2); 23 | imshow(dnsp); 24 | d3=im2double(dnsp); 25 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 26 | [center,U,obj_fcn]=flicm(d3,4); 27 | maxU=max(U); 28 | index1=find(U(1,:)==maxU); 29 | index2=find(U(2,:)==maxU); 30 | index3=find(U(3,:)==maxU); 31 | index4=find(U(4,:)==maxU); 32 | r=round(center)*255; 33 | d5=d4; 34 | d5(index1,1)=r(1); 35 | d5(index2,1)=r(2); 36 | d5(index3,1)=r(3); 37 | d5(index4,1)=r(4); 38 | d6=reshape(d5,size(d3,1),size(d3,2)); 39 | d7=mat2gray(d6,[0 255]); 40 | subplot(1,3,3); 41 | imshow(d7); 42 | toc -------------------------------------------------------------------------------- /FLIcm/test.m~: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % lin e(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | tic 16 | clc;clear 17 | d1=imread('wheel.png'); 18 | d2=rgb2gray(d1); 19 | subplot(1,3,1); 20 | imshow(d2); 21 | dnsp=imnoise(d2,'salt & pepper',0.02); 22 | subplot(1,3,2); 23 | imshow(dnsp); 24 | d3=im2double(dnsp); 25 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 26 | [center,U,obj_fcn]=flicm(d3,4); 27 | maxU=max(U); 28 | index1=find(U(1,:)==maxU); 29 | index2=find(U(2,:)==maxU); 30 | index3=find(U(3,:)==maxU); 31 | index4=find(U(4,:)==maxU); 32 | r=round(center)*255; 33 | d5=d4; 34 | d5(index1,1)=r(1); 35 | d5(index2,1)=r(2); 36 | d5(index3,1)=r(3); 37 | d5(index4,1)=r(4); 38 | d6=reshape(d5,size(d3,1),size(d3,2)); 39 | d7=mat2gray(d6,[0 255]); 40 | subplot(1,3,3); 41 | imshow(d7); 42 | toc -------------------------------------------------------------------------------- /fcm_s1/test.m: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | clc;clear 16 | tic 17 | d1=imread('wheel.png'); 18 | d2=rgb2gray(d1); 19 | subplot(1,3,1); 20 | imshow(d2); 21 | dnsp=imnoise(d2,'salt & pepper',0.02); 22 | subplot(1,3,2); 23 | imshow(dnsp); 24 | d3=im2double(dnsp); 25 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 26 | [center,U,obj_fcn]=udfcm_s1(d3,4,4.2); 27 | maxU=max(U); 28 | index1=find(U(1,:)==maxU); 29 | index2=find(U(2,:)==maxU); 30 | index3=find(U(3,:)==maxU); 31 | index4=find(U(4,:)==maxU); 32 | r=round(center*255); 33 | d5=d4; 34 | d5(index1,1)=r(1); 35 | d5(index2,1)=r(2); 36 | d5(index3,1)=r(3); 37 | d5(index4,1)=r(4); 38 | d6=reshape(d5,size(d3,1),size(d3,2)); 39 | d7=mat2gray(d6,[0 255]); 40 | subplot(1,3,3); 41 | imshow(d7); 42 | toc -------------------------------------------------------------------------------- /fcm_s2/test.m: -------------------------------------------------------------------------------- 1 | % data = rand(100,2); 2 | % [center,U,obj_fcn] = udfcm(data,2); 3 | % plot(data(:,1), data(:,2),'o'); 4 | % hold on; 5 | % maxU = max(U); 6 | % % Find the data points with highest grade of membership in cluster 1 7 | % index1 = find(U(1,:) == maxU); 8 | % % Find the data points with highest grade of membership in cluster 2 9 | % index2 = find(U(2,:) == maxU); 10 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 11 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 12 | % % Plot the cluster centers 13 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 14 | % hold off; 15 | clc;clear 16 | tic 17 | d1=imread('wheel.png'); 18 | d2=rgb2gray(d1); 19 | subplot(1,3,1); 20 | imshow(d2); 21 | dnsp=imnoise(d2,'salt & pepper',0.02); 22 | subplot(1,3,2); 23 | imshow(dnsp); 24 | d3=im2double(dnsp); 25 | d4=reshape(d3,size(d3,1)*size(d3,2),1); 26 | [center,U,obj_fcn]=udfcm_s2(d3,4,4.2); 27 | maxU=max(U); 28 | index1=find(U(1,:)==maxU); 29 | index2=find(U(2,:)==maxU); 30 | index3=find(U(3,:)==maxU); 31 | index4=find(U(4,:)==maxU); 32 | r=round(center*255); 33 | d5=d4; 34 | d5(index1,1)=r(1); 35 | d5(index2,1)=r(2); 36 | d5(index3,1)=r(3); 37 | d5(index4,1)=r(4); 38 | d6=reshape(d5,size(d3,1),size(d3,2)); 39 | d7=mat2gray(d6,[0 255]); 40 | subplot(1,3,3); 41 | imshow(d7); 42 | toc -------------------------------------------------------------------------------- /fcm_m1/udstepfcm.m: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = udstepfcm(data, U, cluster_n, expo) 2 | %STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | mf = U.^expo; % MF matrix after exponential modification 24 | center = mf*data./((ones(size(data, 2), 1)*sum(mf'))'); % new center 25 | dist = uddistfcm(center, data); % fill the distance matrix 26 | obj_fcn = sum(sum((dist.^2).*mf)); % objective function 27 | tmp = dist.^(-2/(expo-1)); % calculate new U, suppose expo != 1 28 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); -------------------------------------------------------------------------------- /FLIcm/stepflicm.m: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = stepflicm(data,rc, U, cluster_n, expo) 2 | % STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | mf = U.^expo; % MF matrix after exponential modification 24 | center = mf*data./((ones(size(data, 2), 1)*sum(mf'))'); % new center 25 | [dist1,dist2] = uddistfcm(center, data,rc,U,expo); % fill the distance matrix 26 | obj_fcn = sum(sum((dist1.^2).*mf))+sum(sum(dist2)); % objective function 27 | dist=dist1.^2+dist2; 28 | tmp = dist.^(-1/(expo-1)); % calculate new U, suppose expo != 1 29 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); -------------------------------------------------------------------------------- /FLIcm/stepflicm.m~: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = stepflicm(data,rc, U, cluster_n, expo) 2 | %STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | mf = U.^expo; % MF matrix after exponential modification 24 | center = mf*data./((ones(size(data, 2), 1)*sum(mf'))'); % new center 25 | [dist1,dist2] = uddistfcm(center, data,rc,U,expo); % fill the distance matrix 26 | obj_fcn = sum(sum((dist1.^2).*mf))+sum(sum(dist2)); % objective function 27 | dist=dist1.^2+dist2; 28 | tmp = dist.^(-1/(expo-1)); % calculate new U, suppose expo != 1 29 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); -------------------------------------------------------------------------------- /fcm_s1/obj_mat.m: -------------------------------------------------------------------------------- 1 | function [out1,out2]=obj_mat(center, data,dm1,U,expo) 2 | out1 = zeros(size(center, 1), size(data, 1)); 3 | dm2=reshape(dm1,size(dm1,1)*size(dm1,2),1); 4 | out2=zeros(size(center, 1), size(data, 1)); 5 | for k = 1:size(center, 1), 6 | out1(k, :) = abs(center(k)-data)'; 7 | end 8 | r11=size(center,1); 9 | c11=size(data,1); 10 | for j=1:r11, 11 | for i=1:c11, 12 | neigh=neighbor(dm1,i); 13 | % tic 14 | % if mod(i,1000)==0, 15 | % fprintf('Number of 1000 calulations = %d\n', floor(i/1000)); 16 | % end 17 | % toc 18 | % for k=1:size(neigh,1), 19 | % out2(i,j)=out2(i,j)+U(j,neigh(k))^(expo)*(dm2(neigh(k,1))-center(j))^2; 20 | % end 21 | out2(j,i)=sum(U(j,neigh).^expo.*((dm2(neigh)-center(j)).^2)'); 22 | end 23 | end 24 | 25 | 26 | function out=neighbor(dm1,i) 27 | out=[]; 28 | r=size(dm1,1); 29 | c=size(dm1,2); 30 | %c1=floor(i/r)+1; 31 | r1=mod(i,r); 32 | if(r1==0), 33 | r1=r; 34 | c1=floor(i/r); 35 | else 36 | c1=floor(i/r)+1; 37 | end 38 | temp=[-1 1;-1 0;1 -1;1 0;0 1;0 -1;1 1;-1 -1]; 39 | temp(:,1)=temp(:,1)+r1; 40 | temp(:,2)=temp(:,2)+c1; 41 | if(r1==1 || c1==1 || r1==r || c1==c), %Deal with boundary 42 | nr=find(temp(:,1)==0 | temp(:,1)==r+1); 43 | nc=find(temp(:,2)==0 | temp(:,2)==c+1); 44 | rc=union(nc,nr); 45 | temp(rc,:)=[]; 46 | end 47 | temp(:,2)=temp(:,2)-1; 48 | out=temp*[1;r]; 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /fcm_s2/obj_mat.m: -------------------------------------------------------------------------------- 1 | function [out1,out2]=obj_mat(center, data,dm1,U,expo) 2 | out1 = zeros(size(center, 1), size(data, 1)); 3 | dm2=reshape(dm1,size(dm1,1)*size(dm1,2),1); 4 | out2=zeros(size(center, 1), size(data, 1)); 5 | for k = 1:size(center, 1), 6 | out1(k, :) = abs(center(k)-data)'; 7 | end 8 | r11=size(center,1); 9 | c11=size(data,1); 10 | for j=1:r11, 11 | for i=1:c11, 12 | neigh=neighbor(dm1,i); 13 | % tic 14 | % if mod(i,1000)==0, 15 | % fprintf('Number of 1000 calulations = %d\n', floor(i/1000)); 16 | % end 17 | % toc 18 | % for k=1:size(neigh,1), 19 | % out2(i,j)=out2(i,j)+U(j,neigh(k))^(expo)*(dm2(neigh(k,1))-center(j))^2; 20 | % end 21 | out2(j,i)=sum(U(j,neigh).^expo.*((dm2(neigh)-center(j)).^2)'); 22 | end 23 | end 24 | 25 | 26 | function out=neighbor(dm1,i) 27 | out=[]; 28 | r=size(dm1,1); 29 | c=size(dm1,2); 30 | %c1=floor(i/r)+1; 31 | r1=mod(i,r); 32 | if(r1==0), 33 | r1=r; 34 | c1=floor(i/r); 35 | else 36 | c1=floor(i/r)+1; 37 | end 38 | temp=[-1 1;-1 0;1 -1;1 0;0 1;0 -1;1 1;-1 -1]; 39 | temp(:,1)=temp(:,1)+r1; 40 | temp(:,2)=temp(:,2)+c1; 41 | if(r1==1 || c1==1 || r1==r || c1==c), %Deal with boundary 42 | nr=find(temp(:,1)==0 | temp(:,1)==r+1); 43 | nc=find(temp(:,2)==0 | temp(:,2)==c+1); 44 | rc=union(nc,nr); 45 | temp(rc,:)=[]; 46 | end 47 | temp(:,2)=temp(:,2)-1; 48 | out=temp*[1;r]; 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /fcm_s1/udstepfcm_s1.m: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = udstepfcm_s1(data,data_mean, U, cluster_n,a,expo) 2 | %STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | mf = U.^expo; % MF matrix after exponential modification 24 | dm1=reshape(data_mean,size(data_mean,1)*size(data_mean,2),1); 25 | %dm1=data_mean; 26 | data1=data+a*dm1; 27 | center = mf*data1./((ones(size(data1, 2), 1)*sum(mf'))'*(1+a)); % new center 28 | [t1,t2]=obj_mat(center, data,data_mean,U,expo); 29 | obj_fcn = sum(sum((t1.^2).*mf))+a*sum(sum(t2)); % objective function 30 | 31 | dist = uddistfcm_s1(center, data,dm1,a); % fill the distance matrix 32 | tmp = dist.^(-1/(expo-1)); % calculate new U, suppose expo != 1 33 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); 34 | -------------------------------------------------------------------------------- /fcm_s2/udstepfcm_s2.m: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = udstepfcm_s2(data,data_mean, U, cluster_n,a,expo) 2 | %STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | mf = U.^expo; % MF matrix after exponential modification 24 | dm1=reshape(data_mean,size(data_mean,1)*size(data_mean,2),1); 25 | %dm1=data_mean; 26 | data1=data+a*dm1; 27 | center = mf*data1./((ones(size(data1, 2), 1)*sum(mf'))'*(1+a)); % new center 28 | [t1,t2]=obj_mat(center, data,data_mean,U,expo); 29 | obj_fcn = sum(sum((t1.^2).*mf))+a*sum(sum(t2)); % objective function 30 | 31 | dist = uddistfcm_s2(center, data,dm1,a); % fill the distance matrix 32 | tmp = dist.^(-1/(expo-1)); % calculate new U, suppose expo != 1 33 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); 34 | -------------------------------------------------------------------------------- /Enfcm/stepenfcm.m: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = stepenfcm(data_N,N, U, cluster_n,expo) 2 | %STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | % mf = U.^expo; % MF matrix after exponential modification 24 | % dm1=reshape(data_mean,size(data_mean,1)*size(data_mean,2),1); 25 | % %dm1=data_mean; 26 | % data1=data+a*dm1; 27 | % center = mf*data1./((ones(size(data1, 2), 1)*sum(mf'))'*(1+a)); % new center 28 | % [t1,t2]=obj_mat(center, data,data_mean,U,expo); 29 | % obj_fcn = sum(sum((t1.^2).*mf))+a*sum(sum(t2)); % objective function 30 | % 31 | % dist = uddistfcm_s2(center, data,dm1,a); % fill the distance matrix 32 | % tmp = dist.^(-1/(expo-1)); % calculate new U, suppose expo != 1 33 | % U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); 34 | mf=U.^expo; 35 | n=size(N,1); 36 | mf1= mf.*(ones(cluster_n,1)*N'); 37 | center=mf1*data_N./(sum(mf1'))'; 38 | dist=dist_enfcm(center,data_N); 39 | obj_fcn = sum(sum((dist.^2).*mf1)); 40 | tmp = dist.^(-2/(expo-1)); 41 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); -------------------------------------------------------------------------------- /FGfcm/stepfgfcm.m: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = stepenfcm(data_N,N, U, cluster_n,expo) 2 | %STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | % mf = U.^expo; % MF matrix after exponential modification 24 | % dm1=reshape(data_mean,size(data_mean,1)*size(data_mean,2),1); 25 | % %dm1=data_mean; 26 | % data1=data+a*dm1; 27 | % center = mf*data1./((ones(size(data1, 2), 1)*sum(mf'))'*(1+a)); % new center 28 | % [t1,t2]=obj_mat(center, data,data_mean,U,expo); 29 | % obj_fcn = sum(sum((t1.^2).*mf))+a*sum(sum(t2)); % objective function 30 | % 31 | % dist = uddistfcm_s2(center, data,dm1,a); % fill the distance matrix 32 | % tmp = dist.^(-1/(expo-1)); % calculate new U, suppose expo != 1 33 | % U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); 34 | mf=U.^expo; 35 | n=size(N,1); 36 | mf1= mf.*(ones(cluster_n,1)*N'); 37 | center=mf1*data_N./(sum(mf1'))'; 38 | dist=dist_fgfcm(center,data_N); 39 | obj_fcn = sum(sum((dist.^2).*mf1)); 40 | tmp = dist.^(-2/(expo-1)); 41 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); -------------------------------------------------------------------------------- /FGfcm_s1/stepfgfcm.m: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = stepenfcm(data_N,N, U, cluster_n,expo) 2 | %STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | % mf = U.^expo; % MF matrix after exponential modification 24 | % dm1=reshape(data_mean,size(data_mean,1)*size(data_mean,2),1); 25 | % %dm1=data_mean; 26 | % data1=data+a*dm1; 27 | % center = mf*data1./((ones(size(data1, 2), 1)*sum(mf'))'*(1+a)); % new center 28 | % [t1,t2]=obj_mat(center, data,data_mean,U,expo); 29 | % obj_fcn = sum(sum((t1.^2).*mf))+a*sum(sum(t2)); % objective function 30 | % 31 | % dist = uddistfcm_s2(center, data,dm1,a); % fill the distance matrix 32 | % tmp = dist.^(-1/(expo-1)); % calculate new U, suppose expo != 1 33 | % U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); 34 | mf=U.^expo; 35 | n=size(N,1); 36 | mf1= mf.*(ones(cluster_n,1)*N'); 37 | center=mf1*data_N./(sum(mf1'))'; 38 | dist=dist_fgfcm(center,data_N); 39 | obj_fcn = sum(sum((dist.^2).*mf1)); 40 | tmp = dist.^(-2/(expo-1)); 41 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); -------------------------------------------------------------------------------- /FGfcm_s2/stepfgfcm.m: -------------------------------------------------------------------------------- 1 | function [U_new, center, obj_fcn] = stepenfcm(data_N,N, U, cluster_n,expo) 2 | %STEPFCM One step in fuzzy c-mean clustering. 3 | % [U_NEW, CENTER, ERR] = STEPFCM(DATA, U, CLUSTER_N, EXPO) 4 | % performs one iteration of fuzzy c-mean clustering, where 5 | % 6 | % DATA: matrix of data to be clustered. (Each row is a data point.) 7 | % U: partition matrix. (U(i,j) is the MF value of data j in cluster j.) 8 | % CLUSTER_N: number of clusters. 9 | % EXPO: exponent (> 1) for the partition matrix. 10 | % U_NEW: new partition matrix. 11 | % CENTER: center of clusters. (Each row is a center.) 12 | % ERR: objective function for partition U. 13 | % 14 | % Note that the situation of "singularity" (one of the data points is 15 | % exactly the same as one of the cluster centers) is not checked. 16 | % However, it hardly occurs in practice. 17 | % 18 | % See also DISTFCM, INITFCM, IRISFCM, FCMDEMO, FCM. 19 | 20 | % Roger Jang, 11-22-94. 21 | % Copyright 1994-2002 The MathWorks, Inc. 22 | 23 | % mf = U.^expo; % MF matrix after exponential modification 24 | % dm1=reshape(data_mean,size(data_mean,1)*size(data_mean,2),1); 25 | % %dm1=data_mean; 26 | % data1=data+a*dm1; 27 | % center = mf*data1./((ones(size(data1, 2), 1)*sum(mf'))'*(1+a)); % new center 28 | % [t1,t2]=obj_mat(center, data,data_mean,U,expo); 29 | % obj_fcn = sum(sum((t1.^2).*mf))+a*sum(sum(t2)); % objective function 30 | % 31 | % dist = uddistfcm_s2(center, data,dm1,a); % fill the distance matrix 32 | % tmp = dist.^(-1/(expo-1)); % calculate new U, suppose expo != 1 33 | % U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); 34 | mf=U.^expo; 35 | n=size(N,1); 36 | mf1= mf.*(ones(cluster_n,1)*N'); 37 | center=mf1*data_N./(sum(mf1'))'; 38 | dist=dist_fgfcm(center,data_N); 39 | obj_fcn = sum(sum((dist.^2).*mf1)); 40 | tmp = dist.^(-2/(expo-1)); 41 | U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); -------------------------------------------------------------------------------- /FLIcm/uddistfcm.m: -------------------------------------------------------------------------------- 1 | function [out1,out2] = uddistfcm(center, data,rc,U,expo) 2 | %DISTFCM Distance measure in fuzzy c-mean clustering. 3 | % OUT = DISTFCM(CENTER, DATA) calculates the Euclidean distance 4 | % between each row in CENTER and each row in DATA, and returns a 5 | % distance matrix OUT of size M by N, where M and N are row 6 | % dimensions of CENTER and DATA, respectively, and OUT(I, J) is 7 | % the distance between CENTER(I,:) and DATA(J,:). 8 | % 9 | % See also FCMDEMO, INITFCM, IRISFCM, STEPFCM, and FCM. 10 | 11 | % Roger Jang, 11-22-94, 6-27-95. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | out1 = zeros(size(center, 1), size(data, 1)); 14 | out2 = zeros(size(center, 1), size(data, 1)); 15 | % fill the output matrix 16 | for k = 1:size(center, 1), 17 | out1(k, :) = abs(center(k)-data)'; 18 | end 19 | for k=1:size(center,1), 20 | for i=1:size(data,1), 21 | [neigh1,neigh2,ct]=neighbor(rc,i); 22 | neigh1=neigh1-ones(size(neigh1,1),1)*ct; 23 | d=(sqrt(sum((neigh1.^2)'))+1).^(-1); 24 | u=(1-U(k,neigh2)).^expo; 25 | xv=(data(neigh2,1)-center(k)).^2; 26 | out2(k,i)=(d.*u)*xv; 27 | end 28 | end 29 | end 30 | 31 | function [out1,out2,out3]=neighbor(rc,i) 32 | out=[]; 33 | r=rc(1); 34 | c=rc(2); 35 | %c1=floor(i/r)+1; 36 | r1=mod(i,r); 37 | if(r1==0), 38 | r1=r; 39 | c1=floor(i/r); 40 | else 41 | c1=floor(i/r)+1; 42 | end 43 | temp=[-1 1;-1 0;1 -1;1 0;0 1;0 -1;1 1;-1 -1]; 44 | temp(:,1)=temp(:,1)+r1; 45 | temp(:,2)=temp(:,2)+c1; 46 | if(r1==1 || c1==1 || r1==r || c1==c), %Deal with boundary 47 | nr=find(temp(:,1)==0 | temp(:,1)==r+1); 48 | nc=find(temp(:,2)==0 | temp(:,2)==c+1); 49 | rc=union(nc,nr); 50 | temp(rc,:)=[]; 51 | end 52 | out1=temp; 53 | temp(:,2)=temp(:,2)-1; 54 | out2=temp*[1;r]; 55 | out3=[r1 c1]; 56 | end -------------------------------------------------------------------------------- /FLIcm/uddistfcm.m~: -------------------------------------------------------------------------------- 1 | function [out1,out2] = uddistfcm(center, data,rc,U,expo) 2 | %DISTFCM Distance measure in fuzzy c-mean clustering. 3 | % OUT = DISTFCM(CENTER, DATA) calculates the Euclidean distance 4 | % between each row in CENTER and each row in DATA, and returns a 5 | % distance matrix OUT of size M by N, where M and N are row 6 | % dimensions of CENTER and DATA, respectively, and OUT(I, J) is 7 | % the distance between CENTER(I,:) and DATA(J,:). 8 | % 9 | % See also FCMDEMO, INITFCM, IRISFCM, STEPFCM, and FCM. 10 | 11 | % Roger Jang, 11-22-94, 6-27-95. 12 | % Copyright 1994-2002 The MathWorks, Inc. 13 | out1 = zeros(size(center, 1), size(data, 1)); 14 | out2 = zeros(size(center, 1), size(data, 1)); 15 | % fill the output matrix 16 | for k = 1:size(center, 1), 17 | out1(k, :) = abs(center(k)-data)'; 18 | end 19 | for k=1:size(center,1), 20 | for i=1:size(data,1), 21 | [neigh1,neigh2,ct]=neighbor(rc,i); 22 | neigh1=neigh1-ones(size(neigh1,1),1)*ct; 23 | d=((sqrt(sum((neigh1.^2)'))+1).^(-1))'; 24 | u=((1-U(k,neigh2)).^expo)'; 25 | xv=(data(neigh2,1)-center(k)).^2; 26 | out2(k,i)=(d.*u)'*xv; 27 | end 28 | end 29 | end 30 | 31 | function [out1,out2,out3]=neighbor(rc,i) 32 | out=[]; 33 | r=rc(1); 34 | c=rc(2); 35 | %c1=floor(i/r)+1; 36 | r1=mod(i,r); 37 | if(r1==0), 38 | r1=r; 39 | c1=floor(i/r); 40 | else 41 | c1=floor(i/r)+1; 42 | end 43 | temp=[-1 1;-1 0;1 -1;1 0;0 1;0 -1;1 1;-1 -1]; 44 | temp(:,1)=temp(:,1)+r1; 45 | temp(:,2)=temp(:,2)+c1; 46 | if(r1==1 || c1==1 || r1==r || c1==c), %Deal with boundary 47 | nr=find(temp(:,1)==0 | temp(:,1)==r+1); 48 | nc=find(temp(:,2)==0 | temp(:,2)==c+1); 49 | rc=union(nc,nr); 50 | temp(rc,:)=[]; 51 | end 52 | out1=temp; 53 | temp(:,2)=temp(:,2)-1; 54 | out2=temp*[1;r]; 55 | out3=[r1 c1]; 56 | end -------------------------------------------------------------------------------- /fcm_s2/udfcm_s2.m: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = udfcm_s2(data, cluster_n,a,options) 2 | % 3 | data1=data; 4 | data=[]; 5 | data=reshape(data1,size(data1,1)*size(data1,2),1); 6 | data2=padmatrix(data1,1); 7 | for i=2:size(data1,1)+1, 8 | for j=2:size(data1,2)+1, 9 | % if(i==1 || j==1 ||i==size(data1,1) || j==size(data1,2)) 10 | % data_mean(i,j)=data1(i,j); 11 | % continue; 12 | % end 13 | data_mean(i-1,j-1)=dm(i,j,data2); 14 | end 15 | end 16 | 17 | if nargin ~= 2 && nargin ~= 3 && nargin ~=4, 18 | error('Too many or too few input arguments!'); 19 | end 20 | 21 | data_n = size(data, 1); 22 | in_n = size(data, 2); 23 | 24 | % Change the following to set default options 25 | default_options = [2; % exponent for the partition matrix U 26 | 100; % max. number of iteration 27 | 1e-5; % min. amount of improvement 28 | 1]; % info display during iteration 29 | 30 | if nargin == 2, 31 | options = default_options; 32 | a=4.2; %control the effects of the neighbors term 33 | elseif nargin==3, 34 | options = default_options; 35 | else 36 | % If "options" is not fully specified, pad it with default values. 37 | if length(options) < 5, 38 | tmp = default_options; 39 | tmp(1:length(options)) = options; 40 | options = tmp; 41 | end 42 | % If some entries of "options" are nan's, replace them with defaults. 43 | nan_index = find(isnan(options)==1); 44 | options(nan_index) = default_options(nan_index); 45 | if options(1) <= 1, 46 | error('The exponent should be greater than 1!'); 47 | end 48 | end 49 | 50 | expo = options(1); % Exponent for U 51 | max_iter = options(2); % Max. iteration 52 | min_impro = options(3); % Min. improvement 53 | display = options(4); % Display info or not 54 | 55 | obj_fcn = zeros(max_iter, 1); % Array for objective function 56 | 57 | U = udinitfcm_s2(cluster_n, data_n); % Initial fuzzy partition 58 | % Main loop 59 | for i = 1:max_iter, 60 | [U, center, obj_fcn(i)] = udstepfcm_s2(data,data_mean, U, cluster_n,a,expo); 61 | if display, 62 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 63 | end 64 | % check termination condition 65 | if i > 1, 66 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 67 | end 68 | end 69 | 70 | iter_n = i; % Actual number of iterations 71 | obj_fcn(iter_n+1:max_iter) = []; 72 | 73 | 74 | function out=dm(x,y,d) 75 | temp=[d(x-1,y) d(x-1,y-1) d(x-1,y+1) d(x+1,y) d(x+1,y-1) d(x+1,y+1) d(x,y-1) d(x,y+1)]; 76 | out=median(temp); 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /FLIcm/flicm.m: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = flicm(data, cluster_n, options) 2 | %FCM Data set clustering using fuzzy c-means clustering. 3 | % 4 | % [CENTER, U, OBJ_FCN] = FCM(DATA, N_CLUSTER) finds N_CLUSTER number of 5 | % clusters in the data set DATA. DATA is size M-by-N, where M is the number of 6 | % data points and N is the number of coordinates for each data point. The 7 | % coordinates for each cluster center are returned in the rows of the matrix 8 | % CENTER. The membership function matrix U contains the grade of membership of 9 | % each DATA point in each cluster. The values 0 and 1 indicate no membership 10 | % and full membership respectively. Grades between 0 and 1 indicate that the 11 | % data point has partial membership in a cluster. At each iteration, an 12 | % objective function is minimized to find the best location for the clusters 13 | % and its values are returned in OBJ_FCN. 14 | % 15 | % [CENTER, ...] = FCM(DATA,N_CLUSTER,OPTIONS) specifies a vector of options 16 | % for the clustering process: 17 | % OPTIONS(1): exponent for the matrix U (default: 2.0)ri li 18 | % OPTIONS(2): maximum number of iterations (default: 100) 19 | % OPTIONS(3): minimum amount of improvement (default: 1e-5) 20 | % OPTIONS(4): info display during iteration (default: 1) 21 | % The clustering process stops when the maximum number of iterations 22 | % is reached, or when the objective function improvement between two 23 | % consecutive iterations is less than the minimum amount of improvement 24 | % specified. Use NaN to select the default value. 25 | 26 | data1=data; %Preprocess data 27 | data=[]; 28 | data=reshape(data1,size(data1,1)*size(data1,2),1); 29 | data2=padmatrix(data1,1); %Preprocess data 30 | rc=[size(data1,1) size(data1,2)]; 31 | if nargin ~= 2 & nargin ~= 3, 32 | error('Too many or too few input arguments!'); 33 | end 34 | 35 | data_n = size(data, 1); 36 | in_n = size(data, 2); 37 | 38 | % Change the following to set default options 39 | default_options = [2; % exponent for the partition matrix U 40 | 100; % max. number of iteration 41 | 1e-5; % min. amount of improvement 42 | 1]; % info display during iteration 43 | 44 | if nargin == 2, 45 | options = default_options; 46 | else 47 | % If "options" is not fully specified, pad it with default values. 48 | if length(options) < 4, 49 | tmp = default_options; 50 | tmp(1:length(options)) = options; 51 | options = tmp; 52 | end 53 | % If some entries of "options" are nan's, replace them with defaults. 54 | nan_index = find(isnan(options)==1); 55 | options(nan_index) = default_options(nan_index); 56 | if options(1) <= 1, 57 | error('The exponent should be greater than 1!'); 58 | end 59 | end 60 | 61 | expo = options(1); % Exponent for U 62 | max_iter = options(2); % Max. iteration 63 | min_impro = options(3); % Min. improvement 64 | display = options(4); % Display info or not 65 | 66 | obj_fcn = zeros(max_iter, 1); % Array for objective function 67 | 68 | U = udinitfcm(cluster_n, data_n); % Initial fuzzy partition 69 | % Main loop 70 | for i = 1:max_iter, 71 | [U, center, obj_fcn(i)] = stepflicm(data,rc, U, cluster_n, expo); 72 | if display, 73 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 74 | end 75 | % check termination condition 76 | if i > 1, 77 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 78 | end 79 | end 80 | 81 | iter_n = i; % Actual number of iterations 82 | obj_fcn(iter_n+1:max_iter) = []; -------------------------------------------------------------------------------- /FLIcm/flicm.m~: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = flicm(data, cluster_n, options) 2 | %FCM Data set clustering using fuzzy c-means clustering. 3 | % 4 | % [CENTER, U, OBJ_FCN] = FCM(DATA, N_CLUSTER) finds N_CLUSTER number of 5 | % clusters in the data set DATA. DATA is size M-by-N, where M is the number of 6 | % data points and N is the number of coordinates for each data point. The 7 | % coordinates for each cluster center are returned in the rows of the matrix 8 | % CENTER. The membership function matrix U contains the grade of membership of 9 | % each DATA point in each cluster. The values 0 and 1 indicate no membership 10 | % and full membership respectively. Grades between 0 and 1 indicate that the 11 | % data point has partial membership in a cluster. At each iteration, an 12 | % objective function is minimized to find the best location for the clusters 13 | % and its values are returned in OBJ_FCN. 14 | % 15 | % [CENTER, ...] = FCM(DATA,N_CLUSTER,OPTIONS) specifies a vector of options 16 | % for the clustering process: 17 | % OPTIONS(1): exponent for the matrix U (default: 2.0)ri li 18 | % OPTIONS(2): maximum number of iterations (default: 100) 19 | % OPTIONS(3): minimum amount of improvement (default: 1e-5) 20 | % OPTIONS(4): info display during iteration (default: 1) 21 | % The clustering process stops when the maximum number of iterations 22 | % is reached, or when the objective function improvement between two 23 | % consecutive iterations is less than the minimum amount of improvement 24 | % specified. Use NaN to select the default value. 25 | 26 | data1=data; %Preprocess data 27 | data=[]; 28 | data=reshape(data1,size(data1,1)*size(data1,2),1); 29 | data2=padmatrix(data1,1); %Preprocess data 30 | rc=[size(data1,1) size(data1,2)]; 31 | if nargin ~= 2 & nargin ~= 3, 32 | error('Too many or too few input arguments!'); 33 | end 34 | 35 | data_n = size(data, 1); 36 | in_n = size(data, 2); 37 | 38 | % Change the following to set default options 39 | default_options = [2; % exponent for the partition matrix U 40 | 100; % max. number of iteration 41 | 1e-5; % min. amount of improvement 42 | 1]; % info display during iteration 43 | 44 | if nargin == 2, 45 | options = default_options; 46 | else 47 | % If "options" is not fully specified, pad it with default values. 48 | if length(options) < 4, 49 | tmp = default_options; 50 | tmp(1:length(options)) = options; 51 | options = tmp; 52 | end 53 | % If some entries of "options" are nan's, replace them with defaults. 54 | nan_index = find(isnan(options)==1); 55 | options(nan_index) = default_options(nan_index); 56 | if options(1) <= 1, 57 | error('The exponent should be greater than 1!'); 58 | end 59 | end 60 | 61 | expo = options(1); % Exponent for U 62 | max_iter = options(2); % Max. iteration 63 | min_impro = options(3); % Min. improvement 64 | display = options(4); % Display info or not 65 | 66 | obj_fcn = zeros(max_iter, 1); % Array for objective function 67 | 68 | U = udinitfcm(cluster_n, data_n); % Initial fuzzy partition 69 | % Main loop 70 | for i = 1:max_iter, 71 | [U, center, obj_fcn(i)] = stepflicm(data,rc, U, cluster_n, expo); 72 | if display, 73 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 74 | end 75 | % check termination condition 76 | if i > 1, 77 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 78 | end 79 | end 80 | 81 | iter_n = i; % Actual number of iterations 82 | obj_fcn(iter_n+1:max_iter) = []; -------------------------------------------------------------------------------- /Enfcm/enfcm.m: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = enfcm(data, cluster_n,a,options) 2 | % Use a 3*3 window 3 | data1=data; %Preprocess data 4 | data=[]; 5 | data=reshape(data1,size(data1,1)*size(data1,2),1); 6 | data2=padmatrix(data1,1); %Preprocess data 7 | % for i=2:size(data1,1)+1, 8 | % for j=2:size(data1,2)+1, 9 | % % if(i==1 || j==1 ||i==size(data1,1) || j==size(data1,2)) 10 | % % data_mean(i,j)=data1(i,j); 11 | % % continue; 12 | % % end 13 | % data_mean(i-1,j-1)=dm(i,j,data2); 14 | % end 15 | % end 16 | 17 | if nargin ~= 2 && nargin ~= 3 && nargin ~=4, 18 | error('Too many or too few input arguments!'); 19 | end 20 | % 21 | data_n = size(data, 1); 22 | % in_n = size(data, 2); 23 | 24 | % Change the following to set default options 25 | default_options = [2; % exponent for the partition matrix U 26 | 100; % max. number of iteration 27 | 1e-5; % min. amount of improvement 28 | 1]; % info display during iteration 29 | 30 | if nargin == 2, 31 | options = default_options; 32 | a=4.2; %control the effects of the neighbors term 33 | elseif nargin==3, 34 | options = default_options; 35 | else 36 | % If "options" is not fully specified, pad it with default values. 37 | if length(options) < 5, 38 | tmp = default_options; 39 | tmp(1:length(options)) = options; 40 | options = tmp; 41 | end 42 | % If some entries of "options" are nan's, replace them with defaults. 43 | nan_index = find(isnan(options)==1); 44 | options(nan_index) = default_options(nan_index); 45 | if options(1) <= 1, 46 | error('The exponent should be greater than 1!'); 47 | end 48 | end 49 | 50 | expo = options(1); % Exponent for U 51 | max_iter = options(2); % Max. iteration 52 | min_impro = options(3); % Min. improvement 53 | display = options(4); % Display info or not 54 | 55 | bitnum=8; 56 | gln=2^bitnum; 57 | obj_fcn = zeros(max_iter, 1); % Array for objective function 58 | lnai=zeros(data_n,1); % Compute the local neighbor average image from original image 59 | rc=[size(data1,1) size(data1,2)]; 60 | 61 | for i=1:data_n, 62 | lnai(i)=round(lnai_fun(i,data2,rc,a)*(gln-1)); 63 | end % Compute the local neighbor average image from original image 64 | 65 | edges=linspace(0,2^bitnum,2^bitnum+1); % Compute the histogram of the processed images,8 bit image 66 | [N,~,bin]=histcounts(lnai,edges); 67 | N=N'; 68 | data_N=(linspace(0,2^bitnum-1,2^bitnum))';% Compute the histogram of the processed images,8 bit image 69 | 70 | 71 | data_n=size(N,1); 72 | U = enfcm_init(cluster_n, data_n); % Initial fuzzy partition 73 | % Main loop 74 | for i = 1:max_iter, 75 | [U, center, obj_fcn(i)] = stepenfcm(data_N,N, U, cluster_n,expo); 76 | if display, 77 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 78 | end 79 | % check termination condition 80 | if i > 1, 81 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 82 | end 83 | end 84 | 85 | iter_n = i; % Actual number of iterations 86 | obj_fcn(iter_n+1:max_iter) = []; 87 | U1=U; 88 | U=[]; 89 | U=U1(:,bin); 90 | 91 | 92 | 93 | function out=lnai_fun(i,data2,rc,a) 94 | data=reshape(data2,size(data2,1)*size(data2,2),1); 95 | neigh=neighbor(rc,i); 96 | out=(data(i)+a*sum(data(neigh))/8)/(1+a); 97 | 98 | 99 | function out=neighbor(rc,i) 100 | out=[]; 101 | r=rc(1); 102 | c=rc(2); 103 | %c1=floor(i/r)+1; 104 | r1=mod(i,r); 105 | if(r1==0), 106 | r1=r; 107 | c1=floor(i/r); 108 | else 109 | c1=floor(i/r)+1; 110 | end 111 | temp=[-1 1;-1 0;1 -1;1 0;0 1;0 -1;1 1;-1 -1]; 112 | temp(:,1)=temp(:,1)+r1+1; 113 | temp(:,2)=temp(:,2)+c1+1; 114 | % if(r1==1 || c1==1 || r1==r || c1==c), %Deal with boundary 115 | % nr=find(temp(:,1)==0 | temp(:,1)==r+1); 116 | % nc=find(temp(:,2)==0 | temp(:,2)==c+1); 117 | % rc=union(nc,nr); 118 | % temp(rc,:)=[]; 119 | % end 120 | temp(:,2)=temp(:,2)-1; 121 | out=temp*[1;r+2]; 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /fcm_m1/udfcm.m: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = udfcm(data, cluster_n, options) 2 | %FCM Data set clustering using fuzzy c-means clustering. 3 | % 4 | % [CENTER, U, OBJ_FCN] = FCM(DATA, N_CLUSTER) finds N_CLUSTER number of 5 | % clusters in the data set DATA. DATA is size M-by-N, where M is the number of 6 | % data points and N is the number of coordinates for each data point. The 7 | % coordinates for each cluster center are returned in the rows of the matrix 8 | % CENTER. The membership function matrix U contains the grade of membership of 9 | % each DATA point in each cluster. The values 0 and 1 indicate no membership 10 | % and full membership respectively. Grades between 0 and 1 indicate that the 11 | % data point has partial membership in a cluster. At each iteration, an 12 | % objective function is minimized to find the best location for the clusters 13 | % and its values are returned in OBJ_FCN. 14 | % 15 | % [CENTER, ...] = FCM(DATA,N_CLUSTER,OPTIONS) specifies a vector of options 16 | % for the clustering process: 17 | % OPTIONS(1): exponent for the matrix U (default: 2.0)ri li 18 | % OPTIONS(2): maximum number of iterations (default: 100) 19 | % OPTIONS(3): minimum amount of improvement (default: 1e-5) 20 | % OPTIONS(4): info display during iteration (default: 1) 21 | % The clustering process stops when the maximum number of iterations 22 | % is reached, or when the objective function improvement between two 23 | % consecutive iterations is less than the minimum amount of improvement 24 | % specified. Use NaN to select the default value. 25 | % 26 | % Example 27 | % data = rand(100,2); 28 | % [center,U,obj_fcn] = fcm(data,2); 29 | % plot(data(:,1), data(:,2),'o');ri li 30 | % hold on; 31 | % maxU = max(U); 32 | % % Find the data points with highest grade of membership in cluster 1 33 | % index1 = find(U(1,:) == maxU); 34 | % % Find the data points with highest grade of membership in cluster 2 35 | % index2 = find(U(2,:) == maxU); 36 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 37 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 38 | % % Plot the cluster centers 39 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 40 | % hold off; 41 | % 42 | % See also FCMDEMO, INITFCM, IRISFCM, DISTFCM, STEPFCM. 43 | 44 | % Roger Jang, 12-13-94, N. Hickey 04-16-01 45 | % Copyright 1994-2002 The MathWorks, Inc. 46 | 47 | if nargin ~= 2 & nargin ~= 3, 48 | error('Too many or too few input arguments!'); 49 | end 50 | 51 | data_n = size(data, 1); 52 | in_n = size(data, 2); 53 | 54 | % Change the following to set default options 55 | default_options = [2; % exponent for the partition matrix U 56 | 100; % max. number of iteration 57 | 1e-5; % min. amount of improvement 58 | 1]; % info display during iteration 59 | 60 | if nargin == 2, 61 | options = default_options; 62 | else 63 | % If "options" is not fully specified, pad it with default values. 64 | if length(options) < 4, 65 | tmp = default_options; 66 | tmp(1:length(options)) = options; 67 | options = tmp; 68 | end 69 | % If some entries of "options" are nan's, replace them with defaults. 70 | nan_index = find(isnan(options)==1); 71 | options(nan_index) = default_options(nan_index); 72 | if options(1) <= 1, 73 | error('The exponent should be greater than 1!'); 74 | end 75 | end 76 | 77 | expo = options(1); % Exponent for U 78 | max_iter = options(2); % Max. iteration 79 | min_impro = options(3); % Min. improvement 80 | display = options(4); % Display info or not 81 | 82 | obj_fcn = zeros(max_iter, 1); % Array for objective function 83 | 84 | U = udinitfcm(cluster_n, data_n); % Initial fuzzy partition 85 | % Main loop 86 | for i = 1:max_iter, 87 | [U, center, obj_fcn(i)] = udstepfcm(data, U, cluster_n, expo); 88 | if display, 89 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 90 | end 91 | % check termination condition 92 | if i > 1, 93 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 94 | end 95 | end 96 | 97 | iter_n = i; % Actual number of iterations 98 | obj_fcn(iter_n+1:max_iter) = []; -------------------------------------------------------------------------------- /FGfcm_s1/fgfcm.m: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = fgfcm(data, cluster_n,rg,options) 2 | % Use a 3*3 window 3 | data1=data; %Preprocess data 4 | data=[]; 5 | data=reshape(data1,size(data1,1)*size(data1,2),1); 6 | data2=padmatrix(data1,1); %Preprocess data 7 | % for i=2:size(data1,1)+1, 8 | % for j=2:size(data1,2)+1, 9 | % % if(i==1 || j==1 ||i==size(data1,1) || j==size(data1,2)) 10 | % % data_mean(i,j)=data1(i,j); 11 | % % continue; 12 | % % end 13 | % data_mean(i-1,j-1)=dm(i,j,data2); 14 | % end 15 | % end 16 | 17 | if nargin ~= 2 && nargin ~= 3 && nargin ~=4, 18 | error('Too many or too few input arguments!'); 19 | end 20 | % 21 | data_n = size(data, 1); 22 | % in_n = size(data, 2); 23 | 24 | % Change the following to set default options 25 | default_options = [2; % exponent for the partition matrix U 26 | 100; % max. number of iteration 27 | 1e-5; % min. amount of improvement 28 | 1; % info display during iteration 29 | 3;]; % scale factor 30 | 31 | if nargin == 2, 32 | options = default_options; 33 | rg=6.0; %control the effects of the neighbors term 34 | elseif nargin==3, 35 | options = default_options; 36 | else 37 | % If "options" is not fully specified, pad it with default values. 38 | if length(options) < 6, 39 | tmp = default_options; 40 | tmp(1:length(options)) = options; 41 | options = tmp; 42 | end 43 | % If some entries of "options" are nan's, replace them with defaults. 44 | nan_index = find(isnan(options)==1); 45 | options(nan_index) = default_options(nan_index); 46 | if options(1) <= 1, 47 | error('The exponent should be greater than 1!'); 48 | end 49 | end 50 | 51 | expo = options(1); % Exponent for U 52 | max_iter = options(2); % Max. iteration 53 | min_impro = options(3); % Min. improvement 54 | display = options(4); % Display info or not 55 | rs=options(5); 56 | bitnum=8; % 8bit image 57 | gln=2^bitnum; 58 | obj_fcn = zeros(max_iter, 1); % Array for objective function 59 | lnai=zeros(data_n,1); % Compute the local neighbor weighted image from original image 60 | rc=[size(data1,1) size(data1,2)]; 61 | 62 | for i=1:data_n, 63 | lnwi(i)=round(lnwi_fun(i,data2,rc,rs,rg)*(gln-1)); 64 | end % Compute the local neighbor weighted image from original image 65 | 66 | edges=linspace(0,2^bitnum,2^bitnum+1); % Compute the histogram of the processed images,8 bit image 67 | [N,~,bin]=histcounts(lnwi,edges); 68 | N=N'; 69 | data_N=(linspace(0,2^bitnum-1,2^bitnum))';% Compute the histogram of the processed images,8 bit image 70 | 71 | 72 | data_n=size(N,1); 73 | U = fgfcm_init(cluster_n, data_n); % Initial fuzzy partition 74 | % Main loop 75 | for i = 1:max_iter, 76 | [U, center, obj_fcn(i)] = stepfgfcm(data_N,N, U, cluster_n,expo); 77 | if display, 78 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 79 | end 80 | % check termination condition 81 | if i > 1, 82 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 83 | end 84 | end 85 | 86 | iter_n = i; % Actual number of iterations 87 | obj_fcn(iter_n+1:max_iter) = []; 88 | U1=U; 89 | U=[]; 90 | U=U1(:,bin); 91 | 92 | 93 | 94 | function out=lnwi_fun(i,data2,rc,rs,rg) 95 | data=reshape(data2,size(data2,1)*size(data2,2),1); 96 | window=neighbor(rc,i); 97 | out=mean(data(window)); 98 | 99 | % sigma=sqrt(sum((data(neigh1)-data(i)).^2)/8); 100 | % S=lsm(data(i),lc,neigh1,neigh2,rs,rg,data,sigma); 101 | % out=S'*data(neigh1)/sum(S); 102 | 103 | function out=neighbor(rc,i) 104 | out=[]; 105 | r=rc(1); 106 | c=rc(2); 107 | %c1=floor(i/r)+1; 108 | r1=mod(i,r); 109 | if(r1==0), 110 | r1=r; 111 | c1=floor(i/r); 112 | else 113 | c1=floor(i/r)+1; 114 | end 115 | temp=[-1 1;-1 0;1 -1;1 0;0 1;0 -1;1 1;-1 -1]; 116 | temp(:,1)=temp(:,1)+r1+1; 117 | temp(:,2)=temp(:,2)+c1+1; 118 | temp=[temp;r1+1 c1+1]; 119 | % if(r1==1 || c1==1 || r1==r || c1==c), %Deal with boundary 120 | % nr=find(temp(:,1)==0 | temp(:,1)==r+1); 121 | % nc=find(temp(:,2)==0 | temp(:,2)==c+1); 122 | % rc=union(nc,nr); 123 | % temp(rc,:)=[]; 124 | % end 125 | temp(:,2)=temp(:,2)-1; 126 | out=temp*[1;r+2]; 127 | 128 | function out=lsm(lcv,lc,neigh1,neigh2,rs,rg,data,sigma) 129 | neigh2(:,1)=neigh2(:,1)-lc(1); 130 | neigh2(:,2)=neigh2(:,1)-lc(2); 131 | if sigma==0, 132 | p2=1/rg*ones(8,1); 133 | else 134 | p2=(data(neigh1)-lcv).^2/(rg*sigma^2); 135 | end 136 | out=exp(-((max(abs(neigh2'))/rs)'+p2)); 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /FGfcm/fgfcm.m: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = fgfcm(data, cluster_n,rg,options) 2 | % Use a 3*3 window 3 | data1=data; %Preprocess data 4 | data=[]; 5 | data=reshape(data1,size(data1,1)*size(data1,2),1); 6 | data2=padmatrix(data1,1); %Preprocess data 7 | % for i=2:size(data1,1)+1, 8 | % for j=2:size(data1,2)+1, 9 | % % if(i==1 || j==1 ||i==size(data1,1) || j==size(data1,2)) 10 | % % data_mean(i,j)=data1(i,j); 11 | % % continue; 12 | % % end 13 | % data_mean(i-1,j-1)=dm(i,j,data2); 14 | % end 15 | % end 16 | 17 | if nargin ~= 2 && nargin ~= 3 && nargin ~=4, 18 | error('Too many or too few input arguments!'); 19 | end 20 | % 21 | data_n = size(data, 1); 22 | % in_n = size(data, 2); 23 | 24 | % Change the following to set default options 25 | default_options = [2; % exponent for the partition matrix U 26 | 100; % max. number of iteration 27 | 1e-5; % min. amount of improvement 28 | 1; % info display during iteration 29 | 3;]; % scale factor 30 | 31 | if nargin == 2, 32 | options = default_options; 33 | rg=6.0; %control the effects of the neighbors term 34 | elseif nargin==3, 35 | options = default_options; 36 | else 37 | % If "options" is not fully specified, pad it with default values. 38 | if length(options) < 6, 39 | tmp = default_options; 40 | tmp(1:length(options)) = options; 41 | options = tmp; 42 | end 43 | % If some entries of "options" are nan's, replace them with defaults. 44 | nan_index = find(isnan(options)==1); 45 | options(nan_index) = default_options(nan_index); 46 | if options(1) <= 1, 47 | error('The exponent should be greater than 1!'); 48 | end 49 | end 50 | 51 | expo = options(1); % Exponent for U 52 | max_iter = options(2); % Max. iteration 53 | min_impro = options(3); % Min. improvement 54 | display = options(4); % Display info or not 55 | rs=options(5); 56 | bitnum=8; % 8bit image 57 | gln=2^bitnum; 58 | obj_fcn = zeros(max_iter, 1); % Array for objective function 59 | lnai=zeros(data_n,1); % Compute the local neighbor weighted image from original image 60 | rc=[size(data1,1) size(data1,2)]; 61 | 62 | for i=1:data_n, 63 | lnwi(i)=round(lnwi_fun(i,data2,rc,rs,rg)*(gln-1)); 64 | end % Compute the local neighbor weighted image from original image 65 | 66 | edges=linspace(0,2^bitnum,2^bitnum+1); % Compute the histogram of the processed images,8 bit image 67 | [N,~,bin]=histcounts(lnwi,edges); 68 | N=N'; 69 | data_N=(linspace(0,2^bitnum-1,2^bitnum))';% Compute the histogram of the processed images,8 bit image 70 | 71 | 72 | data_n=size(N,1); 73 | U = fgfcm_init(cluster_n, data_n); % Initial fuzzy partition 74 | % Main loop 75 | for i = 1:max_iter, 76 | [U, center, obj_fcn(i)] = stepfgfcm(data_N,N, U, cluster_n,expo); 77 | if display, 78 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 79 | end 80 | % check termination condition 81 | if i > 1, 82 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 83 | end 84 | end 85 | 86 | iter_n = i; % Actual number of iterations 87 | obj_fcn(iter_n+1:max_iter) = []; 88 | U1=U; 89 | U=[]; 90 | U=U1(:,bin); 91 | 92 | 93 | 94 | function out=lnwi_fun(i,data2,rc,rs,rg) 95 | data=reshape(data2,size(data2,1)*size(data2,2),1); 96 | [neigh1,neigh2,lc]=neighbor(rc,i); 97 | sigma=sqrt(sum((data(neigh1)-data(i)).^2)/8); 98 | S=lsm(data(i),lc,neigh1,neigh2,rs,rg,data,sigma); 99 | out=S'*data(neigh1)/sum(S); 100 | function [out1,out2,out3]=neighbor(rc,i) 101 | out1=[]; 102 | out2=[]; 103 | r=rc(1); 104 | c=rc(2); 105 | %c1=floor(i/r)+1; 106 | r1=mod(i,r); 107 | if(r1==0), 108 | r1=r; 109 | c1=floor(i/r); 110 | else 111 | c1=floor(i/r)+1; 112 | end 113 | temp=[-1 1;-1 0;1 -1;1 0;0 1;0 -1;1 1;-1 -1]; 114 | temp(:,1)=temp(:,1)+r1+1; 115 | temp(:,2)=temp(:,2)+c1+1; 116 | out3=[r1+1 c1+1]; 117 | % if(r1==1 || c1==1 || r1==r || c1==c), %Deal with boundary 118 | % nr=find(temp(:,1)==0 | temp(:,1)==r+1); 119 | % nc=find(temp(:,2)==0 | temp(:,2)==c+1); 120 | % rc=union(nc,nr); 121 | % temp(rc,:)=[]; 122 | % end 123 | out2=temp; 124 | temp(:,2)=temp(:,2)-1; 125 | out1=temp*[1;r+2]; 126 | 127 | function out=lsm(lcv,lc,neigh1,neigh2,rs,rg,data,sigma) 128 | neigh2(:,1)=neigh2(:,1)-lc(1); 129 | neigh2(:,2)=neigh2(:,1)-lc(2); 130 | if sigma==0, 131 | p2=1/rg*ones(8,1); 132 | else 133 | p2=(data(neigh1)-lcv).^2/(rg*sigma^2); 134 | end 135 | out=exp(-((max(abs(neigh2'))/rs)'+p2)); 136 | 137 | 138 | 139 | -------------------------------------------------------------------------------- /FGfcm_s2/fgfcm.m: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = fgfcm(data, cluster_n,rg,options) 2 | % Use a 3*3 window 3 | data1=data; %Preprocess data 4 | data=[]; 5 | data=reshape(data1,size(data1,1)*size(data1,2),1); 6 | data2=padmatrix(data1,1); %Preprocess data 7 | % for i=2:size(data1,1)+1, 8 | % for j=2:size(data1,2)+1, 9 | % % if(i==1 || j==1 ||i==size(data1,1) || j==size(data1,2)) 10 | % % data_mean(i,j)=data1(i,j); 11 | % % continue; 12 | % % end 13 | % data_mean(i-1,j-1)=dm(i,j,data2); 14 | % end 15 | % end 16 | 17 | if nargin ~= 2 && nargin ~= 3 && nargin ~=4, 18 | error('Too many or too few input arguments!'); 19 | end 20 | % 21 | data_n = size(data, 1); 22 | % in_n = size(data, 2); 23 | 24 | % Change the following to set default options 25 | default_options = [2; % exponent for the partition matrix U 26 | 100; % max. number of iteration 27 | 1e-5; % min. amount of improvement 28 | 1; % info display during iteration 29 | 3;]; % scale factor 30 | 31 | if nargin == 2, 32 | options = default_options; 33 | rg=6.0; %control the effects of the neighbors term 34 | elseif nargin==3, 35 | options = default_options; 36 | else 37 | % If "options" is not fully specified, pad it with default values. 38 | if length(options) < 6, 39 | tmp = default_options; 40 | tmp(1:length(options)) = options; 41 | options = tmp; 42 | end 43 | % If some entries of "options" are nan's, replace them with defaults. 44 | nan_index = find(isnan(options)==1); 45 | options(nan_index) = default_options(nan_index); 46 | if options(1) <= 1, 47 | error('The exponent should be greater than 1!'); 48 | end 49 | end 50 | 51 | expo = options(1); % Exponent for U 52 | max_iter = options(2); % Max. iteration 53 | min_impro = options(3); % Min. improvement 54 | display = options(4); % Display info or not 55 | rs=options(5); % scale factor 56 | bitnum=8; % 8bit image 57 | gln=2^bitnum; 58 | obj_fcn = zeros(max_iter, 1); % Array for objective function 59 | lnai=zeros(data_n,1); % Compute the local neighbor weighted image from original image 60 | rc=[size(data1,1) size(data1,2)]; 61 | 62 | for i=1:data_n, 63 | lnwi(i)=round(lnwi_fun(i,data2,rc,rs,rg)*(gln-1)); 64 | end % Compute the local neighbor weighted image from original image 65 | 66 | edges=linspace(0,2^bitnum,2^bitnum+1); % Compute the histogram of the processed images,8 bit image 67 | [N,~,bin]=histcounts(lnwi,edges); 68 | N=N'; 69 | data_N=(linspace(0,2^bitnum-1,2^bitnum))';% Compute the histogram of the processed images,8 bit image 70 | 71 | 72 | data_n=size(N,1); 73 | U = fgfcm_init(cluster_n, data_n); % Initial fuzzy partition 74 | % Main loop 75 | for i = 1:max_iter, 76 | [U, center, obj_fcn(i)] = stepfgfcm(data_N,N, U, cluster_n,expo); 77 | if display, 78 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 79 | end 80 | % check termination condition 81 | if i > 1, 82 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 83 | end 84 | end 85 | 86 | iter_n = i; % Actual number of iterations 87 | obj_fcn(iter_n+1:max_iter) = []; 88 | U1=U; 89 | U=[]; 90 | U=U1(:,bin); 91 | 92 | 93 | 94 | function out=lnwi_fun(i,data2,rc,rs,rg) 95 | data=reshape(data2,size(data2,1)*size(data2,2),1); 96 | window=neighbor(rc,i); 97 | out=median(data(window)); 98 | 99 | % sigma=sqrt(sum((data(neigh1)-data(i)).^2)/8); 100 | % S=lsm(data(i),lc,neigh1,neigh2,rs,rg,data,sigma); 101 | % out=S'*data(neigh1)/sum(S); 102 | 103 | function out=neighbor(rc,i) 104 | out=[]; 105 | r=rc(1); 106 | c=rc(2); 107 | %c1=floor(i/r)+1; 108 | r1=mod(i,r); 109 | if(r1==0), 110 | r1=r; 111 | c1=floor(i/r); 112 | else 113 | c1=floor(i/r)+1; 114 | end 115 | temp=[-1 1;-1 0;1 -1;1 0;0 1;0 -1;1 1;-1 -1]; 116 | temp(:,1)=temp(:,1)+r1+1; 117 | temp(:,2)=temp(:,2)+c1+1; 118 | temp=[temp;r1+1 c1+1]; 119 | % if(r1==1 || c1==1 || r1==r || c1==c), %Deal with boundary 120 | % nr=find(temp(:,1)==0 | temp(:,1)==r+1); 121 | % nc=find(temp(:,2)==0 | temp(:,2)==c+1); 122 | % rc=union(nc,nr); 123 | % temp(rc,:)=[]; 124 | % end 125 | temp(:,2)=temp(:,2)-1; 126 | out=temp*[1;r+2]; 127 | 128 | function out=lsm(lcv,lc,neigh1,neigh2,rs,rg,data,sigma) 129 | neigh2(:,1)=neigh2(:,1)-lc(1); 130 | neigh2(:,2)=neigh2(:,1)-lc(2); 131 | if sigma==0, 132 | p2=1/rg*ones(8,1); 133 | else 134 | p2=(data(neigh1)-lcv).^2/(rg*sigma^2); 135 | end 136 | out=exp(-((max(abs(neigh2'))/rs)'+p2)); 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /fcm_s1/udfcm_s1.m: -------------------------------------------------------------------------------- 1 | function [center, U, obj_fcn] = udfcm_s1(data, cluster_n,a,options) 2 | %FCM Data set clustering using fuzzy c-means clustering. 3 | % 4 | % [CENTER, U, OBJ_FCN] = FCM(DATA, N_CLUSTER) finds N_CLUSTER number of 5 | % clusters in the data set DATA. DATA is size M-by-N, where M is the number of 6 | % data points and N is the number of coordinates for each data point. The 7 | % coordinates for each cluster center are returned in the rows of the matrix 8 | % CENTER. The membership function matrix U contains the grade of membership of 9 | % each DATA point in each cluster. The values 0 and 1 indicate no membership 10 | % and full membership respectively. Grades between 0 and 1 indicate that the 11 | % data point has partial membership in a cluster. At each iteration, an 12 | % objective function is minimized to find the best location for the clusters 13 | % and its values are returned in OBJ_FCN. 14 | % 15 | % [CENTER, ...] = FCM(DATA,N_CLUSTER,OPTIONS) specifies a vector of options 16 | % for the clustering process: 17 | % OPTIONS(1): exponent for the matrix U (default: 2.0)ri li 18 | % OPTIONS(2): maximum number of iterations (default: 100) 19 | % OPTIONS(3): minimum amount of improvement (default: 1e-5) 20 | % OPTIONS(4): info display during iteration (default: 1) 21 | % The clustering process stops when the maximum number of iterations 22 | % is reached, or when the objective function improvement between two 23 | % consecutive iterations is less than the minimum amount of improvement 24 | % specified. Use NaN to select the default value. 25 | % 26 | % Example 27 | % data = rand(100,2); 28 | % [center,U,obj_fcn] = fcm(data,2); 29 | % plot(data(:,1), data(:,2),'o');ri li 30 | % hold on; 31 | % maxU = max(U); 32 | % % Find the data points with highest grade of membership in cluster 1 33 | % index1 = find(U(1,:) == maxU); 34 | % % Find the data points with highest grade of membership in cluster 2 35 | % index2 = find(U(2,:) == maxU); 36 | % line(data(index1,1),data(index1,2),'marker','*','color','g'); 37 | % line(data(index2,1),data(index2,2),'marker','*','color','r'); 38 | % % Plot the cluster centers 39 | % plot([center([1 2],1)],[center([1 2],2)],'*','color','k') 40 | % hold off; 41 | % 42 | % See also FCMDEMO, INITFCM, IRISFCM, DISTFCM, STEPFCM. 43 | 44 | % Roger Jang, 12-13-94, N. Hickey 04-16-01 45 | % Copyright 1994-2002 The MathWorks, Inc. 46 | data1=data; % 47 | data=[]; 48 | data=reshape(data1,size(data1,1)*size(data1,2),1); 49 | data2=padmatrix(data1,1); 50 | for i=2:size(data1,1)+1, 51 | for j=2:size(data1,2)+1, 52 | % if(i==1 || j==1 ||i==size(data1,1) || j==size(data1,2)) 53 | % data_mean(i,j)=data1(i,j); 54 | % continue; 55 | % end 56 | data_mean(i-1,j-1)=dm(i,j,data2); 57 | end 58 | end 59 | 60 | if nargin ~= 2 && nargin ~= 3 && nargin ~=4, 61 | error('Too many or too few input arguments!'); 62 | end 63 | 64 | data_n = size(data, 1); 65 | in_n = size(data, 2); 66 | 67 | % Change the following to set default options 68 | default_options = [2; % exponent for the partition matrix U 69 | 100; % max. number of iteration 70 | 1e-5; % min. amount of improvement 71 | 1]; % info display during iteration 72 | 73 | if nargin == 2, 74 | options = default_options; 75 | a=4.2; %control the effects of the neighbors term 76 | elseif nargin==3, 77 | options = default_options; 78 | else 79 | % If "options" is not fully specified, pad it with default values. 80 | if length(options) < 5, 81 | tmp = default_options; 82 | tmp(1:length(options)) = options; 83 | options = tmp; 84 | end 85 | % If some entries of "options" are nan's, replace them with defaults. 86 | nan_index = find(isnan(options)==1); 87 | options(nan_index) = default_options(nan_index); 88 | if options(1) <= 1, 89 | error('The exponent should be greater than 1!'); 90 | end 91 | end 92 | 93 | expo = options(1); % Exponent for U 94 | max_iter = options(2); % Max. iteration 95 | min_impro = options(3); % Min. improvement 96 | display = options(4); % Display info or not 97 | 98 | obj_fcn = zeros(max_iter, 1); % Array for objective function 99 | 100 | U = udinitfcm_s1(cluster_n, data_n); % Initial fuzzy partition 101 | % Main loop 102 | for i = 1:max_iter, 103 | [U, center, obj_fcn(i)] = udstepfcm_s1(data,data_mean, U, cluster_n,a,expo); 104 | if display, 105 | fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i)); 106 | end 107 | % check termination condition 108 | if i > 1, 109 | if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end, 110 | end 111 | end 112 | 113 | iter_n = i; % Actual number of iterations 114 | obj_fcn(iter_n+1:max_iter) = []; 115 | 116 | 117 | function out=dm(x,y,d) 118 | out=(d(x-1,y)+d(x-1,y-1)+d(x-1,y+1)+d(x+1,y)+d(x+1,y-1)+d(x+1,y+1)+d(x,y-1)+d(x,y+1))/8; 119 | 120 | 121 | 122 | 123 | --------------------------------------------------------------------------------