【图像分割】基于均值聚类+OUST+区域生长法实现MRI图像分割附matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

个人主页:Matlab科研工作室

个人信条:格物致知。

更多Matlab仿真内容点击

智能优化算法  神经网络预测 雷达通信  无线传感器

信号处理 图像处理 路径规划 元胞自动机 无人机  电力系统

⛄ 内容介绍

聚类分析是医学图像分剖的重要方法.针对聚类算法中存在缺少先验知识,人为因素干扰,分割速度慢等缺陷,涌现出了大量的改进算法.结合现有的国内外研究成果,文章对近年来的基于聚类分析的医掌图像分割算法,发展现状,发展趋势及部分改进算法进行综述,主要介绍区域生长法,K-means算法,OUST算法等在医学图像分割领域的应用.

⛄ 部分代码

function varargout = main_imagseg(varargin)

% MAIN_IMAGSEG MATLAB code for main_imagseg.fig

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

%      singleton*.

%

%      H = MAIN_IMAGSEG returns the handle to a new MAIN_IMAGSEG or the handle to

%      the existing singleton*.

%

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

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

%

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

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

%      applied to the GUI before main_imagseg_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

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

% Last Modified by GUIDE v2.5 06-Nov-2022 19:17:42

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

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

                   'gui_Singleton',  gui_Singleton, ...

                   'gui_OpeningFcn', @main_imagseg_OpeningFcn, ...

                   'gui_OutputFcn',  @main_imagseg_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 main_imagseg is made visible.

function main_imagseg_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 main_imagseg (see VARARGIN)

% Choose default command line output for main_imagseg

handles.output = hObject;

axes(handles.axes1)

axis off

axes(handles.axes2)

axis off

% Update handles structure

guidata(hObject, handles);

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

% uiwait(handles.figure1);

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

function varargout = main_imagseg_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 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)

clc%清除command window上的命令  清屏

[filename,pathname,~]=uigetfile({'*.bmp';'*.jpg'},'All files');%选择图片文件

if~ischar(filename)%如果没有选择 则返回

    return

end

str=[pathname filename];%文件所在路径及名称

img=imread(str);%读取图片

axes(handles.axes1)

imshow(img);%显示灰度图片

title('输入图片')

handles.img=img;%存为结构体 便于其他函数使用

guidata(hObject, handles);

% --- 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)

img=handles.img;

if(size(img,3)==1)

    msgbox('请选择彩色图片')

    while 1,

        [filename,pathname,~]=uigetfile({'*.jpg';'*.bmp'},'All files');%选择图片文件

        if~ischar(filename)%如果没有选择 则返回

            return

        end

        str=[pathname filename];%文件所在路径及名称

        img=imread(str);%读取图片

        if(size(img,3)>1)

            axes(handles.axes1)

            imshow(img);%显示灰度图片

            title('输入图片')

            break;

        end

    end

end

I = im2double(img);                    % 转换为double类型数据

F = reshape(I,size(I,1)*size(I,2),3);                 %颜色特征

% 均值聚类参数获取

%簇类数目

Num_Clust=get(handles.edit1,'string');

K =str2num(Num_Clust);                       % 字符串转化为数字

Num_Iter=get(handles.edit2,'string');                                         % 迭代次数

Iters=str2num(Num_Iter);%字符串转化为数字

CENTS = F( ceil(rand(K,1)*size(F,1)) ,:);             % 初始化簇中心

DAL   = zeros(size(F,1),K+2);                         % 距离存储矩阵

for n = 1:Iters

    for i = 1:size(F,1)  

        for j = 1:K  

            DAL(i,j) = norm(F(i,:) - CENTS(j,:)); 

        end

        [Distance, CN] = min(DAL(i,1:K));               % 1:K计算距离

        DAL(i,K+1) = CN;                                % K+1 列 所属簇标签

        DAL(i,K+2) = Distance;                          % K+2 列最小距离

    end

    for i = 1:K

        A = (DAL(:,K+1) == i);                          % 第k个簇的点集

        CENTS(i,:) = mean(F(A,:));                      %更新簇中心点

        if sum(isnan(CENTS(:))) ~= 0                    % 若不存在 则更换为随机点作为中心点

            NC = find(isnan(CENTS(:,1)) == 1);           % 新的中心点

            for Ind = 1:size(NC,1)

                CENTS(NC(Ind),:) = F(randi(size(F,1)),:);%

            end

        end

    end

end

X = zeros(size(F));

for i = 1:K

idx = find(DAL(:,K+1) == i);

X(idx,:) = repmat(CENTS(i,:),size(idx,1),1); 

end

T = reshape(X,size(I,1),size(I,2),3);

% Show

axes(handles.axes2)

imshow(T); 

title('分割图像')

function edit1_Callback(hObject, eventdata, handles)

% hObject    handle to edit1 (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 edit1 as text

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

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

function edit1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit1 (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

function edit2_Callback(hObject, eventdata, handles)

% hObject    handle to edit2 (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 edit2 as text

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

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

function edit2_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit2 (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​

⛄ 运行结果

【图像分割】基于均值聚类+OUST+区域生长法实现MRI图像分割附matlab代码_第1张图片

【图像分割】基于均值聚类+OUST+区域生长法实现MRI图像分割附matlab代码_第2张图片

【图像分割】基于均值聚类+OUST+区域生长法实现MRI图像分割附matlab代码_第3张图片

⛄ 参考文献

[1]何瀚志, 朱红, 王竞. 基于聚类分析的医学图像分割综述[J]. 中国科技信息, 2017(15):2.

❤️ 关注我领取海量matlab电子书和数学建模资料

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

你可能感兴趣的:(图像处理,聚类,均值算法,matlab)