【水果识别】基于matlab GUI RGB+HSV水果成熟度分级系统【含Matlab源码 825期】

一、简介

1 RGB颜色空间:
RGB(red,green,blue)颜色空间最常用的用途就是显示器系统(计算机、电视机等都是采用RGB颜色空间来进行图像显示)。一般来说,电脑,电视机等是利用三个电子枪分别发射R分量,G分量,B分量的电子束,以此来激发屏幕上的RGB三种颜色的荧光粉,从而发出不同颜色、不同亮度的像素、进而组成了一幅图像;很明显,RGB颜色空间利用了物理学中的三原色叠加从而组成产生各种不同颜色的原理。在RGB颜色空间中,R、G、B三个分量的属性是独立的。也即是说,RGB颜色可以表示为(Red, Green, Blue)。其中,各个分量的数值越小,亮度越低。数值越大,亮度越高;如:(0,0,0)表示黑色,(255,255,255)表示白色;
RGB颜色空间表示颜色的格式有RGB565,RGB555,RGB24,RGB32等;
其中,RGB565是使用16位表示一个像素:5位表示R,6位表示G,5位表示B;
RGB555是另一种16位表示一个像素的方法:分别用5位来表示RGB分量;剩余一位不用;
RGB24是使用24位表示一个像素:分别用8位表示RGB各个分量;这种方式最为常见;
RGB32是使用32位来表示一个像素:分别用8位表示RGB各个分量;剩余8位为alpha通道,也就是用来表示图像的“透明度”。注意:在某些系统中,剩余的8位并没有使用;
RGB色彩空间称为与设备相关的色彩空间,因为不同的扫描仪扫描同一幅图像,会得到不同色彩的图像数据;不同型号的显示器显示同一幅图像,也会有不同 的色彩显示结果。显示器和扫描仪使用的RGB空间与CIE 1931 RGB真实三原色表色系统空间是不同的,后者 是与设备无关的颜色空间。

2 HSV颜色空间:
HSV(hue,saturation,value)表示色相、饱和度和亮度。该颜色空间可以用一个圆锥来表示,如下图所示:
在这里插入图片描述
在这里插入图片描述
这里,hue表示颜色的相位角,取值范围是0—360;S表示颜色的饱和度;S为一比例值,范围从0到1,它表示成所选颜色的纯度和该颜色最大的纯度之间的比率,通俗点讲,S表示的是某种颜色的“纯度”, S取值越大,表示色彩越纯,取值越小,表示色彩越灰。V表示色彩的明亮程度,范围从0到1。V等于0表示圆锥的底部定点,也就是黑色,V等于1表示圆锥的顶面,当V=1并且S=0时表示纯白色;需要注意的是:Apple的Mac操作系统以及photoshop都是采用HSV颜色空间。

二、源代码

function varargout = GUIzyp(varargin)
% GUIZYP MATLAB code for GUIzyp.fig
%      GUIZYP, by itself, creates a new GUIZYP or raises the existing
%      singleton*.
%
%      H = GUIZYP returns the handle to a new GUIZYP or the handle to
%      the existing singleton*.
%
%      GUIZYP('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUIZYP.M with the given input arguments.
%
%      GUIZYP('Property','Value',...) creates a new GUIZYP or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GUIzyp_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUIzyp_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 GUIzyp

% Last Modified by GUIDE v2.5 10-Jun-2017 22:22:31

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

% Choose default command line output for GUIzyp
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = GUIzyp_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)
global FileName 
global FilePath
  global z
global H
global var
 [FileName, FilePath]=uigetfile('*.jpg;*.png;*.tif;*.img;*.gif;','请选择图像数据');
Image_f=[FilePath FileName];
Src_Image =imread(Image_f);%读取图像数据
axes(handles.axes1);
imshow(Image_f);

var=get(handles.popupmenu1,'value'); %假定这个下拉菜单的TAG是popupmenu1
switch var

    case  1 %下面还有几个选项

    hsv_f = rgb2hsv(Src_Image);
    H = hsv_f(:,:,1)*255;
    S = hsv_f(:,:,2)*255;
    V = hsv_f(:,:,3)*255;
    
    [y,x,z]=size(Src_Image);
    Red_y=zeros(y,1);
    Green_y=zeros(y,1);
    Yellow_y=zeros(y,1);
    for i=1:y
        for j=1:x
             if(((H(i,j)>=1)&&(H(i,j)<10))||((H(i,j) >= 245) && (H(i,j) <=254)) &&(V(i,j)>50)&&(S(i,j)>30))  %(V(i,j)<255)      &&(V(i,j)>50)&&(S(i,j)>30)
                Red_y(i,1)= Red_y(i,1)+1;%红像素点统计
             elseif(((H(i,j)>=11)&&(H(i,j)<20)) &&(V(i,j)>50)&&(S(i,j)>30))  %(V(i,j)<255)    &&(V(i,j)>50)&&(S(i,j)>30)
                Yellow_y(i,1)= Yellow_y(i,1)+1;%黄像素点统计
             elseif(((H(i,j)>=20)&&(H(i,j)<30)) &&(V(i,j)>50)&&(S(i,j)>30))  %(V(i,j)<255)   &&(V(i,j)>50)&&(S(i,j)>30)
                Green_y(i,1)= Green_y(i,1)+1;%绿像素点统计

         end
     end      
 end
 
Max_Red_y=max(Red_y)
Max_Green_y=max(Green_y)
Max_Yellow_y=max(Yellow_y)
if((Max_Red_y> Max_Green_y)&&(Max_Red_y> Max_Yellow_y))
    Result =1;
elseif((Max_Yellow_y> Max_Green_y)&&(Max_Yellow_y> Max_Red_y))
    Result =2;
elseif((Max_Green_y> Max_Red_y)&&(Max_Green_y> Max_Yellow_y))
    Result =3;
else
    Result =4;
end

 axes(handles.axes3);
imhist(uint8(H));
if(Result==1) 
   set(handles.edit1,'string','一等');    
elseif(Result==2); 
    set(handles.edit1,'string','二等'); 
elseif(Result==3)
    set(handles.edit1,'string','三等'); 
else   
  set(handles.edit1,'string','不成熟'); 
end

    case 2
        hsv_f = rgb2hsv(Src_Image);
H = hsv_f(:,:,1)*255;
S = hsv_f(:,:,2)*255;
V = hsv_f(:,:,3)*255;
[y,x,z]=size(Src_Image);
Red_y=zeros(y,1);
Green_y=zeros(y,1);
Yellow_y=zeros(y,1);
 for i=1:y
    for j=1:x
             if(((H(i,j)>=2)&&(H(i,j)<5))||((H(i,j) >= 245) && (H(i,j) <=254)) &&(V(i,j)>50)&&(S(i,j)>30))  %(V(i,j)<255)      &&(V(i,j)>50)&&(S(i,j)>30)
                Red_y(i,1)= Red_y(i,1)+1;%红像素点统计
             elseif(((H(i,j)>=5)&&(H(i,j,1)<8)) &&(V(i,j)>50)&&(S(i,j)>27))  %(V(i,j)<39)    &&(V(i,j)>50)&&(S(i,j)>30)
                Yellow_y(i,1)= Yellow_y(i,1)+1;%黄像素点统计
             elseif(((H(i,j)>=8)&&(H(i,j)<11)) &&(V(i,j)>50)&&(S(i,j)>30))  %(V(i,j)<255)   &&(V(i,j)>50)&&(S(i,j)>30)
                Green_y(i,1)= Green_y(i,1)+1;%绿像素点统计

             end
     end      
 end
 
Max_Red_y=max(Red_y)
Max_Green_y=max(Green_y)
Max_Yellow_y=max(Yellow_y)
if((Max_Red_y> Max_Green_y)&&(Max_Red_y> Max_Yellow_y))
    Result =1;
elseif((Max_Yellow_y> Max_Green_y)&&(Max_Yellow_y> Max_Red_y))
    Result =2;
elseif((Max_Green_y> Max_Red_y)&&(Max_Green_y> Max_Yellow_y))
    Result =3;
else
    Result =4;
end

 axes(handles.axes3);
imhist(uint8(H));

if(Result==1) 
    set(handles.edit1,'string','一等');    
elseif(Result==2);
    set(handles.edit1,'string','二等');      
elseif(Result==3)
     set(handles.edit1,'string','三等');    
else
    set(handles.edit1,'string','不成熟');    
end

三、运行结果

【水果识别】基于matlab GUI RGB+HSV水果成熟度分级系统【含Matlab源码 825期】_第1张图片
【水果识别】基于matlab GUI RGB+HSV水果成熟度分级系统【含Matlab源码 825期】_第2张图片
【水果识别】基于matlab GUI RGB+HSV水果成熟度分级系统【含Matlab源码 825期】_第3张图片
【水果识别】基于matlab GUI RGB+HSV水果成熟度分级系统【含Matlab源码 825期】_第4张图片

四、备注

完整代码或者代写添加QQ 1564658423

你可能感兴趣的:(matlab,图像处理)