# [Fruit detection] Fruit size detection based on morphology matlab source code contains GUI

Posted by mark103 on Tue, 07 Sep 2021 21:39:55 +0200

## 1. Introduction

Mathematical morphology operations can be divided into binary morphology and gray morphology, which is extended from binary morphology.Mathematical morphology has two basic operations, corrosion and expansion, which combine to form open and closed operations.
Open operation means corrosion before expansion, close operation means expansion before corrosion.

1 Binary Morphology
Roughly speaking, corrosion can make the target area "smaller", which essentially causes the image's boundary to shrink and can be used to eliminate small and meaningless targets.The expression is:

This formula denotes that structure B corrodes A, and it is important to note that B needs to define an origin, [and B moves in the same way as the convolution core moves, as it is calculated after the convolution core overlaps the image]. When B's origin shifts to the cell (x,y) of image A, if B is completely contained in the area where image A overlaps at (x,y),(that is, the corresponding A image values for element positions of 1 in B are all 1) then the corresponding cells (x,y) of the output image are assigned a value of 1, otherwise they are assigned a value of 0.
Let's take a look at a presentation.

B moves sequentially on A (as the convolution kernel moves on the image, and then performs morphological operations on B's coverage), when its coverage of A is [1,1;1,1] or [1,0;When 1,1] (that is,'1'in B is a subset of the area covered), the corresponding output image location will be 1.

2 Expansion
Roughly speaking, expansion "enlarges" the range of the target area, merging background points that the target area touches into the target, and expanding the target boundary outward.The purpose is to fill some voids in the target area and eliminate small particle noise contained in the target area.

The subexpression represents expanding A with structure B, shifting the origin of structure element B to the image cell (x,y) position.If the intersection of B and A at the image cell (x,y) is not empty (that is, at least one image value of A at the element location of 1 in B) then the corresponding cell (x,y) of the output image is assigned a value of 1, otherwise it is assigned a value of 0.
The illustration shows:

3 Summary
That is, whether corroded or expanded, structural element B is translated on the image like a convolution operation, and the origin of structural element B corresponds to the nuclear center of the convolution core, and the result is also stored on the element corresponding to the nuclear center.It is simply that the corrosion is completely contained in the area covered by B, which intersects with the area covered by B when expanding.

4 Grayscale Morphology
Before describing the morphology of gray values, we made a convention that the area of image A covered by structural element B is marked as P (in the sense of Part).

5-Gray Morphological Corrosion
Corrosion in gray scale morphology is then a convolution-like operation, in which P is used to subtract the small rectangle formed by the structural element B, and the minimum value is assigned to the location of the corresponding origin.
Let's take an example to deepen our understanding of gray morphology.
Suppose we have the following image A and structure element B:

The process of gray scale morphological corrosion is as follows:

We show the output of the first element of the output image, that is, the location of the origin corresponding to 4.The values of the other elements of the output image are also derived in this way.We will see that the first area covered by B is the subtraction matrix, and then min (minimum) is computed in its difference matrix as the value of the corresponding position at the origin.

Expansion of Grayscale Morphology
Based on the above description of corrosion, we describe expansion in the same way. Expansion in grayscale morphology is a convolution-like operation, where P is added to B, and then the maximum value in this area is assigned to the position corresponding to the origin of structural element B.

The origin of the first element value of the output image is also explained here.

The sum of the above matrices is maximized to 6, so 6 is assigned to the position corresponding to the origin of the structural element.

6 Summary
The concepts of grayscale morphology are introduced above, and their respective uses are described here.Compared with the original image, the result of corrosion makes each cell smaller than before, so it is suitable for removing peak noise.The result of expanding gray values makes each cell larger than before, so it is suitable for removing low valley noise.

## Source Code

```function varargout = fruit_identify(varargin)
% FRUIT_IDENTIFY M-file for fruit_identify.fig
%      FRUIT_IDENTIFY, by itself, creates a new FRUIT_IDENTIFY or raises the existing
%      singleton*.
%
%      H = FRUIT_IDENTIFY returns the handle to a new FRUIT_IDENTIFY or the handle to
%      the existing singleton*.
%
%      FRUIT_IDENTIFY('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FRUIT_IDENTIFY.M with the given input arguments.
%
%      FRUIT_IDENTIFY('Property','Value',...) creates a new FRUIT_IDENTIFY or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before fruit_identify_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to fruit_identify_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%

% Edit the above text to modify the response to help fruit_identify

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

% Choose default command line output for fruit_identify
handles.output = hObject;
axes(handles.axes1)
imshow(im)

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = fruit_identify_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 popupmenu1.
% hObject    handle to popupmenu1 (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 popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1

% --- Executes during object creation, after setting all properties.
% hObject    handle to popupmenu1 (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 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

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

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

% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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

```

Version: 2014 a