[Fruit recognition] Fruit size recognition based on matlab GUI morphology [Contains Matlab source code 920]

[Fruit recognition] Fruit size recognition based on matlab GUI morphology [Contains Matlab source code 920]

1. Introduction

Mathematical morphology operations can be divided into binary morphology and gray morphology, and gray morphology is extended from binary morphology. Mathematical morphology has two basic operations, namely corrosion and expansion, and the combination of corrosion and expansion forms an opening operation and a closing operation.
The open operation is to first corrode and then expand, and the closed operation is to first expand and then corrode.

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

This formula means that structure B is used to corrode A. It should be noted that B needs to define an origin. [The process of B's movement is consistent with the process of convolution kernel movement, and the same convolution kernel and image overlap The calculation is the same afterwards] When the origin of B is translated to the pixel (x, y) of image A, if B is at (x, y), it is completely contained in the overlapping area of image A, (that is, 1 in B) The corresponding A image value at the element position of is also 1) The pixel (x, y) corresponding to the output image is assigned the value 1, otherwise the value is 0.
Let's look at a demo picture.

B moves on A in order (the same as the convolution kernel moves on the image, and then performs morphological operations on the coverage area of B), when the area covered by A is [1,1;1,1] or [1 ,0;1,1], (that is, '1' in B is a subset of the coverage area), the position corresponding to the output image will be 1.

2 Expansion
Roughly speaking, expansion will "enlarge" the scope of the target area, and merge the background points in contact with the target area into the target, so that the target boundary will expand to the outside. The function is to fill some holes in the target area and eliminate the small particle noise contained in the target area.

This formula expresses that structure B is used to expand A, and the origin of structure element B is translated to the position of the image pixel (x, y). If the intersection of B and A at the image pixel (x, y) is not empty (that is, at least one image value corresponding to A at the element position of B in B is 1), then the pixel corresponding to the output image ( x,y) is assigned a value of 1, otherwise it is assigned a value of 0.
The demo picture is:

3 Summary
That is to say, whether it is corroded or expanded, the structure element B is translated on the image like a convolution operation. The origin in the structure element B is equivalent to the core center of the convolution kernel, and the result is also stored On the element at the corresponding position of the nuclear center. It's just that the corrosion is that B is completely contained in the area covered by it, and B has an intersection with the area covered by it when it expands.

4 Gray Morphology
Before we talk about the gray value morphology, we make an agreement to mark the area of image A covered by structural element B as P (take the meaning of Part).

5 Corrosion of
gray-scale morphology Then the corrosion in gray-scale morphology is an operation similar to convolution. Use P to subtract the small rectangle formed by structural element B, and take the smallest value and assign it to the position of the corresponding origin.
Let's look at an example to deepen our understanding of grayscale morphology.
Suppose we have the following image A and structural element B:

the process of performing gray-scale morphological corrosion is as follows:

We display the output result of the first element of the output image in detail, that is, the position of 4 corresponding to the origin. The values of other elements of the output image are also obtained in this way. We will see that the area covered by B first is the subtracted matrix, and then min (the minimum value) is found in the difference matrix as the value of the corresponding position of the origin.

Expansion of gray-scale morphology
According to the description of corrosion above, we make the same description of expansion. The expansion in gray-scale morphology is an operation similar to convolution. Use P plus B, and then take this area The maximum value of is assigned to the position corresponding to the origin of the structure element B.


Here is also an explanation of the origin of the first element value of the output image.

The maximum value of the sum of the above matrix is 6, so assign 6 to the position corresponding to the origin of the structure element.

6 Summary
The concept of gray-scale morphology was introduced above, and here are some examples of their respective uses. Compared with the original image, because the result of the erosion makes each pixel smaller than before, it is suitable for removing peak noise. The result of gray value expansion will make each pixel larger than before, so it is suitable for removing low-pit noise.

2. the 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)" . % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help fruit_identify % Last Modified by GUIDE v2 .5 22 -May -2021 10 : 27 : 49 % 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; im=imread( 'pic.jpg' ); 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. function popupmenu1_Callback (hObject, eventdata, handles) % 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. function popupmenu1_CreateFcn (hObject, eventdata, handles) % 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 Copy code

3. running results


4. remarks

Version: 2014a