【图像处理】基于matlab图像RGB三色合成+分离【含Matlab源码 401期】

864 阅读4分钟

一、简介

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真实三原色表色系统空间是不同的,后者 是与设备无关的颜色空间。

二、源代码

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

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help rgb1

% Last Modified by GUIDE v2.5 17-Dec-2008 10:31:18

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @rgb1_OpeningFcn, ...
                   'gui_OutputFcn',  @rgb1_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 rgb1 is made visible.
function rgb1_OpeningFcn(hObject, eventdata, handles, varargin)
A=imread('aa.jpg');
axes(handles.axes1);
imshow(A);

% 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 rgb1 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = rgb1_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)
global A;
val=get(hObject,'value');
if(val==1)
    A=imread('aa.jpg');
elseif(val==2)
    A=imread('a.jpg');
elseif(val==3)
    A=imread('b.jpg');
end
axes(handles.axes1);
imshow(A);
% 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)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


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

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

三、运行结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

四、备注

版本:2014a