一、Sobel、Prewitt、Canny算子简介
1 Sobel算子边缘检测算法
传统Sobel算子是边缘检测中常用的梯度幅度检测算子,该算子首先使用3×3的卷积模板对检测图像进行加权平均或邻域平均,然后通过一阶微分计算来检测图像的边缘。假设f(x,y) 表示为一幅函数图像,它在点f(x,y)处的梯度是一个矢量,定义为:
其中ᐁf(x,y)表示梯度的模,其值可按下式计算:
Sobel算子包含水平和垂直两个方向的卷积模板,如图1所示。
图1 传统Sobel算子模板
Sobel算子的具体步骤如下:
1)将水平和垂直方向的模板从左到右、从上到下遍历图像,模板的中心点对应于图像中相应的像素点。
2)对图像所有像素点组成的每个模板进行离散卷积运算。
3)将两个模板卷积运算结果的最大值替换中心像素点的灰度值,用pmax表示。
4)取恰当的阈值T进行二值化处理,若pmax≥T,则判定该像素点为图像的边缘,反之判定该像素点为背景区域。
2 Prewitt算法
它属于一阶微分算法的边缘检测,所用的是像素点四周邻点的灰度值,当边缘处的数值最大时,对边缘进行检测,将不合格的部分删掉,有利于图像噪点更加滑顺。它通过对图像空间的两个模板和图像进行邻域卷积运算,两个模板分辨对水平和垂直边缘实施检测。
对数字图像f(x,y),Prewitt算法的定义如下:
经典Prewitt算法理论内容是:如果像素点的灰度新值不小于阈值,则这些像素点都属于边缘点。也就是说,选用合适的阈值T,如果P(i,j)≥T,可得(i,j)就是边缘点,P(i,j)边缘图像。以上判断缺乏科学的依据,导致不能正确判断边缘点。通常情况下,噪声点有较高的灰度值,尤其是边缘点的幅值很小,会导致边缘不复存在。
3 Canny边缘检测算法
Canny边缘检测算法是由国外学者John F. Canny在1986年提出的,创立了边缘检测计算理论,解释如何实现图像的边缘检测。Canny算法定义了最优边缘准则满足的3个条件:1) 最优检测。算法能够尽可能多地标识出图像中的实际边缘,漏检真实边缘的概率和误检非边缘的概率都尽可能小;2) 最优定位准则。检测到的边缘点的位置距离实际边缘点的位置最近,或者是由于噪声影响引起检测出的边缘偏离物体的真实边缘的程度最小;3) 检测点与边缘点一一对应。算子检测的边缘点与实际边缘点应该是一一对应。
Canny边缘检测算法分为5个基本步骤:1) 应用高斯滤波平滑图像,消除噪声对边缘检测的影响;2) 找寻图像的强度梯度,通常采用一阶有限差分计算图像的梯度幅值和方向;3) 应用非极大值抑制消除边缘误检;4) 采用双阈值算法决定潜在的边界;5) 利用滞后技术来跟踪边界。
二、部分源代码
function varargout = guipic(varargin)
% GUIPIC MATLAB code for guipic.fig
% GUIPIC, by itself, creates a new GUIPIC or raises the existing
% singleton*.
%
% H = GUIPIC returns the handle to a new GUIPIC or the handle to
% the existing singleton*.
%
% GUIPIC('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUIPIC.M with the given input arguments.
%
% GUIPIC('Property','Value',...) creates a new GUIPIC or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before guipic_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to guipic_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 guipic
% Last Modified by GUIDE v2.5 13-Mar-2021 17:27:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @guipic_OpeningFcn, ...
'gui_OutputFcn', @guipic_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 guipic is made visible.
function guipic_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 guipic (see VARARGIN)
% Choose default command line output for guipic
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes guipic wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = guipic_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 ok.
function ok_Callback(hObject, eventdata, handles)
% hObject handle to ok (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im
%选择图片路径
[filename,pathname]=...
uigetfile({'*.jpg';'*.bmp';'*.gif'},'选择图片');
%合成路径加文件名
str=[pathname filename];
%读取图片
im=imread(str);
%使用第一个AXES
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)
close(gcf)
% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uipanel1
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
global im
%拿到选择按钮的名称
str=get(hObject,'string');
axes=(handles.axes1);
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.
[5]胡文文,周日贵,范萍,李尧翀.基于Canny算法的量子图像边缘检测[J].郑州大学学报(理学版). 2020,52(04)
[6]刘源,夏春蕾.一种基于Sobel算子的带钢表面缺陷图像边缘检测算法[J].电子测量技术. 2021,44(03)
[7]翁振斌.基于Prewitt算法的图像边缘检测技术在瓷砖生产中的应用[J].九江学院学报(自然科学版). 2019,34(03)