A list,
Fruit and vegetable recognition based on MATLAB GUI gray scale + binarization + corrosion + histogram processing
Ii. Source code
function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property'.'Value',...). creates anew UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name orinvalid value makes property application % stop. All inputs are passed to untitled_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 untitled
% Last Modified by GUIDE v2. 5 15-Jan- 2017. 01:03:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_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 untitled is made visible.
function untitled_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 untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_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 button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I FilledLabel HSV MeanHue Ecllipseratio Label num Premeter Area
for i = 1 : num
Premeter(i) = 0;
end
[row,col] = size(Label);
for i = 1 : row
for j = 1 : col
if(Label(i,j) > 0)
Premeter(Label(i,j)) = Premeter(Label(i,j)) + 1; FilledLabel=imfill(Label,'holes'); % fills the area of the graph enclosed in the middle of the marked boundaryfor i = 1 : num
Area(i) = 0;
end
[row,col] = size(FilledLabel);
for i = 1 : row
for j = 1 : col
if(FilledLabel(i,j) > 0)
Area(FilledLabel(i,j)) = Area(FilledLabel(i,j)) + 1; End end % Calculates the roundness of each graphic unit by counting the number of pixelsfor i = 1 : num
Ecllipseratio(i) = 4*pi*Area(i)/Premeter(i)^2; End % Calculate the color (chroma) of each image HSV= RGB2HSV (I); % to HSV in preparation for extraction of color elements [row,col] = size(FilledLabel); % Count the number of pixels in each block of the filled graph. MeanHue = zeros(1,num);
for i = 1 : num
Hue = zeros(Area(i),1);
nPoint = 0;
for j = 1 : row
for k = 1 : col
if(FilledLabel(j,k) == i)
nPoint = nPoint + 1;
Hue(nPoint,1) = HSV(j,k,1);
end
end
end
Hue(:,i) = sort(Hue(:,1));
for j = floor(nPoint*0.1) : floor(nPoint*0.9)
MeanHue(i) = MeanHue(i) + Hue(j,1);
end
MeanHue(i) = MeanHue(i) / (0.8*nPoint); % Calculate the average chroma value end % identify carrot % build carrot classifier, classify each image in two-dimensional feature space Huluobo =1;
mazarea=Area(1);
for i=1:num
if(MAZarea <Area(I))% criteria for peach classifier recognition: carrot is judged to have the largest Area in each graph mazarea=Area(I); huluobo=i; HuluoboHSV =HSV; End end % Build corresponding image masks respectively for the categories separated, and mask the original image brightness image.for j = 1 : row
for k = 1 : col
if(FilledLabel(j,k) ~=huluobo)
%huluoboHSV(j,k,2) =0;
huluoboHSV(j,k,3) =0; End End % Transform to generate the final result image, the result displayed in the image is the corresponding classifier in the specified category Huluobomatrix = HSV2RGB (huluoboHSV); % convert to an RGB color plot where the rest of the fruit has been filtered out, leaving only carrots subplot(2.2.4),imshow(huluobomatrix);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)Global I Area FilledLabel HSV MeanHue Ecllipseratio Label Num Premeter % Calculates the perimeter of each graph unit by connecting pixels or counting the number of boundary pixels The numPoints array represents the number of pixels (that is, the number of perimeters) of each graph %num= max(max(Label));
for i = 1 : num
Premeter(i) = 0;
end
[row,col] = size(Label);
for i = 1 : row
for j = 1 : col
if(Label(i,j) > 0)
Premeter(Label(i,j)) = Premeter(Label(i,j)) + 1; FilledLabel=imfill(Label,'holes'); % fills the area of the graph enclosed in the middle of the marked boundaryfor i = 1 : num
Area(i) = 0;
end
[row,col] = size(FilledLabel);
for i = 1 : row
for j = 1 : col
if(FilledLabel(i,j) > 0)
Area(FilledLabel(i,j)) = Area(FilledLabel(i,j)) + 1; % Calculate the area of each shape by counting the number of pixelsCopy the code
3. Operation results
Fourth, note
Version: 2014 a