基于matlab 机器视觉颜色识别系统
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
版本:2014a
完整代码或代写加1564658423