二、部分源代码
function varargout = Color_Detect(varargin)
% COLOR_DETECT MATLAB code for Color_Detect.fig
% COLOR_DETECT, by itself, creates a new COLOR_DETECT or raises the existing
% singleton*.
%
% H = COLOR_DETECT returns the handle to a new COLOR_DETECT or the handle to
% the existing singleton*.
%
% COLOR_DETECT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in COLOR_DETECT.M with the given input arguments.
%
% COLOR_DETECT('Property','Value',...) creates a new COLOR_DETECT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Color_Detect_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Color_Detect_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 Color_Detect
% Last Modified by GUIDE v2.5 22-Jul-2021 12:54:19
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Color_Detect_OpeningFcn, ...
'gui_OutputFcn', @Color_Detect_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 Color_Detect is made visible.
function Color_Detect_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 Color_Detect (see VARARGIN)
% Choose default command line output for Color_Detect
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
set(handles.axes1,'Visible','off');
set(handles.axes2,'Visible','off');
set(handles.uipanel1,'visible','off');
set(handles.uipanel2,'visible','off');
set(handles.start,'Enable','off');
set(handles.snap,'Enable','off');
set(handles.oi,'visible','off');
set(handles.dc,'visible','off');
set(handles.uipanel3,'visible','off');
set(handles.startv,'enable','off');
set(handles.text,'enable','off');
set(handles.set,'enable','off');
set(handles.pop,'enable','off');
set(handles.text5,'visible','off');
% UIWAIT makes Color_Detect wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Color_Detect_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 image.
function image_Callback(hObject, eventdata, handles)
% hObject handle to image (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.uipanel1,'visible','on');
set(handles.video,'Enable','off');
% --- Executes on button press in video.
function video_Callback(hObject, eventdata, handles)
% hObject handle to video (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.image,'Enable','off');
set(handles.text,'enable','on');
set(handles.pop,'enable','on');
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
im=handles.im; x=handles.x;
switch x
case 1
axes(handles.axes2); cla;
im=handles.im;
r=im(:,:,1); g=im(:,:,2); b=im(:,:,3);
diff=imsubtract(r,rgb2gray(im));
bw=im2bw(diff,0.18);
area=bwareaopen(bw,300);
rm=immultiply(area,r); gm=g.*0; bm=b.*0;
image=cat(3,rm,gm,bm);
imshow(image);
% Transparcy part
%[m n p]=size(im);
% mask=ones(m,n);
%i=find(image(:,:,1)==0);
% mask(i)=.9; % Change Transparacy
%imshow(im);
%hold on
%h=imshow(image);
%set(h,'AlphaData',mask);
case 2
axes(handles.axes2); cla;
im=handles.im;
r=im(:,:,1); g=im(:,:,2); b=im(:,:,3);
diff=imsubtract(g,rgb2gray(im));
bw=im2bw(diff,0.18);
area=bwareaopen(bw,300);
gm=immultiply(area,g); rm=r.*0; bm=b.*0;
image=cat(3,rm,gm,bm);
imshow(image);
case 3
axes(handles.axes2); cla;
im=handles.im;
r=im(:,:,1); g=im(:,:,2); b=im(:,:,3);
diff=imsubtract(b,rgb2gray(im));
bw=im2bw(diff,0.18);
area=bwareaopen(bw,300);
bm=immultiply(area,b); gm=g.*0; rm=r.*0;
image=cat(3,rm,gm,bm);
imshow(image);
end
set(handles.dc,'visible','on');
% --- Executes on button press in red.
function red_Callback(hObject, eventdata, handles)
% hObject handle to red (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.x=1;
guidata(hObject,handles);
set(handles.start,'enable','on');
% --- Executes on button press in green.
function green_Callback(hObject, eventdata, handles)
% hObject handle to green (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.x=2;
guidata(hObject,handles);
set(handles.start,'enable','on');
% --- Executes on button press in blue.
function blue_Callback(hObject, eventdata, handles)
% hObject handle to blue (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.x=3;
set(handles.start,'enable','on');
guidata(hObject,handles);
% --- Executes on button press in camera.
function camera_Callback(hObject, eventdata, handles)
% hObject handle to camera (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
imaqreset
info= imaqhwinfo;
name = char(info.InstalledAdaptors(end));
c_info = imaqhwinfo(name);
id = c_info.DeviceInfo.DeviceID(end);
format = char(c_info.DeviceInfo.SupportedFormats(end));
handles.vid= videoinput(name, id, format);
axes(handles.axes1);cla;
h = waitbar(0,'Please wait...');
steps = 200;
for step = 1:steps
% computations take place here
waitbar(step / steps)
end
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.