1 简介 图像分割是图像处理与理解,模式识别和人工智能等多个领域中非常关键的问题.现介绍了在MATLAB开发环境下,基于边缘检测与基于阈值的图像分割算法的仿真设计方法,为后续基于
1 简介
图像分割是图像处理与理解,模式识别和人工智能等多个领域中非常关键的问题.现介绍了在MATLAB开发环境下,基于边缘检测与基于阈值的图像分割算法的仿真设计方法,为后续基于MATLAB的数字图像处理方面的教学,科研提供了参考.
2 部分代码
function varargout = untitled(varargin)% UNTITLED M-file for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2.5 11-Jan-2013 13:05:10
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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 im;
global str;
[filename,pathname]=uigetfile_new({'*.*'},'选择训练图片...');
str=[pathname filename];
im=imread(str);
axes(handles.axes1);
imshow(im);
% --- 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 im1;
global str1;
[filename,pathname]=uigetfile_new({'*.*'},'选择测试图片...');
str1=[pathname filename];
im1=imread(str1);
axes(handles.axes2);
imshow(im1);
% --- 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 im;
global im1;
global str1;
se1 = strel('disk',5);
se2 = strel('square',16);
thresh1 = 0.2;
thresh2 = 0.01;
imtra = im2double(im);
rt = imtra(:,:,1);
gt = imtra(:,:,2);
bt = imtra(:,:,3);
idxr1 = find(rt>0);
idxg1 = find(gt>0);
idxb1 = find(bt>0);
mr1 = mean(rt(idxr1));
mg1 = mean(gt(idxg1));
mb1 = mean(bt(idxb1));
clear idxr1 idxg1 idxb1 idxgr1;
im1=imread(str1);
I1 = im2double(im1);
col = size(I1,2);
rate = 1600/col;
I1 = imresize(I1,rate);
r1 = I1(:,:,1);
g1 = I1(:,:,2);
b1 = I1(:,:,3);
[row1,col1] = size(r1);
r1 = abs(r1 - mr1);
g1 = abs(g1 - mg1);
b1 = abs(b1 - mb1);
% --- 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)
clear all
clc
close(gcf)
3 仿真结果
4 参考文献
[1]李小琦. 基于Matlab的图像阈值分割算法研究[J]. 软件导刊, 2014, 13(12):3.