Code covered by the BSD License  

Highlights from
Machine Vision System - Color Recognition

image thumbnail

Machine Vision System - Color Recognition

by

 

03 Mar 2008 (Updated )

This gui is a demonstration on the concept of machine vision system for color recognition

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

% Last Modified by GUIDE v2.5 17-Nov-2007 16:43:45

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

handles.guifig = gcf;
axes(handles.axes1);
handles.test_image = im2uint8(ones([240 320 3])); %set it to white color initially
handles.h2 = image(handles.test_image);%white image on the axis
axis off;
set(handles.messages,'String','Take a Snap.');

% handles.count = 1;%intialiase couter for the markers places in place markers function
set(handles.roi,'enable','off');
set(handles.place_marker,'enable','off');
set(handles.save_solution,'enable','off');

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

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

% Update handles structure
guidata(hObject, handles);

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



% --- Outputs from this function are returned to the command line.
function varargout = control_panel_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);

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
set(handles.h2,'CData',handles.reference_image);%update image on the axes
drawnow;

guidata(handles.guifig, handles);


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

stop(handles.tmr);%stops the timer function
set(hObject,'enable','off');
handles.reference_image = getimage; %gets current image from the axis
set(handles.h2,'CData',handles.reference_image); %places the image onto the axis

[data,flag] = intialcheck;%check whether any intial data is stored on the data folder, this is to avoid taking the region of interest again

if strcmp(flag,'good') %if any previous solutions are found, take the coordinates from the earlier solution file
    temp_data  = data(1); %take values from 1st solution color file
    handles.rect = temp_data.rect;%rect coordinates stored in this handles structure from the stored solution file
    handles.x = temp_data.x;%x coordinates stored in this handles structure from the stored solution file
    handles.y = temp_data.y;%y coordinates stored in this handles structure from the stored solution file
    handles.cropped_image = imcrop(handles.reference_image,handles.rect);
    handles.lab_value = lab_color_fcn(handles.cropped_image,handles.x,handles.y);%lab color function, not used in this program
    handles.color_val = validate_color_ref(handles.cropped_image);%main color finding function

    set(hObject,'enable','off');
    set(handles.save_solution,'enable','on');
    set(handles.messages,'String','Save solution as Color Name');
    rectangle('Position',[handles.rect(1),handles.rect(2),handles.rect(3),handles.rect(4)],'edgecolor','g','linewidth',2);
    guidata(hObject, handles);
    return;
else
    set(handles.roi,'enable','on');
    set(hObject,'enable','off');
    set(handles.messages,'String','Mark the Region of Interest');
end

guidata(hObject, handles);


% --- Executes on button press in roi.
function roi_Callback(hObject, eventdata, handles)
% hObject    handle to roi (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.messages,'String','Select Color ROI from the Region Of Interest');
axes(handles.axes1);
[handles.cropped_image,rect1] = imcrop;
rectangle('Position',[rect1(1),rect1(2),rect1(3),rect1(4)],'edgecolor','g','linewidth',2);
handles.rect = rect1; %cropped image coordinates in handles structure
set(handles.place_marker,'enable','on');
set(hObject,'enable','off');
guidata(hObject, handles);


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


h1 = figure; title('Draw a Region of Interest to Pick the Color');
imshow(handles.cropped_image);
set(h1,'WindowStyle','modal');  %keep it on top
drawnow;
[J,x,y] = roipoly;


handles.x = x;%in handles structure
handles.y = y;%in handles structure
hold on;
plot(x,y,'color','white','linewidth',2); %draws the ROI on the existing image
delete(h1); %removes the cropped image figure

handles.lab_value = lab_color_fcn(handles.cropped_image,x,y);%lab color function, not used in this program
handles.color_val = validate_color_ref(handles.cropped_image);%main color finding function

set(handles.save_solution,'enable','on');
set(handles.messages,'String','Save solution as Color Name or Color Code');
set(hObject,'enable','off');
guidata(hObject, handles);



% --- Executes on button press in save_solution.
function save_solution_Callback(hObject, eventdata, handles)
% hObject    handle to save_solution (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
colorcode = '';
colorcode = inputdlg('Enter Color Name');
while strcmp(colorcode,'')
    colorcode = inputdlg('Enter Color Name');
end
try %if user presses cancel or closes the input dialog then return to calling function
    colorcode = colorcode{1};
catch
    return;
end

strdata = [pwd '\data'];
if ~exist(strdata) %check if data folder is created in the root directory
    mkdir(strdata); %create a folder called solutions where all solutions will be stored
end

pathname = [pwd '\data\']; %new directory for storing data files and color solutions
filename = [colorcode '.mat'];

str = [pathname filename];

cropped_image = handles.cropped_image;
lab_value = handles.lab_value;
rect = handles.rect;
x = handles.x;
y = handles.y;
color_val = handles.color_val;%new addition
save(str,'cropped_image','rect','colorcode','lab_value','x','y','color_val'); %save in the name specified above
set(handles.messages,'String','Solution Saved, Click on Exit');

set(hObject,'Enable','off');
set(handles.messages,'String','Solution Saved, Click on Exit');

button = questdlg('Do you want to continue with next vehicle color? Yes to continue','Question','default'); %ask question before proceeding to save
if ~strcmp(button,'Yes'); return; end;

% preview(handles.vid,handles.h);
start(handles.tmr);
set(handles.take_snap,'enable','on');


guidata(hObject, 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)

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



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

% Hint: delete(hObject) closes the figure

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


Contact us