FCM算法是一种基于划分的聚类算法,它的思想就是使得被划分到同一簇的对象之间相似度最大,而不同簇之间的相似度最小。模糊C均值算法是普通C均值算法的改进,普通C均值算法对于数据的划分是硬性的,而FCM则是一种柔性的模糊划分。在介绍FCM具体算法之前我们先介绍一些模糊集合的基本知识。
1模糊集基本知识
首先说明隶属度函数的概念。隶属度函数是表示一个对象x隶属于集合A的程度的函数,通常记做μA(x),其自变量范围是所有可能属于集合A的对象(即集合A所在空间中的所有点),取值范围是[0,1],即0<=μA(x)<=1。μA(x)=1表示x完全隶属于集合A,相当于传统集合概念上的x∈A。一个定义在空间X={x}上的隶属度函数就定义了一个模糊集合A,或者叫定义在论域X={x}上的模糊子集。对于有限个对象x1,x2,……,xn模糊集合可以表示为:
(6.1)
有了模糊集合的概念,一个元素隶属于模糊集合就不是硬性的了,在聚类的问题中,可以把聚类生成的簇看成模糊集合,因此,每个样本点隶属于簇的隶属度就是[0,1]区间里面的值。
2K均值聚类算法(HCM,K-Means)介绍
K均值聚类(K-Means),即众所周知的C均值聚类,已经应用到各种领域。它的核心思想如下:算法把n个向量xj(1,2…,n)分为c个组Gi(i=1,2,…,c),并求每组的聚类中心,使得非相似性(或距离)指标的价值函数(或目标函数)达到最小。当选择欧几里德距离为组j中向量xk与相应聚类中心ci间的非相似性指标时,价值函数可定义为:
(6.2)
这里是组i内的价值函数。这样Ji的值依赖于Gi的几何特性和ci的位置。
一般来说,可用一个通用距离函数d(xk,ci)代替组I中的向量xk,则相应的总价值函数可表示为:
(6.3)
为简单起见,这里用欧几里德距离作为向量的非相似性指标,且总的价值函数表示为(6.2)式。
划分过的组一般用一个c×n的二维隶属矩阵U来定义。如果第j个数据点xj属于组i,则U中的元素uij为1;否则,该元素取0。一旦确定聚类中心ci,可导出如下使式(6.2)最小uij:
(6.4)
重申一点,如果ci是xj的最近的聚类中心,那么xj属于组i。由于一个给定数据只能属于一个组,所以隶属矩阵U具有如下性质:
(6.5)
且
(6.6)
另一方面,如果固定uij则使(6.2)式最小的最佳聚类中心就是组I中所有向量的均值:
(6.7)
这里|Gi|是Gi的规模或。
为便于批模式运行,这里给出数据集xi(1,2…,n)的K均值算法;该算法重复使用下列步骤,确定聚类中心ci和隶属矩阵U:
步骤1:初始化聚类中心ci,i=1,…,c。典型的做法是从所有数据点中任取c个点。
步骤2:用式(6.4)确定隶属矩阵U。
步骤3:根据式(6.2)计算价值函数。如果它小于某个确定的阀值,或它相对上次价值函数质的改变量小于某个阀值,则算法停止。
步骤4:根据式(6.5)修正聚类中心。返回步骤2。
该算法本身是迭代的,且不能确保它收敛于最优解。K均值算法的性能依赖于聚类中心的初始位置。所以,为了使它可取,要么用一些前端方法求好的初始聚类中心;要么每次用不同的初始聚类中心,将该算法运行多次。此外,上述算法仅仅是一种具有代表性的方法;我们还可以先初始化一个任意的隶属矩阵,然后再执行迭代过程。
K均值算法也可以在线方式运行。这时,通过时间平均,导出相应的聚类中心和相应的组。即对于给定的数据点x,该算法求最近的聚类中心ci,并用下面公式进行修正:
(6.8)
这种在线公式本质上嵌入了许多非监督学习神经元网络的学习法则。
3模糊C均值聚类
模糊C均值聚类(FCM),即众所周知的模糊ISODATA,是用隶属度确定每个数据点属于某个聚类的程度的一种聚类算法。1973年,Bezdek提出了该算法,作为早期硬C均值聚类(HCM)方法的一种改进。
FCM把n个向量xi(i=1,2,…,n)分为c个模糊组,并求每组的聚类中心,使得非相似性指标的价值函数达到最小。FCM与HCM的主要区别在于FCM用模糊划分,使得每个给定数据点用值在0,1间的隶属度来确定其属于各个组的程度。与引入模糊划分相适应,隶属矩阵U允许有取值在0,1间的元素。不过,加上归一化规定,一个数据集的隶属度的和总等于1:
(6.9)
那么,FCM的价值函数(或目标函数)就是式(6.2)的一般化形式:
(6.10)
这里uij介于0,1间;ci为模糊组I的聚类中心,dij=||ci-xj||为第I个聚类中心与第j个数据点间的欧几里德距离;且是一个加权指数。
构造如下新的目标函数,可求得使(6.10)式达到最小值的必要条件:
(6.11)
这里lj,j=1到n,是(6.9)式的n个约束式的拉格朗日乘子。对所有输入参量求导,使式(6.10)达到最小的必要条件为:
(6.12)
和
(6.13)
由上述两个必要条件,模糊C均值聚类算法是一个简单的迭代过程。在批处理方式运行时,FCM用下列步骤确定聚类中心ci和隶属矩阵U[1]:
步骤1:用值在0,1间的随机数初始化隶属矩阵U,使其满足式(6.9)中的约束条件
步骤2:用式(6.12)计算c个聚类中心ci,i=1,…,c。
步骤3:根据式(6.10)计算价值函数。如果它小于某个确定的阀值,或它相对上次价值函数值的改变量小于某个阀值,则算法停止。
步骤4:用(6.13)计算新的U矩阵。返回步骤2。
上述算法也可以先初始化聚类中心,然后再执行迭代过程。由于不能确保FCM收敛于一个最优解。算法的性能依赖于初始聚类中心。因此,我们要么用另外的快速算法确定初始聚类中心,要么每次用不同的初始聚类中心启动该算法,多次运行FCM。
4FCM算法的应用
通过上面的讨论,我们不难看出FCM算法需要两个参数一个是聚类数目C,另一个是参数m。一般来讲C要远远小于聚类样本的总个数,同时要保证C>1。对于m,它是一个控制算法的柔性的参数,如果m过大,则聚类效果会很次,而如果m过小则算法会接近HCM聚类算法。
算法的输出是C个聚类中心点向量和C*N的一个模糊划分矩阵,这个矩阵表示的是每个样本点属于每个类的隶属度。根据这个划分矩阵按照模糊集合中的最大隶属原则就能够确定每个样本点归为哪个类。聚类中心表示的是每个类的平均特征,可以认为是这个类的代表点。
function varargout = CT_image_FCM(varargin)% CT_IMAGE_FCM MATLAB code for CT_image_FCM.fig%CT_IMAGE_FCM, by itself, creates a new CT_IMAGE_FCM or raises the existing%singleton*.%%H = CT_IMAGE_FCM returns the handle to a new CT_IMAGE_FCM or the handle to%the existing singleton*.%%CT_IMAGE_FCM('CALLBACK',hObject,eventData,handles,...) calls the local%function named CALLBACK in CT_IMAGE_FCM.M with the given input arguments.%%CT_IMAGE_FCM('Property','Value',...) creates a new CT_IMAGE_FCM or raises the%existing singleton*. Starting from the left, property value pairs are%applied to the GUI before CT_image_FCM_OpeningFcn gets called. An%unrecognized property name or invalid value makes property application%stop. All inputs are passed to CT_image_FCM_OpeningFcn via varargin.%%*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one%instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help CT_image_FCM% Last Modified by GUIDE v2.5 17-Apr- 00:06:23% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @CT_image_FCM_OpeningFcn, ...'gui_OutputFcn', @CT_image_FCM_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before CT_image_FCM is made visible.function CT_image_FCM_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to CT_image_FCM (see VARARGIN)% Choose default command line output for CT_image_FCMhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes CT_image_FCM wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = CT_image_FCM_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I[filename, pathname]= ...uigetfile({'*.*';'*.bmp';'*.tif';'*.png';'*.jpg'},'select picture');str= [pathname filename];I= imread(str);axes(handles.axes1);imshow(I);title('原图');% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I I0 A[I2,clusterResult] = FCM1(I, 4,[0 80 160 255],2,150,1e-5);axes(handles.axes3);imshow(I2)I0=I2;A=unique(I0);% --- Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)% hObject handle to pushbutton4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I0 AI0_2=zeros(size(I0,1),size(I0,2));for i=1:size(I0,1)for j=1:size(I0,2)if I0(i,j)==A(2)I0_2(i,j)=255;else I0_2(i,j)=0;endendendI0_2=uint8(I0_2);axes(handles.axes4);imshow(I0_2);% --- Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)% hObject handle to pushbutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I0 AI0_3=zeros(size(I0,1),size(I0,2));for i=1:size(I0,1)for j=1:size(I0,2)if I0(i,j)==A(3)I0_3(i,j)=255;else I0_3(i,j)=0;endendendI0_3=uint8(I0_3);axes(handles.axes5);imshow(I0_3);% --- Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)% hObject handle to pushbutton6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I0 AI0_4=zeros(size(I0,1),size(I0,2));for i=1:size(I0,1)for j=1:size(I0,2)if I0(i,j)==A(4)I0_4(i,j)=255;else I0_4(i,j)=0;endendendI0_4=uint8(I0_4);axes(handles.axes6);imshow(I0_4);
function varargout = CT_image_GFCM(varargin)% CT_IMAGE_GFCM MATLAB code for CT_image_GFCM.fig%CT_IMAGE_GFCM, by itself, creates a new CT_IMAGE_GFCM or raises the existing%singleton*.%%H = CT_IMAGE_GFCM returns the handle to a new CT_IMAGE_GFCM or the handle to%the existing singleton*.%%CT_IMAGE_GFCM('CALLBACK',hObject,eventData,handles,...) calls the local%function named CALLBACK in CT_IMAGE_GFCM.M with the given input arguments.%%CT_IMAGE_GFCM('Property','Value',...) creates a new CT_IMAGE_GFCM or raises the%existing singleton*. Starting from the left, property value pairs are%applied to the GUI before CT_image_GFCM_OpeningFcn gets called. An%unrecognized property name or invalid value makes property application%stop. All inputs are passed to CT_image_GFCM_OpeningFcn via varargin.%%*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one%instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help CT_image_GFCM% Last Modified by GUIDE v2.5 13-Apr- 21:47:11% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @CT_image_GFCM_OpeningFcn, ...'gui_OutputFcn', @CT_image_GFCM_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before CT_image_GFCM is made visible.function CT_image_GFCM_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to CT_image_GFCM (see VARARGIN)% Choose default command line output for CT_image_GFCMhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes CT_image_GFCM wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = CT_image_GFCM_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I[filename, pathname]= ...uigetfile({'*.*';'*.bmp';'*.tif';'*.png';'*.jpg'},'select picture');str= [pathname filename];I= imread(str);axes(handles.axes1);imshow(I);title('原图');% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global II=medfilt2(I,[3,3]);axes(handles.axes2);imshow(I)% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I r c U[r,c] = size(I);data = zeros(r*c,1);for i = 1:rfor j = 1:cdata((i-1)*c+j,1) = double(I(i,j));endend[center, U, obj_fcn] = GFCM(data,4,0.9);for i = 1 : rfor j = 1 : ctemp = (double(I(i, j)) - center) .^ 2;[fmin pos] = min(temp);I(i, j) = uint8(pos * 255 / 4);endendaxes(handles.axes3);imshow(I)% --- Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)% hObject handle to pushbutton4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I r c UI_seg2 = I*0;for i = 1:rfor j = 1:cif U(1,(i-1)*c+j)<U(2,(i-1)*c+j)I_seg2(i,j) =0 ;elseI_seg2(i,j) =255;endendendaxes(handles.axes4);imshow(I_seg2)% --- Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)% hObject handle to pushbutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I r c UI_seg3 = I*0;for i = 1:rfor j = 1:cif U(1,(i-1)*c+j)<U(3,(i-1)*c+j)I_seg3(i,j) =0 ;elseI_seg3(i,j) =255;endendendaxes(handles.axes5);imshow(I_seg3)% --- Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)% hObject handle to pushbutton6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global I r c UI_seg4 = I*0;for i = 1:rfor j = 1:cif U(1,(i-1)*c+j)<U(4,(i-1)*c+j)I_seg4(i,j) =0 ;elseI_seg4(i,j) =255;endendendaxes(handles.axes6);imshow(I_seg4)