【图像识别】基于形态学算法实现道路缺陷的自动识别matlab源码含GUI
一般图像处理是针对图像做形状的改变,而形态处理则是对图像进行结构性的改变,而常见的形态处理就是针对二值图像(图像中的任何像素灰度值不是0就是255)的膨胀(Dilation)、腐蚀(Erosion)、开运算(先腐蚀再膨胀)、闭运算(先膨胀再腐蚀)。
注意:如果对灰度图进行膨胀、腐蚀等,会起到增强图像对比度的效果
一、理论:
膨胀(dilation、dilation_rectangle1、dilation_circle)-增加像素:
把结构元素B的中心元素(origin)扫描图像的每一个像素,用结构元素与其覆盖的二值图像做“或”操作,即结构元素与其覆盖的二值图像的重合部分只要有黑色(0),则结构元素的中心点所在的二值图像灰度值就为黑色(0);
如图所示:左边是需要被处理的图像X(二值图像,我们针对的是黑点),中间是结构元素B,右边是膨胀处理之后的图像。膨胀的方法是:拿B的中心点在X上从左到右,从上到下滑动,只要B上的黑点与X上的黑点有重合,则就将B的中心点所在位置的图像像素值置为黑;右边是膨胀后的结果。可以看出,它包括X的所有范围,就像X膨胀了一圈似的。
腐蚀(erosion、erosion_rectangle1、erosion_circle)-减少像素:
把结构元素B的中心元素(origin)扫描图像的每一个像素,用结构元素与其覆盖的二值图像做“与”操作,即结构元素与其覆盖的二值图像的重合部分全都为黑色(0),则结构元素的中心点所在的二值图像灰度值才为黑色(0);换而言之,两者重合部分只要有白色(1)则结构元素的中心点所在的二值图像灰度值就为白色(1)。
如图所示:左边是被处理的图像X(二值图像,我们针对的是黑点),中间是结构元素B,那个标有origin的点是中心点,即当前处理元素的位置。腐蚀的方法是:拿B的中心点在X上从左到右,从上到下滑动,如果结构元素B与二值图像X有空白交集,则将B的中心点所在的二值图像黑点去掉(变为白—1)。
膨胀和腐蚀主要用途:
删除噪声
图中个别原素的隔离与不同原素的连接
寻找图中浓密点与空洞
注:对标准矩形或圆形进行等比例扩大或缩小要用上面的算子,扩展比例为3:1,例:dilation_rectangle1 (RegionTrans, RegionDilation, 3, 3) 为对标准矩形区域RegionTrans的长和宽方向各增加一个像素
开运算(opening_rectangle1等)-减少像素:先进行腐蚀,然后再膨胀
如图所示:左边是被处理的图像X(二值图像,我们针对的是黑点),右边是结构元素B,下面的两幅图中左边是腐蚀后的结果;右边是在此基础上膨胀的结果。可以看到,原图经过开运算后,一些孤立的小点被去掉了。一般来说,开运算能够去除孤立的小点、毛刺和小桥(即连通两块区域的小点),而总的位置和形状不变。这就是开运算的作用。要注意的是,如果B是非对称的,进行开运算时要用B的对称集Bv膨胀,否则,开运算的结果和原图相比要发生平移。
闭运算(closing_rectangle1等)-增加像素:先进行膨胀,然后进行腐蚀
左边是被处理的图像X(二值图像,我们针对的是黑点),右边是结构元素B,下面的两幅图中左边是膨胀后的结果,右边是在此基础上腐蚀的结果可以看到,原图经过闭运算后,断裂的地方被弥合了。一般来说,闭运算能够填平小湖(即小孔),弥合小裂缝,而总的位置和形状不变,比如在OCR字符识别、车牌识别等地方,需要对激光点打印的字体或有一些缺陷的字体进行闭运算,实现连接。同样要注意的是,如果B是非对称的,进行闭运算时要用B的对称集Bv膨胀,否则,闭运算的结果和原图相比要发生平移。
注意:
1)要与原图像的像素点作比较,新增或去掉的像素点不算。
2)上面我们介绍的是对二值化图像的处理。如果是对灰度图像进行形态学处理,则膨胀和闭运算会使灰度图像变亮,腐蚀和开运算会使灰度图像变暗,这点大家注意下。
总结:
1)结构元素的形状(圆形,矩形)一般随着要处理的区域形状(圆形、矩形等)的不同而有所不同。比如圆形的结构元素对圆形区域的腐蚀、膨胀效果会更好。
2)结构元素的半径或边长越大,则膨胀、腐蚀效果越强。
3)如果想增加像素,用膨胀或闭运算;如果想减少像素,用腐蚀或开运算。区别在于膨胀和腐蚀的度要大于闭运算和开运算。
4)形态学对二值化区域是改变形状,而对灰度图像是改变图像明暗。
5)开运算一般适合去除边缘毛刺、孔洞等,如果需要对标准圆形或矩形区域四周进行等比例扩大或缩小,可以使用算子dilation_rectangle1、erosion_rectangle1、dilation_circle、erosion_circle算子。
部分代码
function varargout = x(varargin)
% X M-file for x.fig
% X, by itself, creates a new X or raises the existing
% singleton*.
%
% H = X returns the handle to a new X or the handle to
% the existing singleton*.
%
% X('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in X.M with the given input arguments.
%
% X('Property','Value',...) creates a new X or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before x_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to x_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 x
% Last Modified by GUIDE v2.5 09-May-2015 21:14:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @x_OpeningFcn, ...
'gui_OutputFcn', @x_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 x is made visible.
function x_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 x (see VARARGIN)
% Choose default command line output for x
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes x wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = x_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 pushbutton_DK.
function pushbutton_DK_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_DK (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 f_name;
global im_wide;
global im_heigh;
[filename, pathname]=...
uigetfile({'*.jpg'},'选择图片');
str = [pathname, filename];
f_name=filename;
im = imread(str);
[im_wide im_heigh]=size(im);
axes( handles.axes_orig);
imshow(im);title('输入图片')
set(handles.edit_TP,'string',f_name);
str1=int2str(im_wide);
str2='X';
str3=int2str(im_heigh);
Str=[str1,str2,str3];
set(handles.edit_DX,'string',Str);
% --- Executes on button press in pushbutton_CZ.
function pushbutton_CZ_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_CZ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im;
axes( handles.axes_orig);
imshow(im);title('重载图片')
% --- Executes on button press in pushbutton_XS.
function pushbutton_XS_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_XS (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 BW2;
M = imresize ( im ,[256 ,256 ]);%将图像调整256 ×256 ,为了显示方便
[h,w,ii]=size(M);
if ii==3
YT = rgb2gray(M);%将RGB 格式转换为灰度图像
else
YT=M;
end
YT(200:256,:)=0;
Z1 = wiener2(YT ,[5 ,5 ]);
%2 小波分析去噪%
%使用sym4 小波,设定全局阈值去噪%
[ THR ,SORH ,KEEPAPP] = ddencmp('den','wv', Z1);
X = wdencmp( 'gbl',Z1, 'sym4',2 ,THR ,SORH ,KEEPAPP);
[Z1,s] = wavedec2 (X ,2, 'sym4');
%进行二层小波分解
len = length ( Z1 );
%处理分解系数,突出轮廓,弱化细节%
for i = 1 :len
if (Z1(i) < 500)
Z1(i) = 0.8*Z1(i);
else
Z1(i) = 0.7*Z1(i);
end
end
Z = waverec2 (Z1 ,s, 'sym4'); %分解系数重构
[cA1,cH1,cV1,cD1]=dwt2(Z,'bior3.7');
A1=upcoef2('a',cA1,'bior3.7',1);
H1=upcoef2('h',cH1,'bior3.7',1);
V1=upcoef2('v',cV1,'bior3.7',1);
D1=upcoef2('d',cD1,'bior3.7',1);
Z1=uint8(V1);
level = graythresh(Z1);
BW = im2bw(Z1,level);
se=[1;1;1];
% Hints: get(hObject,'String') returns contents of edit_TP as text
% str2double(get(hObject,'String')) returns contents of edit_TP as a double
% --- Executes during object creation, after setting all properties.
function edit_TP_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_TP (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_DX_Callback(hObject, eventdata, handles)
% hObject handle to edit_DX (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_DX as text
% str2double(get(hObject,'String')) returns contents of edit_DX as a double
% --- Executes during object creation, after setting all properties.
function edit_DX_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_DX (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_LF_Callback(hObject, eventdata, handles)
% hObject handle to edit_LF (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_LF as text
% str2double(get(hObject,'String')) returns contents of edit_LF as a double
% --- Executes during object creation, after setting all properties.
function edit_LF_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_LF (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_LFC_Callback(hObject, eventdata, handles)
% hObject handle to edit_LFC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_LFC as text
% str2double(get(hObject,'String')) returns contents of edit_LFC as a double
% --- Executes during object creation, after setting all properties.
function edit_LFC_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_LFC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_YW_Callback(hObject, eventdata, handles)
% hObject handle to edit_YW (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_YW as text
% str2double(get(hObject,'String')) returns contents of edit_YW as a double
% --- Executes during object creation, after setting all properties.
function edit_YW_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_YW (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_PG_Callback(hObject, eventdata, handles)
% hObject handle to edit_PG (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_PG as text
% str2double(get(hObject,'String')) returns contents of edit_PG as a double
% --- Executes during object creation, after setting all properties.
function edit_PG_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_PG (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_MJ_Callback(hObject, eventdata, handles)
% hObject handle to edit_MJ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_MJ as text
% str2double(get(hObject,'String')) returns contents of edit_MJ as a double
% --- Executes during object creation, after setting all properties.
function edit_MJ_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_MJ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_ZC_Callback(hObject, eventdata, handles)
% hObject handle to edit_ZC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_ZC as text
% str2double(get(hObject,'String')) returns contents of edit_ZC as a double
% --- Executes during object creation, after setting all properties.
function edit_ZC_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_ZC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_JXC_Callback(hObject, eventdata, handles)
% hObject handle to edit_JXC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_JXC as text
% str2double(get(hObject,'String')) returns contents of edit_JXC as a double
% --- Executes during object creation, after setting all properties.
function edit_JXC_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_JXC (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_JXK_Callback(hObject, eventdata, handles)
% hObject handle to edit_JXK (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_JXK as text
% str2double(get(hObject,'String')) returns contents of edit_JXK as a double
% --- Executes during object creation, after setting all properties.
function edit_JXK_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_JXK (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_LWC1_Callback(hObject, eventdata, handles)
% hObject handle to edit_LWC1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_LWC1 as text
% str2double(get(hObject,'String')) returns contents of edit_LWC1 as a double
% --- Executes during object creation, after setting all properties.
function edit_LWC1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_LWC1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_LWK1_Callback(hObject, eventdata, handles)
% hObject handle to edit_LWK1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_LWK1 as text
% str2double(get(hObject,'String')) returns contents of edit_LWK1 as a double
% --- Executes during object creation, after setting all properties.
function edit_LWK1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_LWK1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
》》 喜欢可以关注我