[image processing] GUI based image morphological processing [Matlab 1287]

Posted by apsomum on Thu, 16 Dec 2021 06:20:25 +0100

1, Introduction to morphology

1 Overview
1.1 basic idea
The corresponding shape in the image is measured and extracted by the structural elements with a certain shape, so as to achieve the purpose of image analysis and recognition

1.2 basic operation
Expansion, corrosion, opening and closing operation

1.3 fundamentals of Mathematics
set theory

Structural elements:

The original image needs to be expanded so that when the structural element is located at the edge of the original image, the expanded part can cover the whole structural element.

2 basic operations of binary image morphology
2.1 corrosion operation
All structural elements B are located in the corresponding displacement z in A, so the boundary will be weakened

effect:

Is an operation of shrinking or thinning.

2.1 expansion operation
The displacement z corresponding to the intersection of structural elements B and A will expand the boundary

effect:

Is a growing or coarsening operation.
Note: corrosion and expansion are dual (i.e. inverse after corrosion on the foreground = result of expansion on the background)

2.3 opening operation
First corrosion and then expansion, smooth the contour of the object, disconnect the narrow connecting strip and eliminate the thin protrusion
[external link picture transfer failed. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-k35subpp-1626615953008)( https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0959bcda0e5c49a6801fd0c7c94e20e7 ~tplv-k3u1fbpfcp-zoom-1. image)]

2.4 closing operation
Expansion before corrosion can also smooth the outline of the object, but it will bridge narrow discontinuities and slender strips

Effect: the outward angle remains unchanged, and the inward angle becomes round to fill the gap - "plus"

2.5 comparison of opening operation and closing operation
2.5. 1 Duality (negative result after closed operation = open operation against background)

2.5. 2 nature
After the operator is applied once, there is no change when it is used again

2.5. 3 comparison effect

2.5. 4 Application
Open operation to take out small targets in the image (denoising)
Close operation connection target (eliminate small interval)

2.6 hit miss conversion
Purpose: shape detection (to detect the position of a specific shape, each object is required to be surrounded by a background at least one pixel wide)
Match foreground with background


3 binary image morphological algorithm
3.1 boundary extraction

Foreground - corroded foreground

3.2 hole filling

Find A starting point (located in the hole), expand continuously and reverse A until there is no change

3.3 extraction of connected components

Through the above iterative formula, the whole Unicom pattern can be obtained from a known point of the Unicom pattern

3.4 convex shell
Convex: set A is said to be convex if the connecting line connecting any two points in set A is in A
Convex hull: the convex hull H of any set s is the minimum convex set contained in S

Where X0 is A, Bi is structural elements in different directions, A Bi can make A Di, and the convex shell of A intersects all Di


result:

3.5 refinement
A subtracts the part of pixels matched by hit miss transformation between a and struct B

Where B is the sequence of structural elements


Steps:
A is refined by B1~Bn in order, and then returns to B1. It is refined in order from B1 until convergence

3.6 coarsening
Refined morphological duality
A and the sum of the pixels matched by hit miss transformation between a and struct B


3.7 framework
The skeleton of A can be expressed by corrosion and open operation

Where k represents K times of corrosion on A, and K is the last iteration before A is corroded into an empty set

3.8 tailoring
It is a supplement to the thinning processing and skeleton rendering algorithm, because these processes will retain the additional parts, which should be clear and clean, so it needs to be trimmed to take out the parasitic redundant parts

4 gray level morphology
4.1 operation
Corrosion: the output image darkens and bright details are reduced
Bloat: the output image brightens and the dark details are reduced
4.2 effect

On operation: remove small bright details
Close operation: remove small dark details

4.3 algorithm
Image smoothing: first on (suppress bright details) and then off (suppress dark details)
Morphological gradient: expansion corrosion
Top hat transform: correct uneven lighting and enhance shadow details

2, Partial source code

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

% Last Modified by GUIDE v2.5 31-Aug-2021 23:16:44

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

% Update handles structure
guidata(hObject, handles);
guidata(hObject, handles);
movegui(gcf,'center');

% --- Outputs from this function are returned to the command line.
function varargout = shape_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 selection change in jiegouyuansu1.
function jiegouyuansu1_Callback(hObject, eventdata, handles)
% hObject    handle to jiegouyuansu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h1=findobj('tag','jiegouyuansu1');
if get(h1,'value')==1
   set(handles.text4,'Visible','on');
   set(handles.text4,'String','Side length W');
   set(handles.text5,'Visible','off');
   set(handles.text6,'Visible','off');
   set(handles.edit2,'Visible','on');
   set(handles.edit2,'String','3');
   set(handles.edit3,'Visible','off');
   set(handles.edit4,'Visible','off');
elseif get(h1,'value')==2
   set(handles.text4,'Visible','off');
   set(handles.text5,'Visible','on');
   set(handles.text5,'String','long M');
   set(handles.text6,'Visible','on');
   set(handles.text6,'String','wide N');
   set(handles.edit2,'Visible','off');
   set(handles.edit3,'Visible','on');
   set(handles.edit3,'String','6');
   set(handles.edit4,'Visible','on');
   set(handles.edit4,'String','3');
elseif get(h1,'value')==3
   set(handles.text4,'Visible','on');
   set(handles.text4,'String','radius R');
   set(handles.text5,'Visible','off');
   set(handles.text6,'Visible','off');
   set(handles.edit2,'Visible','on');
   set(handles.edit2,'String','3');
   set(handles.edit3,'Visible','off');
   set(handles.edit4,'Visible','off');
elseif get(h1,'value')==4
   set(handles.text4,'Visible','off');
   set(handles.text5,'Visible','on');
   set(handles.text5,'String','length LEN');
   set(handles.text6,'Visible','on');
   set(handles.text6,'String','angle DEG');
   set(handles.edit2,'Visible','off');
   set(handles.edit3,'Visible','on');
   set(handles.edit3,'String','9');
   set(handles.edit4,'Visible','on');
   set(handles.edit4,'String','45');
elseif get(h1,'value')==5
   set(handles.text4,'Visible','on');
   set(handles.text4,'String','radius R');
   set(handles.text5,'Visible','off');
   set(handles.text6,'Visible','off');
   set(handles.edit2,'Visible','on');
   set(handles.edit2,'String','3');
   set(handles.edit3,'Visible','off');
   set(handles.edit4,'Visible','off');
end
% Hints: contents = get(hObject,'String') returns jiegouyuansu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from jiegouyuansu1


% --- Executes during object creation, after setting all properties.
function jiegouyuansu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to jiegouyuansu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 during object creation, after setting all properties.
function caozuoleixing_CreateFcn(hObject, eventdata, handles)
% hObject    handle to caozuoleixing (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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



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



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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 queding.
function queding_Callback(hObject, eventdata, handles)
% hObject    handle to queding (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global f;
R=get(handles.edit2, 'String');
R=str2num(R);
LEN=get(handles.edit3, 'String');
LEN=str2num(LEN);
M=LEN;
DEG=get(handles.edit4, 'String');
DEG=str2num(DEG);
N=DEG;
jgys= get(handles.jiegouyuansu1, 'Value');
switch jgys

    case 1
        se=strel('square',R);
    case 2
        se=strel('rectangle',[M N]);
    case 3
        se=strel('disk',R);
    case 4
        se=strel('line',LEN,DEG);
    case 5
        se=strel('diamond',R);
end
czlx = get(handles.caozuoleixing1, 'Value');
switch czlx
    case 1
        f0=imdilate(f,se);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('Expanded image');
        hold;
        hold;
    case 2
        f0=imerode(f,se);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('Image after corrosion');
        hold;
        hold;
    case 3
        f0=imopen(f,se);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('Image after open operation');
        hold;
        hold;
    case 4
        f0=imclose(f,se);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('Image after closed operation');
        hold;
        hold;
    case 5
        B1=strel([0 0 0;0 1 1;0 1 0]);
        B2=strel([1 1 1;1 0 0 ;1 0 0]);
        f0=bwhitmiss(f,B1,B2);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('Image after closed operation');
        hold;
        hold;
end




% --- Executes on selection change in caozuoleixing.
function caozuoleixing_Callback(hObject, eventdata, handles)
% hObject    handle to caozuoleixing (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns caozuoleixing contents as cell array
%        contents{get(hObject,'Value')} returns selected item from caozuoleixing




% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global f;
file = uigetfile('*.tif;*.jpg');
if ~isequal(file, 0)
    f=imread(file);
end
axes(handles.axes1);
cla;
imshow(f);
title('Original image');
hold;
hold;


% --------------------------------------------------------------------
function Untitled_3_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename ,pathname]=uiputfile({'*.mat','MAT-files(*.mat)'},'preservation');
str=strcat(pathname,filename);
a='shape.fig';
save(char(str), 'a')

% --------------------------------------------------------------------
function Untitled_4_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global f;

button2=questdlg('Are you sure you want to quit?','Exit program','Yes','No','Yes');
if strcmp(button2,'Yes')
close;
f=0;
end;

3, Operation results





4, matlab version and references

1 matlab version
2014a

2 references
[1] Cai Limei MATLAB image processing -- theory, algorithm and example analysis [M] Tsinghua University Press, 2020
[2] Yang Dan, Zhao Haibin, long Zhe Detailed explanation of MATLAB image processing example [M] Tsinghua University Press, 2013
[3] Zhou pin MATLAB image processing and graphical user interface design [M] Tsinghua University Press, 2013
[4] Liu Chenglong Proficient in MATLAB image processing [M] Tsinghua University Press, 2015

Topics: MATLAB Pytorch Deep Learning image processing