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

接着上一部分的内容——颜色空间

实例三: 设计GUI程序,使三个滑动条实现R、G、B各分量上下可调,调整范围在0-1之间,显示原始图像和调整后的图像,同时显示R、G、B三分量的灰度图像。

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

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

% Last Modified by GUIDE v2.5 27-Mar-2019 22:18:43

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

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

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = RGB_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 slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global R
global G
global B
global val1
global val2
global val3
val1=get(hObject,'Value');
set(handles.text1,'String',num2str(val1));
axes(handles.axes3);
imshow(val1*R);
axes(handles.axes4);
imshow(val2*G);
axes(handles.axes5);
imshow(val3*B);
axes(handles.axes2);
I=cat(3,val1*R,val2*G,val3*B);
imshow(I);

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global R
global G
global B
global val1
global val2
global val3
val2=get(hObject,'Value');
set(handles.text2,'String',num2str(val2));
axes(handles.axes3);
imshow(val1*R);
axes(handles.axes4);
imshow(val2*G);
axes(handles.axes5);
imshow(val3*B);
axes(handles.axes2);
I=cat(3,val1*R,val2*G,val3*B);
imshow(I);

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
% hObject    handle to slider3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global R
global G
global B
global val1
global val2
global val3
val3=get(hObject,'Value');
set(handles.text3,'String',num2str(val3));
axes(handles.axes3);
imshow(val1*R);
axes(handles.axes4);
imshow(val2*G);
axes(handles.axes5);
imshow(val3*B);
axes(handles.axes2);
I=cat(3,val1*R,val2*G,val3*B);
imshow(I);

% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- 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,即未选中文件
    return;
end
global R
global G
global B
axes(handles.axes1);
fpath=[pathname filename];
w=imread(fpath);
imshow(w);
A=w;
R=A(:,:,1);
G=A(:,:,2);
B=A(:,:,3);

% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)
% hObject    handle to save (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 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       %清除内存变量和函数

第三步:运行。
在这里插入图片描述
第四步:在完成GUI界面创建后,对所创建界面的效果进行试验。点击“打开图片”按钮在文件夹中选择一张需要处理的图像,随意调节R、G、B三个分量的滑动条,可以在右上方画布中得到调整后的图像,并在R、G、B三个画布上得到相应的灰度图像,点击“保存图片”按钮可以保存调整后的图像,点击“退出”按钮可以退出界面。

  • R=G=B=0

在这里插入图片描述

  • R=G=B=1

在这里插入图片描述
实例四: 将一张彩色图像从RGB颜色空间转化到HSI颜色空间,并显示原始图像的H、S、I三分量灰度图像。

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

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

% Last Modified by GUIDE v2.5 01-Apr-2019 20:06:54

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

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

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = HSI_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)
global H
global S
global I
[filename,pathname]=uigetfile({'*.bmp;*.jpg;*.png;*.jpeg;*.tif;*.gif;*.Image files'},'载入图像');%选择路径打开图像
str=[pathname filename];
im=imread(str);
axes(handles.axes1);
imshow(im);
im = im2double(im);
r = im(:, :, 1);
g = im(:, :, 2);
b = im(:, :, 3);
%I
I = (r + g + b)/3;
%S
tmp1 = min(min(r, g), b);
tmp2 = r + g + b;
tmp2(tmp2 == 0) = eps;
S = 1 - 3.*tmp1./tmp2;
%H
tmp1 = 0.5*((r - g) + (r - b));
tmp2 = sqrt((r - g).^2 + (r - b).*(g - b));
theta = acos(tmp1./(tmp2 + eps));
H = theta;
H(b > g) = 2*pi - H(b > g);
H = H/(2*pi);
H(S == 0) = 0;
axes(handles.axes5);
T=cat(3,H,S,I);
imshow(T);

% --- Executes on button press in h_s_i.
function h_s_i_Callback(hObject, eventdata, handles)
% hObject    handle to h_s_i (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global H
global S
global I
axes(handles.axes2);
imshow(H);
axes(handles.axes3);
imshow(S);
axes(handles.axes4);
imshow(I);

% --- Executes on button press in save_h.
function save_h_Callback(hObject, eventdata, handles)
% hObject    handle to save_h (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_s.
function save_s_Callback(hObject, eventdata, handles)
% hObject    handle to save_s (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_i.
function save_i_Callback(hObject, eventdata, handles)
% hObject    handle to save_i (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

% --- 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       %清除内存变量和函数

第三步:运行。
在这里插入图片描述
第四步:在完成GUI界面创建后,对所创建界面的效果进行试验。点击“选择图片并转化为HSI模型”按钮在文件夹中选择一张需要处理的图像并且可以在右方画布中得到原始图像的HSI模型图像,点击“原始H、S、I三分量灰度图像”按钮在下方三个画布中分别得到原始图像的H、S、I分量灰度图像,点击“保存H分量灰度图像”或“保存S分量灰度图像”或“保存I分量灰度图像”按钮可以保存按钮对应上方画布中的图像,点击“退出”按钮可以退出界面。在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值