function varargout = pjimage(varargin)
% PJIMAGE MATLAB code for pjimage.fig
% PJIMAGE, by itself, creates a new PJIMAGE or raises the existing
% singleton*.
%
% H = PJIMAGE returns the handle to a new PJIMAGE or the handle to
% the existing singleton*.
%
% PJIMAGE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PJIMAGE.M with the given input arguments.
%
% PJIMAGE('Property','Value',...) creates a new PJIMAGE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before pjimage_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to pjimage_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 pjimage
% Last Modified by GUIDE v2.5 15-Jan-2017 13:31:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @pjimage_OpeningFcn, ...
'gui_OutputFcn', @pjimage_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 pjimage is made visible.
function pjimage_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 pjimage (see VARARGIN)
% Choose default command line output for pjimage
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes pjimage wait for user response (see UIRESUME)
% uiwait(handles.figure_pjimage);
% --- Outputs from this function are returned to the command line.
function varargout = pjimage_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;
% --------------------------------------------------------------------
function m_file_Callback(hObject, eventdata, handles)%文件
% hObject handle to m_file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function fruit_kind_Callback(hObject, eventdata, handles)%单个水果识别器
% hObject handle to fruit_kind (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function m_image_Callback(hObject, eventdata, handles)%找水果
% hObject handle to m_image (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function improsess_Callback(hObject, eventdata, handles)%图像处理
% hObject handle to improsess (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function help_Callback(hObject, eventdata, handles)%帮助
% hObject handle to help (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function info2_Callback(hObject, eventdata, handles)%% 使用说明
% hObject handle to info2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
H=['使用步骤:1、打开一张含有水果的图片;2、在找水果菜单栏点击各个水果的识别命令;3、可以实现单个水果识别,点击水果识别器即可;'];
helpdlg(H,'使用说明');
% --------------------------------------------------------------------
function info_Callback(hObject, eventdata, handles)%% 关于作者
% hObject handle to info (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
H=['本程序由*MATLAB boys and girl*小组联合编写']
helpdlg(H,'about writer');
% --------------------------------------------------------------------
function im2bw_Callback(hObject, eventdata, handles)%% 二值化
% hObject handle to im2bw (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I=getappdata(handles.figure_pjimage,'img_src');
I=im2bw(I);
axes(handles.axes2);
imshow(I);
% --------------------------------------------------------------------
function imcontour_Callback(hObject, eventdata, handles)%% 轮廓提取
% hObject handle to imcontour (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I=getappdata(handles.figure_pjimage,'img_src');
I=im2bw(I);
axes(handles.axes2);
imcontour(I);
% --------------------------------------------------------------------
function canny_Callback(hObject, eventdata, handles)%% canny 边缘检测
% hObject handle to canny (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I=getappdata(handles.figure_pjimage,'img_src');
I=im2bw(I);
axes(handles.axes2);
edge(I,'canny');
% --------------------------------------------------------------------
function bana_Callback(hObject, eventdata, handles)%香蕉识别
% hObject handle to bana (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%% 预处理与边缘检测
I=getappdata(handles.figure_pjimage,'img_src');
[~,~,k]=size(I);
if k==3
I2=rgb2gray(I);
else
I2=I;
end
IA=im2bw(I2,0.2); % 将图像转化为二值图像
IB= bwareaopen(IA, 13); % 去除图像中面积过小的杂点
IC= imfill(IB,'holes'); % 填补图中空
se1=strel('square',3); % 定义腐蚀算法的3*3结构
ID=imerode(IC,se1); % 腐蚀算法
se2=strel('square',3); % 定义膨胀算法的结构
IE=imdilate(ID,se2); % 膨胀算法使轮廓清析
B = bwmorph(IE,'remove'); %将不同的图形进行分别标记,num表示连接的图形对象的个数
[Label,num] = bwlabel(B,8); %得到各个图像的边界像素的数组
%% 计算各个图形单元的周长
Premeter=zeros(1,num);
for i = 1 : num
Premeter(i)=sum(sum(Label==i));
end
%% 计算各个图形单元的面积
FilledLabel = imfill(Label,'holes'); %填充打过标记的边界线中间围成的图形区域
Area=zeros(1,num);
for i = 1 : num
Area(i)=sum(sum(FilledLabel==i));
end
%% 计算各个图形单元的圆度
for i = 1 : num
roundness(i) = 4*pi*Area(i)/Premeter(i)^2;
end
%% 构建香蕉的分类器,在二维特征空间对各个图像进行类别区分
banana=0;
d1=1;
for i=1:num
if((roundness(i)<0.5)) %分类器识别香蕉的准则:判断各个图形中平均圆率值小于0.5的为香蕉
banana(d1)=i;