一.最基本两个形态学运算----膨胀与腐蚀

啥叫形态学操作

形态学操作其实就是改变物体的形状,比如腐蚀就是”变瘦”,膨胀就是”变胖”,看下图就明白了:

(http://ex2tron.wang/opencv-python-erode-and-dilate/)

 【图像检测】教室人数统计系统matlab源码_图像处理

经验之谈:形态学操作一般作用于二值化图,来连接相邻的元素或分离成独立的元素。腐蚀和膨胀是针对图片中的白色(即前景)部分!

开/闭运算

先腐蚀后膨胀叫开运算(因为先腐蚀会分开物体,这样容易记住),其作用是:分离物体,消除小区域。

【图像检测】教室人数统计系统matlab源码_图像处理_02

经验之谈:很多人对开闭运算的作用不是很清楚,但看上图↑,不用怕:如果我们的目标物体外面有很多无关的小区域,就用开运算去除掉;如果物体内部有很多小黑洞,就用闭运算填充掉。

为什么有了膨胀腐蚀还要开运算闭运算呢?其实开闭运算最重要的一点就是,可以保持物体原有大小。然后一个是消除物体内部孔洞的另一个是增强物体之间连接点的。

其他形态学操作

  • 形态学梯度:膨胀图减去腐蚀图,dilation - erosion,这样会得到物体的轮廓:

【图像检测】教室人数统计系统matlab源码_图像处理_03


膨胀与腐蚀能够实现以下作用:

   1.消除噪声

   2.分割出独立的图像元素,在图像中连接相邻的元素

   3.寻找图像中的明显的极大值区域或者极小值区域

   4.求出图像的梯度

需要注意之处: 腐蚀和膨胀都是对图像的白色部分(高亮部分)而言。膨胀是图像中的高亮部分进行膨胀,类似于领域扩张,效果图拥有比原图更大的高亮区域;腐蚀是原图的高亮部分被腐蚀,类似于领域被蚕食,效果图拥有比原图更小的高亮区域。

从数学的角度来说,膨胀和腐蚀操作就是将图像与核进行卷积,核可以是任意形状和大小的。

【图像检测】教室人数统计系统matlab源码_matlab_04

膨胀到底是怎么实现的呢?求局部最大值。

原图与核进行卷积,将最大值赋予指定像素,从而使亮者更亮,效果就是亮的区域膨胀了。

再讲腐蚀(Erosion):

操作与膨胀相反,求局部最小值。

可以理解为,移动结构B(核),如果结构B与结构A的交集完全属于结构A的区域内,则保存该位置点,所有满足条件的点构成结构A被结构B腐蚀的结果。

【图像检测】教室人数统计系统matlab源码_图像处理_05

A被腐蚀后的结果貌似不对,好像应该是这样的:

【图像检测】教室人数统计系统matlab源码_图像处理_06


se_diamond =strel('diamond',4)创建一个菱形的结构元素,4是从结构化元素原点到其点的距离,必须为非负整数。

【图像检测】教室人数统计系统matlab源码_图像处理_07

function varargout = face_collection(varargin)
% FACE_COLLECTION MATLAB code for face_collection.fig
%      FACE_COLLECTION, by itself, creates a new FACE_COLLECTION or raises the existing
%      singleton*.
%
%      H = FACE_COLLECTION returns the handle to a new FACE_COLLECTION or the handle to
%      the existing singleton*.
%
%      FACE_COLLECTION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FACE_COLLECTION.M with the given input arguments.
%
%      FACE_COLLECTION('Property','Value',...) creates a new FACE_COLLECTION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before face_collection_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to face_collection_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 face_collection

% Last Modified by GUIDE v2.5 13-A-2021 12:23:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @face_collection_OpeningFcn, ...
                   'gui_OutputFcn',  @face_collection_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 face_collection is made visible.
function face_collection_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 face_collection (see VARARGIN)

% Choose default command line output for face_collection
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes face_collection wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = face_collection_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;


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 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 I
[x,y,c]=ginput(2);
if c==1
    BB=imcrop(I,[min(x(1),x(2)),min(y(1),y(2)),abs(x(2)-x(1)),abs(y(2)-y(1))]);
    BB=imresize(BB,[252 202]);
    axes(handles.axes2); % 处理后的图像放在axes2中
    imshow(BB)
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 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)
[filename, pathname] = uigetfile( ...
{'*.jpg;*.png;*.jpeg', 'Image Files (*.bmp;*.jpg;*.png;*.jpeg)'; ...
'*.*', 'All Files (*.*)'}, ...
'Pick an image');
if isequal(filename,0) || isequal(pathname,0),
return;
end
fpath=[pathname filename];      %将文件名和目录名组合成一个完整的路径
img=imread(fpath);
axes(handles.axes1)
imshow(img)
title('教室照片')
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.

【图像检测】教室人数统计系统matlab源码_matlab_08