一、简介
基于matlab GUI阙值、边缘、形态学、种子点图像分割
二、源代码
function varargout = bishe2(varargin)
% BISHE2 MATLAB code for bishe2.fig
% BISHE2, by itself, creates a new BISHE2 or raises the existing
% singleton*.
%
% H = BISHE2 returns the handle to a new BISHE2 or the handle to
% the existing singleton*.
%
% BISHE2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BISHE2.M with the given input arguments.
%
% BISHE2('Property','Value',...) creates a new BISHE2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before bishe2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to bishe2_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 bishe2
% Last Modified by GUIDE v2.5 26-Nov-2020 16:50:25
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @bishe2_OpeningFcn, ...
'gui_OutputFcn', @bishe2_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 bishe2 is made visible.
function bishe2_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 bishe2 (see VARARGIN)
% Choose default command line output for bishe2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes bishe2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = bishe2_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)
global yuantu;
%文件打开对话框
[filename,pathname]=uigetfile({
'*.bmp';'*.jpg';'*.gif'},'选择图片');
if isequal(filename,0)|isequal(pathname,0)
errordlg('没有打开图像','出错');
return;
else
file=[pathname,filename];
yuantu=imread(file);%读入图像
%设置显示的坐标轴
axes(handles.axes1);
%显示图像
imshow(yuantu);title('原始图像');
end
% --- 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 yuantu;
axes(handles.axes2);
f= rgb2gray(yuantu);
imshow(f);title('灰度图像');
% --- 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 yuantu;
axes(handles.axes3);
%%%%%%%%%%%%%%%大松算法
level=graythresh(yuantu); %确定灰度阈值
BW=im2bw(yuantu,level);
imshow(BW);
title('基于阈值的细胞分割');
% --- 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 yuantu;
%将axes4作为当前的坐标轴
axes(handles.axes4);
%获取popupmenu1的句柄为c
c=get(handles.popupmenu1,'value');
f=im2bw(yuantu);
[g_sobel_default , ts] = edge(f, 'sobel');
[g_log_default,tlog]=edge(f, 'log');
[g_canny_default,tc]=edge(f, 'canny' );
[g_prewitt_default,tp]=edge(f, 'prewitt');
[g_roberts_default,tr]=edge(f, 'roberts' );
g_sobel_best=edge(f, 'sobel' ,0.25);
g_log_best=edge(f, 'log',0.003,2.25);
g_canny_best=edge(f, 'canny' ,[0.04 0.10],1.5);
switch c
case 1 %使用sobel算子边缘检测
imshow(g_sobel_default);title('sobel图像边缘提取(自动阈值)');
case 2 %使用log特算子边缘检测
imshow(g_log_default);title('log图像边缘提取(自动阈值)');
case 3 %使用canny算子边缘检测
imshow(g_canny_default);title('canny图像边缘提取(自动阈值)');
case 4 %使用log特算子边缘检测
imshow(g_prewitt_default);title('prewitt图像边缘提取(自动阈值)');
case 5 %使用canny算子边缘检测
imshow(g_roberts_default);title('Roberts图像边缘提取(自动阈值)');
case 6 %使用sobel算子边缘检测
imshow(g_sobel_best);title('sobel算子边缘检测');
case 7 %使用log特算子边缘检测
imshow(g_log_best);title('log算子边缘检测');
case 8 %使用canny算子边缘检测
imshow(g_canny_best);title('canny算子边缘检测');
case 9
blood=rgb2gray(yuantu);
[m,n]=size(blood); % 求出图象大小
b=double(blood); % 将blood转为双精度浮点类型
N =sqrt(100) * randn(m,n); % 生成方差为10的白噪声
I=b+N; % 噪声干扰图象
z0=max(max(I)); % 求出图象中最大的灰度
z1=min(min(I)); % 最小的灰度
T=(z0+z1)/2; % 求最大和最小灰度的平均值
TT=0;
S0=0; n0=0;
S1=0; n1=0;
allow=0.5; % 新旧阈值的允许接近程度
d=abs(T-TT); % 数的绝对值
count=0; % 记录几次循环
while(d>=allow) % 迭代最佳阈值分割算法
count=count+1;
for i=1:m
for j=1:n
if (I(i,j)>=T)
S0=S0+I(i,j); % 图像中各个大于平均灰度值的点的灰度之和
n0=n0+1; % 图像中各个大于平均灰度值的点的总数
end
if (I(i,j)<T)
S1=S1+I(i,j); % 图像中各个小于平均灰度值的点的灰度之和
n1=n1+1; % 图像中各个小于平均灰度值的点的总数
end
end
end
T0=S0/n0; % 所有大于平均灰度值的点的平均灰度值
T1=S1/n1; % 所有小于平均灰度值的点的平均灰度值
TT=(T0+T1)/2; % 平均值
d=abs(T-TT); % 两个平均值的差
T=TT;
end
Seg=zeros(m,n);
for i=1:m
for j=1:n
if(I(i,j)>=T)
Seg(i,j)=1; % 阈值分割的图象
end
end
end
SI=1-Seg; % 阈值分割后的图象求反,便于用腐蚀算法求边缘
se1=strel('square',3); % 定义腐蚀算法的结构 strel是构建形态学运算中的结构元素函数
SI1=imerode(SI,se1); % 腐蚀算法
BW=SI-SI1; % 边缘检测
imshow(BW);title('New algorithm');
end
% --- 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 yuantu;
num=0; % 图像位置计数
rowi=3; % 图像显示行数
colui=3; % 图像显示列数
%%%%%%%%%%% 读入图像 %%%%%%%
I=yuantu;
num=num+1;figure(1),subplot(rowi,colui,num),imshow(I),title('原图'); % 显示
%%%%%%%%%% 程序开始,计算时间 %%%%%%%%%%
tic % tic 和 toc 为程序运行时间的计算
%%%%%%%%%% 灰度化 %%%%%%%%%%
Igray = rgb2gray(I);
num=num+1;figure(1),subplot(rowi,colui,num),imshow(Igray),title('灰度化');
%%%%%%%%%% 填充--灰度重构 %%%%%%%%%%
Ifill = imfill(Igray,'holes');%填充二值图像中的空洞区域。 如,黑色的背景上有个白色的圆圈。则这个圆圈内区域将被填充。
num=num+1;figure(1),subplot(rowi,colui,num),imshow(Ifill),title('灰度重构');
%%%%%% 二值化阈值选取过程 %%%%%
%%% 第一步,淋巴细胞不会在视场边缘,因此选择处理比视场小的图像范围,减少背景
[m,n]=size(Ifill); % 图像大小
Ifilltest=Ifill(40:m-50,60:n-50); % 40-238,60-302
%%% 第二步,直方图
[yout,xx]=imhist(Ifilltest); % yout为统计的个数,xx为相应的灰度值
num=num+1;figure(1),subplot(rowi,colui,num),imhist(Ifilltest),title('直方图');
%%% 重构后淋巴细胞的灰度比白细胞灰度低,因此选择灰度低于220的直方图中的最大值
%%% 对应的灰度作为阈值
%%% 重构的时候,只在闭合区域进行填充--设置成相同的灰度值,不同区域设置成不同的灰度
%%% 虽然背景区域看上去比目标区域大,但是未闭合,未填充,没有相同的灰度值,虽然看上去灰度比较相近,但是,灰度值不同
%%% 因此,在选择阈值的时候,还是选择最大区域的灰度作为阈值
yout = yout(1:255); % 可以修改,保证重构之后的目标灰度在所选范围内
[yy,P]=max(yout); % yy返回直方图最高的个数,P为相应灰度值
P = P-1; % P即为阈值
%%%%%%%%%% 二值化 %%%%%%%%%%
Ib = binarize1(Ifill,P); % 自编函数binarize1
num=num+1;figure(1),subplot(rowi,colui,num),imshow(Ib),title('二值化');
%%%%%%%%%% 去小区域 %%%%%%%%%%%
Io = bwareaopen(Ib, 500); % 形态学开运算,去小区域
num=num+1;figure(1),subplot(rowi,colui,num),imshow(Io),title('去小区域');
%%%%%%%%%% 填充孔洞 %%%%%%%%%%%
Iend = imfill(Io,'holes');
num=num+1;figure(1),subplot(rowi,colui,num),imshow(Iend),title('填充孔洞');
% 此时I中白色区域,即为淋巴细胞所在的区域
% 先找到像素最多的一列,即最长的一列
[H, col] = max(sum(Iend)); % 对每一列求和,H为最大值,col为所在行
% 再找到该列上最低点
row = min(find(Iend(:, col)));
%row = find(I(:, col),'first');
% [row, col]为求边界曲线的初始点
boundary = bwtraceboundary(Iend, [row, col], 'N'); % 找出图像的边界点坐
三、运行结果
四、备注
完整代码或者代写添加QQ 1564658423