%在控件本身函数中用hObject调用
%在别的函数中,需要使用handles调用
function varargout = TestGUI(varargin)
% TESTGUI MATLAB code for TestGUI.fig
% TESTGUI, by itself, creates a new TESTGUI or raises the existing
% singleton*.
%
% H = TESTGUI returns the handle to a new TESTGUI or the handle to
% the existing singleton*.
%
% TESTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TESTGUI.M with the given input arguments.
%
% TESTGUI('Property','Value',...) creates a new TESTGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before TestGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to TestGUI_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 TestGUI
% Last Modified by GUIDE v2.5 06-Oct-2015 17:26:26
% Begin initialization code - DO NOT EDIT
global isRight;
isRight = 0;
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @TestGUI_OpeningFcn, ...
'gui_OutputFcn', @TestGUI_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
%这里练习使用helpdlg
if isRight
helpdlg('第一步导入图像/视频;第二步选择功能;第三步点击‘Start’开始处理图像/视频(多幅图像/视频多次点击);第四步点击‘Exit’退出界面');
end
% --- Executes just before TestGUI is made visible.
function TestGUI_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 TestGUI (see VARARGIN)
% 定义全局变量
global isVideo;
isVideo = 0;
global isImage;
isImage = 0;
global Count;
Count = 0;
global isRight;
% Choose default command line output for TestGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes TestGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%简单的密码验证,这里主要是练习使用inputdlg
prompt = {'Name = hen','Code = 1989'};
title = '请输入用户名和密码';
lines = [2,1]';
AvailNameCode = {'hen','1989'};
% FalseNameCode = {'***','***'};
FalseNameCode = AvailNameCode;
answer = inputdlg(prompt,title,lines,FalseNameCode);
if length(answer) == 2 && strcmp(answer{1},AvailNameCode{1}) && strcmp(answer{2},AvailNameCode{2})
msgbox('正确,点击 确定 以继续'); %练习使用msgbox
isRight = 1;
else
errordlg('用户名或密码错误');
% close(gcf); %但是会导致崩溃
end
% --- Outputs from this function are returned to the command line.
function varargout = TestGUI_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
global isRight;
varargout{1} = handles.output;
if ~isRight
close(gcf); %放到这里就不会导致程序崩溃了
end
% --------------------------------------------------------------------
function ImageIO_Callback(hObject, eventdata, handles)
% hObject handle to ImageIO (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function VideoIO_Callback(hObject, eventdata, handles)
% hObject handle to VideoIO (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function ReadVideoIO_Callback(hObject, eventdata, handles)
% hObject handle to ReadVideoIO (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%读入视频,前三个是第一次申明,第四个是引用
global InputVideo;
global ResultVideo;
global VideoFullPath;
global isVideo;
[ReadVideoFileName,ReadVideoPathName,ReadVideoFilterIndex] = uigetfile({'*.avi;*.mp4','VideoFile(*.avi,*.mp4)';'*.avi','AVIVideoFile(*.avi)';'*.*','AllFile(*.*)'},'ReadVideo',...
'MultiSelect','on',... %是否能够多选,'off'不支持多选, 'on'支持多选
'C:\Users\hsw\Desktop'); %设置默认路径
if isequal(ReadVideoFileName,0) || isequal(ReadVideoPathName,0) || isequal(ReadVideoFilterIndex,0)
msgbox('导入视频失败,点击 确定 关闭对话框,再重新导入');
else
%支持多选时需要处理
isVideo = 1;
if iscell(ReadVideoFileName)
%读入多个视频时
InputVideo = cell(length(ReadVideoFileName),1);
VideoFullPath = InputVideo;
for IterVideo = 1:length(ReadVideoFileName)
VideoFullPath{IterVideo} = fullfile(ReadVideoPathName,ReadVideoFileName{IterVideo}); %先保存所有视频或图像路径
end
VideoObject = VideoReader(VideoFullPath{1});
else
%只读入一个视频时
VideoFullPath = fullfile(ReadVideoPathName,ReadVideoFileName);
VideoObject = VideoReader(VideoFullPath);
end
% 显示第一个视频的第一帧,直到按下Start按钮时,开始显示别的
frame = read(VideoObject,1);
axes(handles.OriginalAxes);
imshow(frame);
axes(handles.ResultAxes);
imshow(255*ones(size(frame)));
ResultVideo = InputVideo;
msgbox('成功导入视频,点击 确定 关掉对话框');
end
% --------------------------------------------------------------------
function SaveVideoIO_Callback(hObject, eventdata, handles)
% hObject handle to SaveVideoIO (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%保存视频
[SaveVideoFileName,SaveVideoPathName,SaveVideoFilterIndex] = uiputfile({'*.avi;*.mp4','VideoFile(*.avi,*.mp4)';...
'*.avi','AVIVideoFile(*.avi)';'*.*','AllFile(*.*)'},'ReadVideo',...
'C:\Users\hsw\Desktop'); %设置默认路径
if isequal(SaveVideoFileName,0) || isequal(SaveVideoPathName,0) || isequal(SaveVideoFilterIndex,0)
disp('User seleceted Cancel');
else
%这里保存所有读入的视频的处理结果
end
% --------------------------------------------------------------------
function ReadImageIO_Callback(hObject, eventdata, handles)
% hObject handle to ReadImageIO (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%读入图像,前三个是第一次申明,第四个是引用
global InputImage;
global ResultImage;
global ImageFullPath;
global isImage;
[ReadImageFileName,ReadImagePathName,ReadImageFilterIndex] = uigetfile({'*.jpg;*.png;*.tif','ImageFile(*.jpg;*.png;*.tif)';...
'*.jpg','JPEGImageFile(*.jpg)';'*.*','AllFile(*.*)'},'ReadImage',...
'MultiSelect','on',... %是否能够多选,'off'不支持多选, 'on'支持多选
'C:\Users\hsw\Desktop'); %设置默认路径
if isequal(ReadImageFileName,0)|| isequal(ReadImagePathName,0) || isequal(ReadImageFilterIndex,0)
msgbox('导入图像失败,点击 确定 关闭对话框,再重新导入');
else
% 支持多选时,注意需要分别处理
isImage = 1;
if iscell(ReadImageFileName)
%读入多个图像时,名称为cell数组,多个图像必须在同一个目录
InputImage = cell(length(ReadImageFileName),1);
ImageFullPath = InputImage;
for IterImage = 1:length(ReadImageFileName)
ImageFullPath{IterImage} = fullfile(ReadImagePathName,ReadImageFileName{IterImage});
end
FirstImageFullPath = ImageFullPath{1};
else
%只读入一个视频时
FirstImageFullPath = fullfile(ReadImagePathName,ReadImageFileName);
ImageFullPath = FirstImageFullPath;
end
%显示第一张图片
axes(handles.OriginalAxes);
imshow(imread(FirstImageFullPath));
axes(handles.ResultAxes);
imshow(255*ones(size(imread(FirstImageFullPath))));
ResultImage = InputImage;
msgbox('成功导入图像,点击 确定 关掉对话框');
end
% --------------------------------------------------------------------
function SaveImageIO_Callback(hObject, eventdata, handles)
% hObject handle to SaveImageIO (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%保存图像:保存当前结果
[SaveImageFileName,SaveImagePathName,SaveImageFilterIndex] = uiputfile({'*.jpg;*.png;*.tif','ImageFile(*.jpg;*.png;*.tif)';...
'*.jpg','JPEGImageFile(*.jpg)';'*.*','AllFile(*.*)'},'SaveImage','C:\Users\heshiwen\Desktop');
if isequal(SaveImageFileName,0) || isequal(SaveImagePathName,0) || isequal(SaveImageFilterIndex,0)
disp('User selected Cancel');
else
%保存处理的结果
end
% --- Executes on button press in StartPushButton.
function StartPushButton_Callback(hObject, eventdata, handles)
% hObject handle to StartPushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%开始执行功能
%需要再次申明全局变量
global InputVideo;
global ResultVideo;
global VideoFullPath;
global isVideo;
global InputImage;
global ResultImage;
global ImageFullPath;
global isImage;
global Count;
% handles 不清楚有哪些按钮可以输出handles查看
handles
MaxNum = str2double(get(handles.edit1,'String')); %练习获取可编辑文本框中的值
disp(num2str(MaxNum));
ChooseFunctions1 = get(handles.radiobutton1,'Value'); %执行radiobutton对应功能
ChooseFunctions2 = get(handles.radiobutton2,'Value'); %执行radiobutton对应功能
if isImage %处理图像
if ChooseFunctions1 %进行图像翻转
if iscell(ImageFullPath) && Count < length(ImageFullPath) %处理多幅图像
InputImage = imread(ImageFullPath{Count + 1});
ResultImage{Count + 1} = 255*ones(size(InputImage)) - double(InputImage);
axes(handles.OriginalAxes);
imshow(InputImage);
axes(handles.ResultAxes);
imshow(ResultImage{Count + 1}/255,[]);
Count = Count + 1;
set(handles.edit2,'String',length(ImageFullPath) - Count); %设置可编辑对话框中的值
% set(handles.edit2,'String',[1,2;3,4]);
if Count == length(ImageFullPath)
msgbox('图像处理完!');
Count = 0;
end
elseif ~iscell(ImageFullPath) && Count < 1
InputImage = imread(ImageFullPath);
ResultImage = 255*ones(size(InputImage)) - double(InputImage);
axes(handles.OriginalAxes);
imshow(InputImage);
axes(handles.ResultAxes);
imshow(ResultImage/255,[]);
Count = Count + 1;
set(handles.edit2,'String',0);
if Count == 1
msgbox('图像处理完!');
Count = 0;
end
end
elseif ChooseFunctions2 %进行直方图均衡化
msgbox('没有实现!');
else
errordlg('程序出错了!');
end
elseif isVideo
if ChooseFunctions1
if iscell(VideoFullPath) && Count < length(VideoFullPath)
VideoObject = VideoReader(VideoFullPath{Count + 1});
for IterVideo = 1:VideoObject.NumberOfFrames
InputFrame = read(VideoObject,IterVideo);
ResultFrame = 255*ones(size(InputFrame)) - double(InputFrame);
axes(handles.OriginalAxes);
imshow(InputFrame);
axes(handles.ResultAxes);
imshow(ResultFrame/255,[]);
end
Count = Count + 1; %改如何保存呢?
elseif ~iscell(ImageFullPath) && Count < 1
Count = Count + 1;
end
elseif ChooseFunction2
msgbox('没有实现!');
else
errordlg('程序出错了!');
end
else
errordlg('需要先导入图像/视频');
end
% --- Executes on button press in ExitPushButton.
function ExitPushButton_Callback(hObject, eventdata, handles)
% hObject handle to ExitPushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%退出导航
close(gcf);
% msgbox('Exit !!');
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
ValueOfSlider = get(hObject,'Value'); %获取值
set(handles.edit3,'String',ValueOfSlider);
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit3 as text
% %string就是我们先要获得或显示的部分
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
contents = cellstr(get(hObject,'String')); %弹出式列表中每一行的”文字“
msgbox(['你选择的条目为 = ',contents{1}]);
popupmenuvalue = get(hObject,'Value'); %选择时对应的行
msgbox(num2str(popupmenuvalue));
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
contents = cellstr(get(hObject,'String'));
contents
contentsvalue = get(hObject,'Value');
msgbox(num2str(contentsvalue));
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1
% 可以实现:开始--结束--开始...
togglevalue = get(hObject,'Value');
if togglevalue == 0
msgbox('togglevalue = 1');
set(hObject,'String','播放');
elseif togglevalue == 1
msgbox('togglevalue = 0');
set(hObject,'String','停止');
else
errordlg('togglebutton控件设置错误');
end
%复选框
% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox2
% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject handle to checkbox3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox3
% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
% --- Executes when selected cell(s) is changed in uitable1.
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
注意:其中的表格和面板两个,可能需要使用uitable和uipanel来添加比较合适,可以参考matlab本身help文档