The two most basic morphological operations —- expansion and corrosion

 

What is morphological operation

Morphologic manipulation is actually changing the shape of an object. For example, corroding means “getting thin” and swelling means “getting fat”, as you can see from the picture below:

(ex2tron. Wang/opencv – pyth…

 

A rule of thumb: Morphological operations are commonly used on binary diagrams to join adjacent elements or separate them into separate elements. Corrosion and swelling are targeted at the white (foreground) part of the image!

Open/close operation

Corrosion before expansion is called open operation (because corrosion will separate objects, so easy to remember), its role is: separate objects, eliminate small areas.

As a rule of thumb: Many people are not very clear about the function of open and closed operations, but don’t be afraid to look at the picture ↑ : If our target object has a lot of irrelevant small areas outside, we use open operations to remove them. If there are many small black holes inside the object, they are filled with closed operations.

Why do we need to open and close the operation when there is expansion corrosion? In fact, the most important thing about open and close is that you can keep things the same size. And then one is to eliminate the holes in the objects and one is to enhance the connections between the objects.

Other morphological operations

  • Morphological gradient: expansion diagram minus corrosion diagram,dilation - erosionTo get the outline of the object:

 


 

Expansion and corrosion can achieve the following effects:

1. Eliminate noise

2. Separate out independent image elements and connect adjacent elements in the image

3. Find the obvious maximum or minimum region in the image

4. Find the gradient of the image

Note: Both corrosion and swelling are for the white parts of the image (highlighted parts). Expansion is the expansion of the highlighted part of the image, similar to field expansion. The effect drawing has a larger highlighted area than the original image. Corrosion is the erosion of the highlighted area of the original image, similar to the erosion of the field. The effect image has a smaller highlighted area than the original image.

Mathematically, the expansion and corrosion operation involves convolving an image with a kernel, which can be of any shape or size.

So how does inflation work? Find the local maximum.

The original image is convolved with the kernel, and the maximum value is assigned to the specified pixel, so as to make the brighter one brighter. The effect is that the brighter area expands.

Erosion

The operation is the opposite of expansion, finding local minima.

It can be understood as moving structure B (core). If the intersection of structure B and structure A completely belongs to the region of structure A, this position point is saved. All points meeting the conditions constitute the result of structure A being corroded by structure B.

The result of A being corroded looks wrong, as if it should be like this:

 


Se_diamond =strel(‘diamond’,4) creates a diamond shaped structural element. 4 is the distance from the origin of the structured element to its point and must be a non-negative integer.

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('教室照片')
Copy the code

Complete code or simulation focus on avatar