一、简介
边缘是图像的最重要的特征。边缘是指周围像素灰度有阶跃变化或屋顶变化的那些像素的集合。边缘检测主要是灰度变化的度量、检测和定位。有很多种不同的边缘检测方法,同一种方法使用的滤波器也不尽相同。图像边缘检测就是研究更好的边缘检测方法和检测算子。
边缘检测的基本思想:
(1)利用边缘增强算子,突出图像中的局部边缘;
(2)然后定义像素的“边缘强度”,通过设置阈值的方法提取边缘点集。由于噪声和模糊的存在,监测到的边界可能会变宽或在某点处发生间断。
边界检测包括两个基本内容:
i. 用边缘算子提取出反映灰度变化的边缘点集
ii. 在边缘点集合中剔除某些边界点或填补边界间断点,并将这些边缘连接成完整的线
常用的检测算子有拉普拉斯边缘检测算法、Robert边缘检测算子、Sobel边缘检测算子、Prewitt边缘检测算子、Canny边缘检测算子。
二、源代码
function varargout = DIPGUI(varargin)
% DIPGUI MATLAB code for DIPGUI.fig
% DIPGUI, by itself, creates a new DIPGUI or raises the existing
% singleton*.
%
% H = DIPGUI returns the handle to a new DIPGUI or the handle to
% the existing singleton*.
%
% DIPGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DIPGUI.M with the given input arguments.
%
% DIPGUI('Property','Value',...) creates a new DIPGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before DIPGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to DIPGUI_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 DIPGUI
% Last Modified by GUIDE v2.5 30-Nov-2018 12:21:38
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DIPGUI_OpeningFcn, ...
'gui_OutputFcn', @DIPGUI_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 DIPGUI is made visible.
function DIPGUI_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 DIPGUI (see VARARGIN)
% Choose default command line output for DIPGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes DIPGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = DIPGUI_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 getpic.
function getpic_Callback(hObject, eventdata, handles)
% hObject handle to getpic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I
[filename,pathname]=...
uigetfile({'*.*';'*.bmp';'*.tif';'*.png'},'select picture'); %选择图片路径
str=[pathname filename]; %合成路径+文件名
I=imread(str);
axes(handles.yuantu); %使用第一个axes
imshow(I); %显示图片
% --- 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)
% --- Executes on button press in savepic.
function savepic_Callback(hObject, eventdata, handles)
% hObject handle to savepic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName] = uiputfile({'*.jpg','JPEG(*.jpg)';...
'*.bmp','Bitmap(*.bmp)';...
'*.gif','GIF(*.gif)';...
'*.*', 'All Files (*.*)'},...
'Save Picture','Untitled');
if FileName==0
return;
else
h=getframe(handles.chulihou);
imwrite(h.cdata,[PathName,FileName]);
end
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton2
% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton3
% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton4
% --- Executes on button press in radiobutton5.
function radiobutton5_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton5
% --- Executes during object creation, after setting all properties.
function uibuttongroup2_CreateFcn(hObject, eventdata, handles)
% hObject handle to uibuttongroup2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes when selected object is changed in uibuttongroup2.
function uibuttongroup2_SelectionChangedFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uibuttongroup2
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I
global BW %定义全局变量
str=get(hObject,'string'); %拿到所选按钮的名称
axes(handles.chulihou); %使用第二个axes
switch str %选择语句
case'Initial' %点击原图按钮
imshow(I); %显示原图
function imDst = boxfilter(imSrc, r)
% BOXFILTER O(1) time box filtering using cumulative sum
%
% - Definition imDst(x, y)=sum(sum(imSrc(x-r:x+r,y-r:y+r)));
% - Running time independent of r;
% - Equivalent to the function: colfilt(imSrc, [2*r+1, 2*r+1], 'sliding', @sum);
% - But much faster.
[hei, wid] = size(imSrc);
imDst = zeros(size(imSrc));
%cumulative sum over Y axis
imCum = cumsum(imSrc, 1);
%difference over Y axis
imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :);
imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :);
三、运行结果