基于MATLAB GUI的系统设计(七)上

接下来学习的是关于颜色空间方面的知识。

颜色空间也称彩色模型(又称彩色空间或彩色系统),颜色空间有许多种,常用有RGB,HSI,CMY,HSV等。
下面主要了解的是RGB颜色空间和HSI颜色空间两种。

  • RGB颜色空间模型
    RGB(Red、Green、Blue)颜色空间模型通过红、绿、蓝三原色来对颜色进行描述。以R(Red红)、G(Green绿)、B(Blue蓝)三原色为基础,不同程度地进行叠加,从而得到多彩的颜色,因此一般称为三基色模式,根据其亮度不同每种颜色可分为256个等级,RGB颜色空间模型能够用一个立方体来模拟,如下图所示。
    在这里插入图片描述
  • HSI颜色空间模型
    HSI(Hue、Saturation、Intensity)颜色空间模型基于人的视觉系统,直接采用色调、饱和度和亮度颜色的三要素来表示颜色。其中H决定了彩色颜色的本质,是彩色颜色最本质的性质;S表明颜色的深浅,S越高的话,颜色就越深;I是指人眼对光亮暗的感知水平,HSI颜色空间模型能够用一个圆锥来模拟,如下图所示。
    在这里插入图片描述
    由RGB颜色空间模型转化到HSI颜色空间模型的公式如下:
    在这里插入图片描述
    其中,
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

实例一: 提取一张彩色图像的R、G、B三分量图像。

第一步:GUIDE画界面。在这里插入图片描述
第二步:编辑代码。

function varargout = R(varargin)
%R MATLAB code file for R.fig
%      R, by itself, creates a new R or raises the existing
%      singleton*.
%
%      H = R returns the handle to a new R or the handle to
%      the existing singleton*.
%
%      R('Property','Value',...) creates a new R using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to R_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      R('CALLBACK') and R('CALLBACK',hObject,...) call the
%      local function named CALLBACK in R.M with the given input
%      arguments.
%
%      *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 R

% Last Modified by GUIDE v2.5 03-Aug-2019 19:11:02

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @R_OpeningFcn, ...
                   'gui_OutputFcn',  @R_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 R is made visible.
function R_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = R_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 open.
function open_Callback(hObject, eventdata, handles)
% hObject    handle to open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile({'*.bmp;*.jpg;*.png;*.jpeg;*.tif;*.gif;*.Image files'},'载入图像');%选择路径打开图像
str=[pathname filename]; %打开图像  
im=imread(str);  %打开axes1的句柄 进行axes1的操作 
axes(handles.axes1);  %在axes1中显示图像  
imshow(im);  
handles.img=im;    %把图像发给handles.img
%图像处理部分
axes(handles.axes2);
r=im(:,:,1);
imshow(r); 
axes(handles.axes3);
g=im(:,:,2);
imshow(g);
axes(handles.axes4);
b=im(:,:,3);
imshow(b);

% --- Executes on button press in exit.
function exit_Callback(hObject, eventdata, handles)
% hObject    handle to exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc         %清除指令窗
close all   %关闭所有句柄可见的窗口
close(gcf)  %关闭当前窗口
clear       %清除内存变量和函数

% --- Executes on button press in save_R.
function save_R_Callback(hObject, eventdata, handles)
% hObject    handle to save_R (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName] =uiputfile({'*.jpg','JPEG(*.jpg)';'*.bmp','Bitmap(*.bmp)';'*.gif','GIF(*.gif)';'*.*', 'All Files (*.*)'},'Save Picture','Untitled');
if FileName==0
    return;
else
    h=getframe(handles.axes2);
    imwrite(h.cdata,[PathName,FileName]);
end

% --- Executes on button press in save_G.
function save_G_Callback(hObject, eventdata, handles)
% hObject    handle to save_G (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName] =uiputfile({'*.jpg','JPEG(*.jpg)';'*.bmp','Bitmap(*.bmp)';'*.gif','GIF(*.gif)';'*.*', 'All Files (*.*)'},'Save Picture','Untitled');
if FileName==0
    return;
else
    h=getframe(handles.axes3);
    imwrite(h.cdata,[PathName,FileName]);
end

% --- Executes on button press in save_B.
function save_B_Callback(hObject, eventdata, handles)
% hObject    handle to save_B (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName] =uiputfile({'*.jpg','JPEG(*.jpg)';'*.bmp','Bitmap(*.bmp)';'*.gif','GIF(*.gif)';'*.*', 'All Files (*.*)'},'Save Picture','Untitled');
if FileName==0
    return;
else
    h=getframe(handles.axes4);
    imwrite(h.cdata,[PathName,FileName]);
end

第三步:运行。

在这里插入图片描述
第四步:在完成GUI界面创建后,对所创建界面的效果进行试验。点击“选择图片”按钮在文件夹中选择一张需要处理的图像,随后可以在界面上得到三张分量图像,点击“保存R分量图像”或“保存G分量图像”或“保存B分量图像”按钮可以保存按钮对应上方画布中的图像,点击“退出”按钮可以退出界面。在这里插入图片描述
实例二: 显示一张彩色图像R、G、B三分量图像的直方图。

第一步:GUIDE画界面。在这里插入图片描述
第二步:编辑代码。

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

% Last Modified by GUIDE v2.5 03-Aug-2019 19:31:28

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = RGBzhifangtu_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 open.
function open_Callback(hObject, eventdata, handles)
% hObject    handle to open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile({'*.bmp;*.jpg;*.png;*.jpeg;*.tif;*.gif;*.Image files'},'载入图像');%选择路径打开图像
if isequal(filename,0)||isequal(pathname,0)%若filename为0或pathname为0,即未选中文件
    errordlg('未选中文件','警告');%建立一个名为警告的错误对话框,内容为“未选中文件”
    return;
end
str=[pathname,filename];%将文件名和目录名组合成一个完整的路径
x=imread(str);%读入图像
axes(handles.axes1);%定义图形区域axes1
imshow(x);%显示图像
handles.img=x;%把图像发给handles.img
guidata(hObject,handles);%把handles句柄更新

% --- Executes on button press in exit.
function exit_Callback(hObject, eventdata, handles)
% hObject    handle to exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc         %清除指令窗
close all   %关闭所有句柄可见的窗口
close(gcf)  %关闭当前窗口
clear       %清除内存变量和函数

% --- Executes on button press in zhifangtu_R.
function zhifangtu_R_Callback(hObject, eventdata, handles)
% hObject    handle to zhifangtu_R (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes2);
a=imhist(handles.img(:,:,1));%直方图统计
a1=a(1:10:256);
horz=1:10:256;
bar(horz,a1);
set(handles.axes2,'xtick',0:50:255);

% --- Executes on button press in zhifangtu_G.
function zhifangtu_G_Callback(hObject, eventdata, handles)
% hObject    handle to zhifangtu_G (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes3);
a=imhist(handles.img(:,:,2));%直方图统计
a1=a(1:10:256);
horz=1:10:256;
bar(horz,a1);
set(handles.axes2,'xtick',0:50:255);

% --- Executes on button press in zhifangtu_B.
function zhifangtu_B_Callback(hObject, eventdata, handles)
% hObject    handle to zhifangtu_B (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes4);
a=imhist(handles.img(:,:,3));%直方图统计
a1=a(1:10:256);
horz=1:10:256;
bar(horz,a1);
set(handles.axes2,'xtick',0:50:255);

第三步:运行。在这里插入图片描述
第四步:在完成GUI界面创建后,对所创建界面的效果进行试验。点击“打开图片”按钮在文件夹中选择一张需要处理的图像,分别点击“R直方图”、“G直方图”和“B直方图”按钮可以依次得到彩色图像R、G、B三分量图像的直方图,点击“退出”按钮可以退出界面。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值