% *********************************************************************
% 说明:设计思路:先对原图像进行灰度化即二值化处理,再进行区域分割和特征提取。
% 颜色特征利用已知水果图片计算平均色度,再进行判别;
% 形状特征是对分割出的区域进行分析,不同水果具有特定的形状信息,本设计主要选择面积和长轴长度以及离心率作为判别特征。
% 平均色度的计算可参见sedu.m,形状特征的计算可参见Region_separate.m
% *********************************************************************
function varargout = Fruits_recognition(varargin)
% FRUITS_RECOGNITION MATLAB code for Fruits_recognition.fig
% FRUITS_RECOGNITION, by itself, creates a new FRUITS_RECOGNITION or raises the existing
% singleton*.
%
% H = FRUITS_RECOGNITION returns the handle to a new FRUITS_RECOGNITION or the handle to
% the existing singleton*.
%
% FRUITS_RECOGNITION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FRUITS_RECOGNITION.M with the given input arguments.
%
% FRUITS_RECOGNITION('Property','Value',...) creates a new FRUITS_RECOGNITION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Fruits_recognition_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Fruits_recognition_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 Fruits_recognition
% Last Modified by GUIDE v2.5 04-May-2021 19:46:07
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Fruits_recognition_OpeningFcn, ...
'gui_OutputFcn', @Fruits_recognition_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 Fruits_recognition is made visible.
function Fruits_recognition_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 Fruits_recognition (see VARARGIN)
% Choose default command line output for Fruits_recognition
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
global fruit_name %初始化
fruit_name='Banana';
% UIWAIT makes Fruits_recognition wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Fruits_recognition_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;
% *******************************************************************
% ************** ***********************
% ************** Useful Functions Starts here ***********************
% ************** ***********************
% *******************************************************************
% --- Executes on button press in File_open.
function File_open_Callback(hObject, eventdata, handles)
% hObject handle to File_open (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global img;
global im_gray;
global im_bw;
global HSV;
global fruit_name;
[filename,pathname]=uigetfile('*.jpg'); %读取图像
img = imread([pathname,filename]);
im_gray = rgb2gray(img); %灰度化
im_bw = im2bw(im_gray,0.9); %二值化
im_bw= bwareaopen(im_bw, 10); %去除图像中面积过小的杂点
im_bw= 1 - im_bw; %灰度反转,将背景变为黑色
HSV = rgb2hsv(img); %转换为HSV,为后面的颜色元素的提取做准备
axes(handles.axes1);
colormap(colorcube);
imagesc(img);
set(handles.axes1,'xtick',[],'ytick',[]);
% --- Executes when selected object is changed in uipanel2.
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uipanel2
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
global fruit_name
switch get(hObject, 'Tag')
case 'radiobutton_banana'
fruit_name = 'Banana';
case 'radiobutton_pepper'
fruit_name = 'Pepper';
case 'radiobutton_pear'
fruit_name = 'Pear';
case 'radiobutton_tomato'
fruit_name = 'Tomato';
case 'radiobutton_apple'
fruit_name = 'Apple';
end
% --- Executes on button press in Recognition.
function Recognition_Callback(hObject, eventdata, handles)
% hObject handle to Recognition (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global img;
%global im_gray;
global im_bw;
global fruit_name;
global im_show;
global HSV;
[Label,num] = bwlabel(im_bw,8); %将不同的图形进行分别标记,num表示连接的图形对象的个数
pro = regionprops(Label,'all'); % 统计被标记的区域的形状特征
% %% 计算已知类别水果的平均色度
% I=imread('梨.jpg');
% meanhu_pear=Chromacity_caculate(I)
% I1=imread('青椒.jpg');
% meanhu_pepper=Chromacity_caculate(I1)
% I2=imread('香蕉.jpg');
% meanhu_banana=Chromacity_caculate(I2)
% I3=imread('西红柿.jpg');
% meanhu_tomato=Chromacity_caculate(I3)
% %% 计算各个分块图像的色度
FilledLabel = imfill(Label,'holes'); %填充打过标记的边界线中间围成的图形区域
[row,col] = size(FilledLabel); %统计填充后的图形中各块图形所含像素的个数的多少
MeanHue = zeros(1,num);
for i = 1 : num
Hue = zeros(pro(i).Area,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); %计算出平均的色度值
end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.