Code covered by the BSD License  

Highlights from
Machine Vision System - Color Recognition

image thumbnail
from Machine Vision System - Color Recognition by Vihang Patil
This gui is a demonstration on the concept of machine vision system for color recognition

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

% Last Modified by GUIDE v2.5 03-Mar-2008 14:56:06

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

% warning off;

splash('thirdeye.kon','png');

axes(handles.axes2); %initialise axes where color will be displayed
img = im2uint8(zeros([128 128 3])); %set it to black color initially
handles.h2 = image(img);
axis off;

axes(handles.axes1);%initialise axes where preview will be displayed
axis off; drawnow;

axes(handles.axes1);
handles.h1 = imshow(im2uint8(ones([240 320 3])));%intial white colored image on the axis


try %intialise sound card for sound alert
    [y,Fs] = wavread('warning.wav');% read a warning wave file
    handles.player = audioplayer(y, Fs);
catch %if sound card not present display following error message
    errstr = lasterr;
    h2 =  errordlg(['Sound Card Not Present OR ' errstr] ,'Sound Card Error');
    setWindowOnTop(h2,'true');
end

handles.guifig = gcf;


try
    if exist('startupdata.mat','file') %load startup data. This contains information about initial conditions set. If not found GUI will load its default values
        load startupdata
        set(handles.animation,'Checked',animation);
        set(handles.sound_alert,'Checked',sound_alert);
        set(handles.auto_hide,'Checked',auto_hide);
        set(handles.on_top,'Checked',on_top);
        if strcmp(get(handles.on_top,'Checked'),'on')
            setWindowOnTop(handles.guifig,'true');
        end        
    end
catch
    errordlg(lasterr);
end

handles.displayperiod = 2; %pause for the amount specified

handles.tmr = timer('TimerFcn', {@TmrFcn,handles.guifig},'BusyMode','Queue',...
    'ExecutionMode','fixedRate','Period',0.1);%intialise a Timer Function

    
guidata(handles.guifig, handles);

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = maingui_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 Timer Event
function TmrFcn(src,event,handles)
handles = guidata(handles);

pause(handles.displayperiod);%pause for the number of seconds intialised initially

str = [pwd '\product images'];%product images stored here
names = dir([str '\*.jpg']);
rand_num = random_integer_generator(1,numel(names),1);%generates  a random number from the number of JPG files present in the product directory
handles.reference_image = imread([str '\' names(rand_num,1).name]);% reads any one random image

drawnow;
set(handles.h1,'CData',handles.reference_image);% displays the randome image on the axis

if strcmp(get(handles.auto_hide,'Checked'),'on')
    setWindowState(handles.guifig,'restore'); %sets the gui figure to restore
    drawnow;
end


if isfield(handles,'second') %the color from first variable is stored in variable second. Not true while startup
    if ~strcmp(handles.first,'NOCOLOR')
        handles.second = handles.first;
    end
end

%%%%%%%%%%%%%%%%%%COLOR MATCHING FUNCTION
[output_color output_image] = validate_color_new(handles.reference_image,handles.data);%new main function for finding color match
%%%%%%%%%%%%%%%%%%%%
set(handles.color_text,'string',upper(output_color)); %sets the string value the name of the color

img = im2uint8(zeros([128 128 3])); %create a img file which can display the color
val = output_image(round(size(output_image,1)/2),round(size(output_image,2)/2),:);
val = squeeze(val)';
img(:,:,1) = val(1);
img(:,:,2) = val(2);
img(:,:,3) = val(3);
set(handles.h2,'CData',img);drawnow; %outputs the color on this img

handles.first = output_color; %store the output color in first variable


if ~strcmp(output_color,'NOCOLOR') %if color cannot be identified from the stored colors then dont give audiovisual indication
    if ~isfield(handles,'second') %check condition in first run, if condition is not satisfied then second varaible = first variable
        handles.second = output_color;
    end

    if ~strcmp(handles.first,handles.second) %if colors are different then do the following activity

        try%necessary if sound card is not present
            if strcmp(get(handles.sound_alert,'Checked'),'on')%give and alert only if sound_alert is set to on
                play(handles.player);
            end
        catch
            errordlg(lasterr);
        end

        if strcmp(get(handles.animation,'Checked'),'on') %do the animation if animation is set to on
            model_color(output_color);%calls a model wearing the color.Default time duration of 3 seconds
        end


    end
end

    if strcmp(get(handles.auto_hide,'Checked'),'on')
        pause(handles.displayperiod);%pause for the number of seconds intialised initially
        setWindowState(handles.guifig,'minimize'); %sets the gui figure to minimize        
    end


if strcmp(get(handles.auto_hide,'Checked'),'off')%This is useful to keep a pause of the same time irrespective of auto hide on or off
    pause(handles.displayperiod);%pause for the number of seconds intialised initially
end


guidata(handles.guifig, handles);




% --- Executes on button press in auto_inspect.
function auto_inspect_Callback(hObject, eventdata, handles)
% hObject    handle to auto_inspect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

val = get(hObject,'Value');

[handles.data,flag] = intialcheck; %This function initialcheck checks and loads the color solutions found in the specified directory ,<root>\data\
if strcmp(flag,'bad')%if color solutions not found do the following
    h3 = errordlg('Colors Not loaded yet. Load them through Control Panel','No Solution Found');
    setWindowOnTop(h3,'true');
    set(hObject,'Value',0);
    return;
end
    
if val == 1%start inspection
    set(hObject,'BackGroundColor','g');
    set(handles.manual_inspect,'Enable','off');
    guidata(hObject, handles);
    start(handles.tmr);    
else%stop inspection
    set(hObject,'BackGroundColor',[236 233 216]/255);
    set(handles.manual_inspect,'Enable','on');
    guidata(hObject, handles);
    stop(handles.tmr);    
end



% --- Executes on button press in manual_auto_inspect.
function manual_inspect_Callback(hObject, eventdata, handles)
% hObject    handle to manual_auto_inspect (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[handles.data,flag] = intialcheck; %This function initialcheck checks and loads the color solutions found in the specified directory ,<root>\data\
if strcmp(flag,'bad')%if color solutions not found do the following
    h3 = errordlg('Colors Not loaded yet. Load them through Control Panel','No Solution Found');
    setWindowOnTop(h3,'true');
    set(hObject,'Value',0);
    return;
end

[filename, pathname] = uigetfile([pwd '\product images\*.jpg'], 'Pick an Image-File');%select the product photo
if filename == 0  
    return;
end
handles.reference_image = imread([pathname filename]);%reads the random image

drawnow;
set(handles.h1,'CData',handles.reference_image);


set(hObject,'Enable','off');
set(hObject,'String','Wait');

try
    if strcmp(get(handles.auto_hide,'Checked'),'on')
        setWindowState(handles.guifig,'restore'); %sets h to restore
    end
catch
    errordlg(lasterr);
end

if isfield(handles,'second') %the color from first variable is stored in variable second. Not true while startup
    if ~strcmp(handles.first,'NOCOLOR')
        handles.second = handles.first;
    end
end

%%%%%%%%%%%%%%%%COLOR MATCHING FUNCTION
[output_color output_image] = validate_color_new(handles.reference_image,handles.data);%new main function for finding color match
%%%%%%%%%%%%%%%%%%%%
set(handles.color_text,'string',upper(output_color)); %sets the string value the name of the color

img = im2uint8(zeros([128 128 3])); %create a img file which can display the color
val = output_image(round(size(output_image,1)/2),round(size(output_image,2)/2),:);
val = squeeze(val)';
img(:,:,1) = val(1);
img(:,:,2) = val(2);
img(:,:,3) = val(3);
set(handles.h2,'CData',img);drawnow; %outputs the color on this img

handles.first = output_color; %store the output color in first variable



if ~strcmp(output_color,'NOCOLOR') %if color cannot be identified from the stored colors then dont give audiovisual indication
    if ~isfield(handles,'second') %check condition in first run, if condition is not satisfied then second varaible = first variable
        handles.second = output_color;
    end

    if ~strcmp(handles.first,handles.second) %if colors are different then do the following activity
        try
            if strcmp(get(handles.sound_alert,'Checked'),'on')%give and alert only if sound_alert is set to on
                play(handles.player);
            end
        catch
            errordlg(lasterr);
        end
       
        if strcmp(get(handles.animation,'Checked'),'on') %do the animation if animation is set to on
            model_color(output_color);%calls a model wearing the color.Default time duration of 3 seconds
        end
    end
end

try
    if strcmp(get(handles.auto_hide,'Checked'),'on')
        pause(handles.displayperiod);
        setWindowState(handles.guifig,'minimize'); %sets h to minimize
    end
catch
    errstr = lasterr;
    errordlg([errstr 'or Not able to AutoHide'],'Auto Hide Error');
end

if strcmp(get(handles.auto_hide,'Checked'),'off')%This is useful to keep a pause of the same time irrespective of auto hide on or off
    pause(handles.displayperiod);
end
set(hObject,'Enable','on');
set(hObject,'String','Manual Inspect');

guidata(hObject, handles);





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




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

if get(handles.auto_inspect,'Value') == 1%stops auto inspect if set to on
    set(handles.auto_inspect,'Value',0);
    set(handles.auto_inspect,'BackGroundColor',[236 233 216]/255);
    stop(handles.tmr);
end

if strcmp(get(handles.on_top,'Checked'),'on')%Checks if on_top is on or off
    set(hObject,'Checked','off');
    setWindowOnTop(handles.guifig,'false');
end
h4 = control_panel; %calls the control panel
waitfor(h4); %until control panel is not destroyed the master window waits

[handles.data,flag] = intialcheck; %This function initialcheck checks and loads the color solutions found in the specified directory ,<root>\data\

guidata(hObject, handles);

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



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

if strcmp(get(hObject,'Checked'),'on')%Checks if animation is on or off
    set(hObject,'Checked','off');
else
    set(hObject,'Checked','on');
end

guidata(hObject, handles);

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

if strcmp(get(hObject,'Checked'),'on')%Checks if sound alert is on or off
    set(hObject,'Checked','off');
else
    set(hObject,'Checked','on');
end

guidata(hObject, handles);


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

if strcmp(get(hObject,'Checked'),'on')%Checks if auto-hide is on or off
    set(hObject,'Checked','off');
else
    set(hObject,'Checked','on');
end

guidata(hObject, handles);

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

if strcmp(get(hObject,'Checked'),'on')%Checks if on_top is on or off
    set(hObject,'Checked','off');
    setWindowOnTop(handles.guifig,'false');
else
    set(hObject,'Checked','on');
    setWindowOnTop(handles.guifig,'true');
end

guidata(hObject, handles);

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

splash('thirdeye.kon','png');%shows the splash screen 


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text2.
function text2_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to text2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
web('http://www.konem.com/index-3.html','-browser');

% --------------------------------------------------------------------
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)

if get(handles.auto_inspect,'Value') == 1 %give warning to turn off Auto Inspect
    hwarn = warndlg('Please Turn-Off Auto Inspect before exiting','Auto Inspect warning');
    setWindowOnTop(hwarn,'true');
    return;
end
try
    if strcmp(get(handles.tmr,'Running'),'on'), stop(handles.tmr);end;
    delete(timerfindall);
end

position = get(handles.guifig,'Position');
animation = get(handles.animation,'Checked');
sound_alert = get(handles.sound_alert,'Checked');
auto_hide = get(handles.auto_hide,'Checked');
on_top = get(handles.on_top,'Checked');
save('startupdata.mat','animation','sound_alert','auto_hide','position','on_top'); %saves the gui updates in the mat file


delete(get(0,'Children'));



% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


if get(handles.auto_inspect,'Value') == 1 %give warning to turn off Auto Inspect
    hwarn = warndlg('Please Turn-Off Auto Inspect before exiting','Auto Inspect warning');
    setWindowOnTop(hwarn,'true');
    return;
end

try
    if strcmp(get(handles.tmr,'Running'),'on'), stop(handles.tmr);end;
    delete(timerfindall);
end

position = get(handles.guifig,'Position');
animation = get(handles.animation,'Checked');
sound_alert = get(handles.sound_alert,'Checked');
auto_hide = get(handles.auto_hide,'Checked');
on_top = get(handles.on_top,'Checked');
save('startupdata.mat','animation','sound_alert','auto_hide','position','on_top'); %saves the gui updates in the mat file


delete(get(0,'Children'));





Contact us at files@mathworks.com