飞道的博客

【图像分割】基于matlab GUI阙值+边缘检测+区域法图像分割【含Matlab源码 817期】

322人阅读  评论(0)

一、简介

基于matlab GUI阙值+边缘检测+区域法图像分割

二、源代码

function varargout = Segimage(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Segimage_OpeningFcn, ...
                   'gui_OutputFcn',  @Segimage_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 Segimage is made visible.
function Segimage_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = Segimage_OutputFcn(hObject, eventdata, handles) 
varargout{
   1} = handles.output;

% -----载入图像
function inputimage_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile( ...
{
   '*.bmp;*.jpg;*.png;*.tif;*.jpeg', 'Image Files (*.bmp;*.jpg;*.png;*.tif;*.jpeg)'; ...
        '*.*',                   'All Files (*.*)'}, ...
        'Pick an Image');
 axes(handles.axes_src);
 fpath=[pathname filename];
img_src=imread(fpath);
global S
 S=img_src;
imshow(img_src);

% -----绘制灰度直方图
function imhist_Callback(hObject, eventdata, handles)
global S
figure,imhist(S)

% --------------------------------------------------------------------
function Thresholdmethod_Callback(hObject, eventdata, handles)
% hObject    handle to Thresholdmethod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Edgedet_Callback(hObject, eventdata, handles)
% hObject    handle to Edgedet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Areamethod_Callback(hObject, eventdata, handles)
% hObject    handle to Areamethod (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% ----迭代式阀值
function diedai_Callback(hObject, eventdata, handles)
axes(handles.axes_dst);
global S
tic  ;       %计时器
[x,y]=size(S);         % 求出图象大小 
b=double(S);                   
zd=double(max(max(S)));        % 求出图象中最大的灰度 
zx=double(min(min(S))) ;        % 最小的灰度  
T=double((zd+zx))/2;       % T赋初值,为最大值和最小值的平均值 
count=double(0);           % 记录几次循环 
while 1                   % 迭代最佳阈值分割算法 
    count=count+1; 
    S0=0.0; n0=0.0;   %为计算灰度大于阈值的元素的灰度总值、个数赋值 
    S1=0.0; n1=0.0;   %为计算灰度小于阈值的元素的灰度总值、个数赋值 
    for i=1:x
        for j=1:y
            if double(S(i,j))>=T
                S1=S1+double(S(i,j));  %大于阈域值图像点灰度值累加
                n1=n1+1;             %大于阈域值图像点个数累加
            else 
                S0=S0+double(S(i,j));  %小于阈域值图像点灰度值累加
                n0=n0+1;              %小于阀域值图像点个数累加
            end 
        end 
    end  
    T0=S0/n0; %求小于阀域值均值
    T1=S1/n1; %求大于阀域值均值
  if abs(T-((T0+T1)/2))<0.1 %迭代至 前后两次阀域值相差几乎为0时停止
        break;
else
T=(T0+T1)/2;    %在阈值T下,迭代阈值的计算过程 
   end 
end 
count ;           %显示运行次数
disp('迭代法最佳阀值:')
T                  %显示最佳阈值 算出T
toc ;               %显示运算时间
i1=im2bw(S,T/255);  % 图像在最佳阈值下二值化 
imshow(i1);
title('迭代阀值分割结果');

% -------Otsu阀值法
function otsu_Callback(hObject, eventdata, handles)
axes(handles.axes_dst);
global S
T=graythresh(S);
g=im2bw(S,T);
imshow(g);
title('otsu阀值分割结果');

% ------分水岭法
function watershed_Callback(hObject, eventdata, handles)
global S
f=S;
%计算梯度图
f=double(f);
hv=fspecial('prewitt');
hh=hv.';
gv=abs(imfilter(f,hv,'replicate'));
gh=abs(imfilter(f,hh,'replicate'));
g=sqrt(gv.^2+gh.^2);

%计算距离函数
df=bwdist(f);

%计算外部约束
L=watershed(df);
em=L==0;

%计算内部约束
im=imextendedmax(f,20);

%重构梯度图
g2=imimposemin(g,im|em);

%watershed算法分割
L2=watershed(g2);
 
 
axes(handles.axes_dst);
imshow(uint8(f));
title('分水岭法分割结果');

% --------------------------------------------------------------------
function roberts_Callback(hObject, eventdata, handles)
% hObject    handle to roberts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes_dst);
global S
[g,t]=edge(S,'roberts',[],'both');
imshow(g);
title('Roberts算子分割结果');
% --------------------------------------------------------------------
function sobel_Callback(hObject, eventdata, handles)
% hObject    handle to sobel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes_dst);
global S
[g,t]=edge(S,'sobel',[],'both');
imshow(g);
title('Sobel算子分割结果');
% --------------------------------------------------------------------
function prewitt_Callback(hObject, eventdata, handles)
% hObject    handle to prewitt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes_dst);
global S
[g,t]=edge(S,'prewitt',[],'both');
imshow(g);
title('Prewitt算子分割结果');
% --------------------------------------------------------------------
function log_Callback(hObject, eventdata, handles)
% hObject    handle to log (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes_dst);
global S
[g,t]=edge(S,'log');
imshow(g);
title('Log算子分割结果');

% --------------------------------------------------------------------
function canny_Callback(hObject, eventdata, handles)
% hObject    handle to canny (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global S
 
imshow(g);
title('Canny算子分割结果');

% -------区域生长法
function grow_Callback(hObject, eventdata, handles)
global S
image=S;
 
else
    I=image;
end

三、运行结果



四、备注

完整代码或者代写添加QQ 1564658423


转载:https://blog.csdn.net/TIQCmatlab/article/details/116068060
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场