【图像分割】基于模糊核聚类算法KFCM实现医学图像分割matlab代码

195 阅读10分钟

1 简介

医学影像分割的基本目标是将图像分割成不同的解剖组织,从而可以从背景中提取出感兴趣区域。因为图像的低分辨率和弱对比度,实现医学影像分割是一件具有挑战的任务。而且,这个任务由于噪声和伪阴影变得更加困难,这些干扰项可能是因器材限制、重建算法和患者移动等原因造成的。目前还没有通用的医学图像分割算法,算法的优点和缺点经常根据所研究的问题而变化。将分割概念具体到颅内出血 CT 图像上,就是将颅腔中的出血病灶区别于其他组织从图像中分割出来。图像分割有两大类方法 :一种是像素分类,另一种为追踪变化的边界。第一类分割方法假设在每个子类中像素具有几乎恒定的灰度值,这对于具有相似生理特性的解剖结构来说是合理的。这类方法可以同时检测出多个感兴趣的子类,但是该方法很容易受到环境噪声和图像不均匀性的影响。相比之下,追踪变化边界的分割方法要利用图像灰度信息和空间信息。因此,该子类必须是均匀的并且包围在一个特定的变化边界中。当应用于医学图像分割时,由于固有的噪声和伪阴影,它们都不具有普遍的适用性。

本文针对人脑CT图像的出血病灶区域,提出了一种改进的模糊C-均值(Fuzzy C-Means,FCM)算法进行颅脑内出血病灶的分割.首先对颅脑CT图像进行预分割,通过左右扫描算法和中值滤波算法将颅内结构从源CT图像中提取出来;然后对预分割而得到的颅内结构,利用在目标函数和隶属度函数中分别添加空间信息的改进FCM聚类算法进行出血病灶提取.通过对CT颅脑图像和添加椒盐噪声的CT颅脑图像进行病灶分割,结果显示本文算法对噪声不敏感,可以准确分割出出血病灶.

img

img

2 部分代码

function varargout = MainGUI(varargin)

% MAINGUI M-file for MainGUI.fig

%      MAINGUI, by itself, creates a new MAINGUI or raises the existing

%      singleton*.

%

%      HKFCMPARA = MAINGUI returns the handle to a new MAINGUI or the handle to

%      the existing singleton*.

%

%      MAINGUI('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in MAINGUI.M with the given input arguments.

%

%      MAINGUI('Property','Value',...) creates a new MAINGUI or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before MainGUI_OpeningFunction gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to MainGUI_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

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

                   'gui_Singleton',  gui_Singleton, ...

                   'gui_OpeningFcn', @MainGUI_OpeningFcn, ...

                   'gui_OutputFcn',  @MainGUI_OutputFcn, ...

                   'gui_LayoutFcn',  [] , ...

                   'gui_Callback',   []);

if nargin && ischar(varargin{1})

    gui_State.gui_Callback = str2func(varargin{1});

end

if nargout

    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

else

    gui_mainfcn(gui_State, varargin{:});

end

% End initialization code - DO NOT EDIT

% --- Executes just before MainGUI is made visible.

function MainGUI_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 MainGUI (see VARARGIN)

% Choose default command line output for MainGUI

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

% UIWAIT makes MainGUI wait for user response (see UIRESUME)

% uiwait(handles.MainFig);

% 日期时间显示

set(handles.timestr,'string',datestr(now,0));

htimer = timer('StartDelay',1,'TimerFcn',...

    'htimestr=findall(0,''tag'',''timestr'');set(htimestr,''string'',datestr(now,0));',...

    'Period',1,'ExecutionMode','fixedSpacing','tag','showtime');

start(htimer);

% --- Outputs from this function are returned to the command line.

function varargout = MainGUI_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 structure

varargout{1} = handles.output;

% --- Executes during object creation, after setting all properties.

function hslider_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hslider (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.

if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor',[.9 .9 .9]);

end

% --- Executes during object creation, after setting all properties.

function hedit_fname_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hedit_fname (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes during object creation, after setting all properties.

function hedit_heb_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hedit_heb (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes during object creation, after setting all properties.

function hedit_xuhao_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hedit_xuhao (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes during object creation, after setting all properties.

function hedit_zhishu_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hedit_zhishu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes during object creation, after setting all properties.

function hedit_zuixiao_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hedit_zuixiao (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes during object creation, after setting all properties.

function hedit_cishu_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hedit_cishu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes during object creation, after setting all properties.

function hedit_leibie_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hedit_leibie (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes during object creation, after setting all properties.

function hmenu_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hmenu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes during object creation, after setting all properties.

function hedit_info_CreateFcn(hObject, eventdata, handles)

% hObject    handle to hedit_info (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

% --- Executes on slider movement.

function hslider_Callback(hObject, eventdata, handles)

% hObject    handle to hslider (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider

%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% 获取滑动条参数

valMin = get(hObject,'Min'); 

valMax = get(hObject,'Max');

val = get(hObject,'Value');

result = get(handles.btnrun,'UserData'); % 获取分割结果

cluster_n = get(handles.hedit_leibie,'UserData');   % 获取聚类数目

eachstep = (valMax - valMin)/(cluster_n-2);  % 每副图对应的滑块步长

npic = round((val-valMin)/eachstep);    % 计算最近的点

% set(hObject,'Value',eachstep*npic+valMin);  % 自动将滑块移动到整点数

% 得到axes该显示的图像的数据

I1 = result{npic+1};

I2 = result{npic+2};

% 更现分类显示的axes

% 对于第一类的轴

axes(handles.h1leiaxes);

imshow(I1,[]);

set(gcf,'NextPlot','add'); % This is to cinquer the bug of imshow

% 对于第二类的轴

axes(handles.h2leiaxes);

imshow(I2,[]);

set(gcf,'NextPlot','add'); % This is to cinquer the bug of imshow

% 更新轴上的说明文字

h1leitxtstr = ['第',num2str(npic+1),'类图像'];

h2leitxtstr = ['第',num2str(npic+2),'类图像'];

set(handles.h1leitxt,'string',h1leitxtstr);

set(handles.h2leitxt,'string',h2leitxtstr);

% --- Executes on button press in btnOpen.

function btnOpen_Callback(hObject, eventdata, handles)

% hObject    handle to btnOpen (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

[filename, pathname] = uigetfile('*.mat', '打开图像数据文件');

if filename~=0

    set(gcf,'Pointer','watch'); % 改变鼠标状态为等待状态

    drawnow;

    set(handles.hedit_fname,'string',filename);

   % fid=fopen([pathname filename],'r');

   % F=fread(fid,'uint8');

   % fclose(fid);

    load([pathname filename]);

    F = II;

    set(hObject,'UserData',F);

    set(handles.hedit_xuhao,'string',num2str(3));

    % 默认显示第2幅图

    I=F(1+181*181*(3-1):181*181*3);

    I=reshape(I,181,181);

    I=imrotate(I,90);

    axes(handles.hyuansiaxes);

    himage = imshow(I,[]);

    set(gcf,'NextPlot','add'); % This is to cinquer the bug of imshow

    % in image toolbox ver 5.0.0 and 5.0.1.

    set(handles.hyuansiaxes,'UserData',I);

    set(gcf,'Pointer','arrow');

    set(handles.hslider,'enable','off'); % 滑动条不可用

    set(handles.btnsave,'enable','off');    % 结果保存按钮不可用 

else

    set(handles.hedit_fname,'string','尚未打开');

end

function hedit_heb_Callback(hObject, eventdata, handles)

% hObject    handle to hedit_heb (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of hedit_heb as text

%        str2double(get(hObject,'String')) returns contents of hedit_heb as a double

str = get(hObject,'string');

data = str2num(str);

if isempty(data)  % 输入参数有效性检测

    errordlg('输入必须为数值!','参数错误');

    set(hObject,'BackgroundColor','r');

else

    set(hObject,'BackgroundColor','w');

    set(hObject,'UserData',data);

end

function hedit_xuhao_Callback(hObject, eventdata, handles)

% hObject    handle to hedit_xuhao (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of hedit_xuhao as text

%        str2double(get(hObject,'String')) returns contents of hedit_xuhao as a double

str = get(hObject,'string');

data = str2num(str);

if isempty(data)  % 输入参数有效性检测

    errordlg('输入必须为数值!','参数错误');

    set(hObject,'BackgroundColor','r');

elseif data<1 || data>7 || data~=round(data) % 如果序号超出范围,或不是整数

    errordlg('输入必须为介于1到7之间的整数!','参数错误');

    set(hObject,'BackgroundColor','r');

else

    set(hObject,'BackgroundColor','w');

    % 根据输入序号显示原图

    F = get(handles.btnOpen,'UserData');

    if isempty(F)

        msgbox('尚未打开图像数据文件','操作错误');

        return;

    else

        I=F(1+181*181*(data-1):181*181*data);

        I=reshape(I,181,181);

        I=imrotate(I,90);

        axes(handles.hyuansiaxes);

        himage = imshow(I,[]);

        set(gcf,'NextPlot','add'); % This is to cinquer the bug of imshow 

                        % in image toolbox ver 5.0.0 and 5.0.1. 

        set(handles.hyuansiaxes,'UserData',I);

        set(handles.hedit_xuhao,'UserData',data);

    end

end

function hedit_zhishu_Callback(hObject, eventdata, handles)

% hObject    handle to hedit_zhishu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of hedit_zhishu as text

%        str2double(get(hObject,'String')) returns contents of hedit_zhishu as a double

str = get(hObject,'string');

data = str2num(str);

if isempty(data)  % 输入参数有效性检测

    errordlg('输入必须为数值!','参数错误');

    set(hObject,'BackgroundColor','r');

elseif data<=1 % 如果指数不大于1

    errordlg('输入必须大于1','参数错误');

    set(hObject,'BackgroundColor','r');

else    

    set(hObject,'BackgroundColor','w');

    set(hObject,'UserData',data);

end

function hedit_zuixiao_Callback(hObject, eventdata, handles)

% hObject    handle to hedit_zuixiao (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of hedit_zuixiao as text

%        str2double(get(hObject,'String')) returns contents of hedit_zuixiao as a double

str = get(hObject,'string');

data = str2num(str);

if isempty(data)  % 输入参数有效性检测

    errordlg('输入必须为数值!','参数错误');

    set(hObject,'BackgroundColor','r');

elseif data<0 % 如果指数不大于1

    errordlg('输入必须大于0','参数错误');

    set(hObject,'BackgroundColor','r');

else    

    set(hObject,'BackgroundColor','w');

    set(hObject,'UserData',data);

end

function hedit_cishu_Callback(hObject, eventdata, handles)

% hObject    handle to hedit_cishu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of hedit_cishu as text

%        str2double(get(hObject,'String')) returns contents of hedit_cishu as a double

str = get(hObject,'string');

data = str2num(str);

if isempty(data)  % 输入参数有效性检测

    errordlg('输入必须为数值!','参数错误');

    set(hObject,'BackgroundColor','r');

elseif data<1 || data~=round(data) % 如果序号超出范围,或不是整数

    errordlg('输入必须为大于等于1的整数!','参数错误');

    set(hObject,'BackgroundColor','r');

else    

    set(hObject,'BackgroundColor','w');

    set(hObject,'UserData',data);

    

end

function hedit_leibie_Callback(hObject, eventdata, handles)

% hObject    handle to hedit_leibie (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of hedit_leibie as text

%        str2double(get(hObject,'String')) returns contents of hedit_leibie as a double

str = get(hObject,'string');

data = str2num(str);

if isempty(data)  % 输入参数有效性检测

    errordlg('输入必须为数值!','参数错误');

    set(hObject,'BackgroundColor','r');

elseif data<2 || data~=round(data) % 如果序号超出范围,或不是整数

    errordlg('输入必须为大于等于2的整数!','参数错误');

    set(hObject,'BackgroundColor','r');

else    

    set(hObject,'BackgroundColor','w');

    set(hObject,'UserData',data);    

end

% --- Executes on button press in btnrun.

function btnrun_Callback(hObject, eventdata, handles)

% hObject    handle to btnrun (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% 更改界面控件状态

set(handles.hedit_info,'enable','on');  % 信息输出编辑框可用

set(handles.btnsave,'enable','on');    % 结果保存按钮可用

expo = get(handles.hedit_zhishu,'UserData');    % 指数m

max_iter = get(handles.hedit_cishu,'UserData'); % 最大迭代次数

min_impro = get(handles.hedit_zuixiao,'UserData');  % 目标函数最小改变量

cluster_n = get(handles.hedit_leibie,'UserData');   % 聚类数目

display = get(handles.chkbx,'UserData');    % 每次迭代是否输出信息

algorithm = get(handles.hmenu,'UserData');  % 选择的算法种类

data = get(handles.hyuansiaxes,'UserData'); % 获取原始图像数据

data = data(:);

data_n = size(data, 1); % 求出data的第一维(rows)数,即样本个数

in_n = size(data, 2);   % 求出data的第二维(columns)数,即特征值长度

3 仿真结果

4 参考文献

[1]姜春雨, 刘景鑫, 钟慧湘,等. 基于改进的FCM模糊聚类的颅内出血CT图像分割研究[J]. 中国医疗设备, 2018, 33(6):5.

部分理论引用网络文献,若有侵权联系博主删除。