A list,

Image segmentation based on MATLAB GUI threshold + edge detection + region method

Ii. Source code

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; % ----- loads the imagefunction 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); % ----- Draw gray histogramfunction 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)% ---- iterative threshold functiondiedai_Callback(hObject, eventdata, handles)
axes(handles.axes_dst); global S tic ; % timer [x,y]=size(S); % find the image size b=double(S);                   
zd=double(max(max(S))); % Find the maximum grayscale zx= in the imagedouble(min(min(S))) ; % Minimum gray T=double((zd+zx))/2; % T assigns the initial value, which is the average of the maximum and minimum value count=double(0); % Records how many cycleswhile 1% Iterative optimal threshold segmentation algorithm count=count+1; 
    S0=0.0; n0=0.0; % is the total gray value and number of elements whose gray scale is greater than the threshold0.0; n1=0.0; % is the total gray value and number of elements whose gray scale is less than the threshold valuefor i=1:x
        for j=1:y
            if double(S(i,j))>=T
                S1=S1+double(S(i,j)); % is greater than the threshold value of the image point gray value accumulated n1= N1 +1; % is the sum of image points greater than the thresholdelse 
                S0=S0+double(S(i,j)); % is less than the threshold domain value of the sum of image point gray value n0= N0 +1; T0=S0/n0; % is less than the mean value T1=S1/ N1; % find the mean value greater than the threshold valueif abs(T-((T0+T1)/2))<0.1 % iteration to stop when the difference between the two threshold domains is almost 0break;
else
T=(T0+T1)/2; % At threshold T, iteration threshold calculation process end end count; % Display number of runs disp('Iterative method optimal threshold:'T % displays the best threshold to calculate T toc; % Displays the operation time i1=im2bw(S,T/255); % image binarization imshow(i1) under the optimal threshold; title('Iterative threshold segmentation results'); % -------Otsu threshold methodfunction otsu_Callback(hObject, eventdata, handles)
axes(handles.axes_dst);
global S
T=graythresh(S);
g=im2bw(S,T);
imshow(g);
title('OTSU threshold segmentation result'); % ------ watershed methodfunction watershed_Callback(hObject, eventdata, handles)
global S
f=S; % Calculate the gradient graph 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); % Calculate distance function df=bwdist(f); % Calculate the external constraint L=watershed(DF); em=L==0; % calculate internal constraint im=imextendedmax(f,20); % to reconstruct gradient map g2 = imimposemin (g, im | em); % WATERSHED algorithm segmentation L2=watershed(G2); axes(handles.axes_dst); imshow(uint8(f)); title('Watershed segmentation results'); % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --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 operator segmentation result '); % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --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 operator segmentation result '); % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --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 operator segmentation result '); % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --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 operator split result '); % -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --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 operator segmentation result '); % ------- area growth methodfunction grow_Callback(hObject, eventdata, handles)
global S
image=S;
 
else
    I=image;
end
Copy the code

3. Operation results





Fourth, note

Version: 2014 a