Code covered by the BSD License  

Highlights from
RGB Color Extractor

image thumbnail

RGB Color Extractor

by

 

This code is GUI base software to extract Red Green and Blue colors from image

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

% Last Modified by GUIDE v2.5 25-Feb-2010 20:03:30

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

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

% Update handles structure
guidata(hObject, handles);

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

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

% --------------------------------------------------------------------
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 m_open_Callback(hObject, eventdata, handles)
% hObject    handle to m_open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global file;
file = uigetfile('*.jpg');
    if ~isequal(file, 0)
        im = imread(file);
    end
clc;
fimr=im;
fimr(:,:,:)=0;
fimg=im;
fimg(:,:,:)=0;
fimb=im;
fimb(:,:,:)=0;
fimw=im;
fimw(:,:,:)=0;
cr=0;
cg=0;
cb=0;
cw=0;
cim=0;
[imsr,imsc,imsd]=size(im);
test=im;
test(:,:,:)=0;
for i=1:imsr
    for j=1:imsc  
        if im(i,j,1)< 20 &im(i,j,2)< 20&im(i,j,3)< 20
            cim=cim+1;
        else
           test(i,j,:)=im(i,j,:);
        end
    end
end
cim=imsr*imsc-cim;
im=test;
for i=1:imsr
    for j=1:imsc
        if ((im(i,j,1)>150) & (im(i,j,2)<175) & (im(i,j,3)<175))
            fimr(i,j,:)=im(i,j,:);
            cr=cr+1;
        end
    end
end
in=im-fimr;

for i=1:imsr
    for j=1:imsc
        if ((in(i,j,2)>150) & (in(i,j,1)<170) & (in(i,j,3)<170))
            fimg(i,j,:)=in(i,j,:);
            cg=cg+1;
        end
    end
end

in=in-fimg;

for i=1:imsr
    for j=1:imsc        
        if ((in(i,j,3)>150) & (in(i,j,1)<175) & (in(i,j,2)<175))
            fimb(i,j,:)=in(i,j,:);
            cb=cb+1;
        end
   end
end
in=in-fimb;

for i=1:imsr
    for j=1:imsc  
        if sum(in(i,j,:))/(255*3)>0.7
            fimw(i,j,:)=in(i,j,:);
            cw=cw+1;
        end
    end
end
in=in-fimw;

cin=0;
for i=1:imsr
    for j=1:imsc  
        if in(i,j,:)== 0
            cin=cin+1;
        end
    end
end
cin=imsr*imsc-cin;

cr=cr/cim*100;
cg=cg/cim*100;
cb=cb/cim*100;
cw=cw/cim*100;
cin=cin/cim*100;
cim;

subplot(2,3,1), imshow(im), title('Original');
subplot(2,3,2), imshow(fimr), title(sprintf('%0.2f %% of Red',cr));
subplot(2,3,4), imshow(fimb), title(sprintf('%0.2f %% of Blue',cb));
subplot(2,3,3), imshow(fimg), title(sprintf('%0.2f %% of Green',cg));
subplot(2,3,5), imshow(fimw), title(sprintf('%0.2f %% of White',cw));
%subplot(2,3,6), imshow(im-fimr-fimg-fimb-fimw), title(sprintf('%0.2f %% of Image loss',cin));

set(handles.t_red,'String',sprintf('%0.2f',cr))
set(handles.t_green,'String',sprintf('%0.2f',cg))
set(handles.t_blue,'String',sprintf('%0.2f',cb))
set(handles.t_white,'String',sprintf('%0.2f',cw))
set(handles.t_in,'String',sprintf('%0.2f',cin))
set(handles.t_file,'String',sprintf('%s',file))
% --------------------------------------------------------------------
function m_exit_Callback(hObject, eventdata, handles)
% hObject    handle to m_exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function m_save_Callback(hObject, eventdata, handles)
% hObject    handle to m_save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';'*.*','All Files' })
global file;
print(sprintf('sp_%s',file),'-djpeg');


% --------------------------------------------------------------------
function m_help_Callback(hObject, eventdata, handles)
% hObject    handle to m_help (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function m_cont_Callback(hObject, eventdata, handles)
% hObject    handle to m_cont (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
warndlg({'Contact : Prashant Somani';'email : p.m.somani@gmail.com'})

% --------------------------------------------------------------------
function m_about_Callback(hObject, eventdata, handles)
% hObject    handle to m_about (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
warndlg({'Software is developed by sP';'Version : 9.0.1'})



% --------------------------------------------------------------------
function m_batch_Callback(hObject, eventdata, handles)
% hObject    handle to m_batch (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

clc;
[bfile, bpath, filterindex] = uigetfile({'*.jpg','Images'}, 'MultiSelect', 'on');
bfile=char(bfile);
[r,c]=size(bfile);
for f=1:r
im = imread(bfile(f,:));
fimr=im;
fimr(:,:,:)=0;
fimg=im;
fimg(:,:,:)=0;
fimb=im;
fimb(:,:,:)=0;
fimw=im;
fimw(:,:,:)=0;
cr=0;
cg=0;
cb=0;
cw=0;
cim=0;
[imsr,imsc,imsd]=size(im);
test=im;
test(:,:,:)=0;
for i=1:imsr
    for j=1:imsc  
        if im(i,j,1)< 20 &im(i,j,2)< 20&im(i,j,3)< 20
            cim=cim+1;
        else
           test(i,j,:)=im(i,j,:);
        end
    end
end
cim=imsr*imsc-cim;
im=test;
for i=1:imsr
    for j=1:imsc
        if ((im(i,j,1)>150) & (im(i,j,2)<175) & (im(i,j,3)<175))
            fimr(i,j,:)=im(i,j,:);
            cr=cr+1;
        end
    end
end
in=im-fimr;

for i=1:imsr
    for j=1:imsc
        if ((in(i,j,2)>150) & (in(i,j,1)<170) & (in(i,j,3)<170))
            fimg(i,j,:)=in(i,j,:);
            cg=cg+1;
        end
    end
end

in=in-fimg;

for i=1:imsr
    for j=1:imsc        
        if ((in(i,j,3)>150) & (in(i,j,1)<175) & (in(i,j,2)<175))
            fimb(i,j,:)=in(i,j,:);
            cb=cb+1;
        end
   end
end
in=in-fimb;

for i=1:imsr
    for j=1:imsc  
        if sum(in(i,j,:))/(255*3)>0.7
            fimw(i,j,:)=in(i,j,:);
            cw=cw+1;
        end
    end
end
in=in-fimw;

cin=0;
for i=1:imsr
    for j=1:imsc  
        if in(i,j,:)== 0
            cin=cin+1;
        end
    end
end
cin=imsr*imsc-cin;

cr=cr/cim*100;
cg=cg/cim*100;
cb=cb/cim*100;
cw=cw/cim*100;
cin=cin/cim*100;
cim;

subplot(2,3,1), imshow(im), title('Original');
subplot(2,3,2), imshow(fimr), title(sprintf('%0.2f %% of Red',cr));
subplot(2,3,4), imshow(fimb), title(sprintf('%0.2f %% of Blue',cb));
subplot(2,3,3), imshow(fimg), title(sprintf('%0.2f %% of Green',cg));
subplot(2,3,5), imshow(fimw), title(sprintf('%0.2f %% of White',cw));
%subplot(2,3,6), imshow(im-fimr-fimg-fimb-fimw), title(sprintf('%0.2f %% of Image loss',cin));

set(handles.t_red,'String',sprintf('%0.2f',cr))
set(handles.t_green,'String',sprintf('%0.2f',cg))
set(handles.t_blue,'String',sprintf('%0.2f',cb))
set(handles.t_white,'String',sprintf('%0.2f',cw))
%set(handles.t_in,'String',sprintf('%0.2f',cin))
set(handles.t_file,'String',sprintf('%s',bfile(f,:)))
print(sprintf('sp_%s',bfile(f,:)),'-djpeg');
end

Contact us