matlab形态学处理——膨胀腐蚀的原理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab形态学处理——膨胀腐蚀的原理相关的知识,希望对你有一定的参考价值。
膨胀和腐蚀(imerode和imdilate)照理说应该是二值运算,为什么对灰度图像也起作用啊?而且运算后的图像仍然是灰度图像,具有0-255的灰阶,这是为什么啊?
在matlab定义里面,这两个函数本来就是能够作用于灰度图像的,看帮助文件你也能看到作用与灰度图的例子
当然,膨胀和腐蚀多用于处理二值图像
先把输入的图像二值化成为二值图像,在用这两个函数就可以了 参考技术A matlab中灰度膨胀函数为 imdilate()
比如:
I= imread('circles.png');
subplot(121);imshow(I); title('原图像');
se = strel('disk',10);
I2 = imdilate(I,se);
subplot(122);imshow(I2);title('膨胀图像');
灰度腐蚀用函数 imerode()
比如:
I = imread('circles.png');
subplot(121);imshow(I); title('原图像');
se = strel('disk',11);
I2 = imerode(I,se);
subplot(122);imshow(I2);title('腐蚀图像'); 参考技术B Matlab的imopen和imclose就是开闭运算的函数了。
疲劳检测基于形态学实现疲劳检测matlab源码
形态学通常表示生物学的一个分支,研究动植物的形态和结构。图像中的形态学是数学形态学。简单来讲就是基于形状的一系列图像处理操作。
基本运算包括:二值腐蚀和膨胀、二值开闭运算、骨架抽取、极限腐蚀、击中击不中变换、形态学梯度、Top-hat变换、颗粒分析、流域变换、灰值腐蚀和膨胀、灰值开闭运算、灰值形态学梯度。
膨胀与腐蚀
功能:
- 消除噪声;
- 分割出独立的图像元素,在图像中链接相邻的元素;
- 寻找图像中明显的极大值和极小值区域;
- 求图像的梯度;
膨胀
求局部最大值的操作。将与物体接触的所有背景点合并到该物体中,使边界向外部扩张的过程。可以用来填补物体中的空洞。
核可以是任何形状,有一个单独定义的参考点,锚点。
膨胀就是计算核覆盖的区域的像素点的极大值,把这个极大值赋值给参考点指定的像素,使图像的高亮区域逐渐增长。
函数:void dilate
腐蚀
消除边界点,使边界向内部收缩的过程。可以用来消除小且无意义的物体。
与膨胀相反,求局部最小值的操作。
函数:void erode
开运算、闭运算、形态学梯度、顶帽、黑帽
形态学的高级形态,都是建立在腐蚀和膨胀这两个基本操作之上的。
开运算
先腐蚀后膨胀,具有消除亮度较高的细小区域,在纤细点分离物体,对于较大物体,可以在不明显改变其面积的情况下平滑其边界等作用。
闭运算
先膨胀后腐蚀,它具有填充白色物体内细小黑色区域、连接临近物体的作用,也可以在不明显改变其面积的情况下平滑边界。
形态学梯度
膨胀图和腐蚀图之差,对二值图像操作能将团块的边缘突出出来。
顶帽
原图像与开运算的结果图之差,由于开运算放大了裂缝或者局部低亮度的区域,因此原图中减去开运算后的图,得到的效果图突出了比原图轮廓周围的区域更明亮的区域。
用来分离比邻近点亮一些的斑块。
黑帽
闭运算的结果图与源图像之差。
黑帽运算后的效果图突出了比原图轮廓周围的区域更暗的区域。所以黑帽用来分离比邻近点暗一点的斑块
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI.M with the given input arguments.
%
% GUI('Property','Value',...) creates a new GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_OpeningFcn, ...
'gui_OutputFcn', @GUI_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 GUI is made visible.
function GUI_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 GUI (see VARARGIN)
% Choose default command line output for GUI
handles.output = hObject;
handles.output = hObject;
ha=axes('units','normalized','pos',[0 0 1 1]);
uistack(ha,'down');
ii=imread('背景图.jpg');
image(ii);
colormap gray
set(ha,'handlevisibility','off','visible','off');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_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)
% 显示figure标记
flag = 1;
% 获取图片库文件列表
files = ls('video_images/*.jpg');
%N = size(files, 1);
N=15;
kk=0
for i = 1 : N
file = fullfile(pwd, 'video_images', files(i, :));
Img = imread(file);
% 图像预处理
[Im1, rectValide] = GetValideImage(Img, 0);%自定义的函数
figure(99)
imshow(Im1)
imwrite(Im1,'ceshi.jpg')
[m n]=size(Im1)
% 人脸区域定位
[Im2, bw, rectFace] = GetFaceAreaImg(Im1, 0);
% 人眼区域定位
[Im3, rectEye] = GetEyeAreaImg(Im2, 0);
% 人眼精确定位
[Im4, bwf, rect, nv] = GetEyeAccurateImg(Im3, 0);
% 统计特征信息
Nv(i).file = file;%%帧图像名称路径
Nv(i).nv = nv;%921
Nv(i).rectEye = rectEye;%8 31 163 40
Nv(i).rectEyes = rect;%1x4 1x4
Nv(i).rectFace = rectFace;%90.5000 153.0000 198 192
Nv(i).rectValide = rectValide;%301 1 359 600
imshow(Img, []);%%%%原图
hold on;
% 人脸区域
r_face = [Nv(i).rectFace(1)+Nv(i).rectValide(1) Nv(i).rectFace(2)+Nv(i).rectValide(2) Nv(i).rectFace(3:4)];
% 人眼区域
r_eye = [Nv(i).rectEye(1)+r_face(1) Nv(i).rectEye(2)+r_face(2) Nv(i).rectEye(3:4)];
% 人眼精确区域
r_eyes1 = [Nv(i).rectEyes{1}(1)+r_eye(1) Nv(i).rectEyes{1}(2)+r_eye(2) Nv(i).rectEyes{1}(3:4)];
r_eyes2 = [Nv(i).rectEyes{2}(1)+r_eye(1) Nv(i).rectEyes{2}(2)+r_eye(2) Nv(i).rectEyes{2}(3:4)];
rectangle('Position', r_face, 'EdgeColor', 'y', 'LineWidth', 2);
rectangle('Position', r_eyes1, 'EdgeColor', 'r', 'LineWidth', 1);
rectangle('Position', r_eyes2, 'EdgeColor', 'r', 'LineWidth', 1);
title(Nv(i).status);
hold off;
pause(6);%间隔时间
end
f1=(N-kk)/N
if f1>0.12
fprintf('疲劳')
set(handles.edit3,'string','检测结果为:疲劳')
[filex,fs]=audioread('警报.wav');
sound(filex,fs);
else
fprintf('非疲劳')
set(handles.edit3,'string','检测结果为:非疲劳')
end
% 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)
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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 edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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
% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox2
% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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 edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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
% --- Executes on selection change in listbox3.
function listbox3_Callback(hObject, eventdata, handles)
% hObject handle to listbox3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox3 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox3
% --- Executes during object creation, after setting all properties.
function listbox3_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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
% --- 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)
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
%% 将视频逐帧保存为jpg图片
obj = VideoReader(uigetfile('*.avi','选择视频'));%输入视频位置
setappdata(0,'obj',obj);%设置全局变量
Show_Frames=read(obj,1);%显示第一帧作为封面
axes(handles.axes1);
imshow(Show_Frames);
set(handles.listbox1,'String','分帧开始...');
prompt={'输入图片名是几位数:'};
defans={'2'};%默认两位数
p=inputdlg(prompt,'输入位数',1,defans);
numzeros=str2num(p{1});
nz = strcat('%0',num2str(numzeros),'d');
numFrames = obj.NumberOfFrames;% 帧的总数
for k = 1 :5% 读取前15帧 numFrames
frame = read(obj,k);%读取第几帧
id=sprintf(nz,k);
imwrite(frame,strcat('video_images/',id,'.jpg'),'jpg');% 保存帧
end
set(handles.listbox1,'String','分帧结束');
% 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)
完整代码或者仿真咨询添加QQ1575304183
以上是关于matlab形态学处理——膨胀腐蚀的原理的主要内容,如果未能解决你的问题,请参考以下文章
MATLAB教程案例32基于matlab的交通标志检测分割算法的仿真——形态学处理,膨胀,腐蚀,形状检测,颜色模型,小波滤波等知识的综合应用