最近项目要求用MATLAB设计GUI处理图片,看了几天的书还是毫无头绪,不管了直接开始从最简单的开始写,慢慢查资料终于写出个雏形,可以用来选择图片灰度化或二值化图形,其他的功能在学习其他算法后可以添加,基本没有自己要写的函数,毕竟MATLAB最大的优势就是函数丰富,需要的函数MATLAB函数库都有,但也学到了各种UI变量的调用,还学习了和visual c++混合编译生成exe文件,网上看到大神用matlab写的代码几乎无所不能,也希望自己一点一点进步,学好语言,学好算法,一通百通吧,权以这篇日志作为好的开篇,加油!
function varargout = dipfinal(varargin)
% DIPFINAL MATLAB code for dipfinal.fig
% DIPFINAL, by itself, creates a new DIPFINAL or raises the existing
% singleton*.
%
% H = DIPFINAL returns the handle to a new DIPFINAL or the handle to
% the existing singleton*.
%
% DIPFINAL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DIPFINAL.M with the given input arguments.
%
% DIPFINAL('Property','Value',...) creates a new DIPFINAL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before dipfinal_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to dipfinal_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 dipfinal
% Last Modified by GUIDE v2.5 26-Oct-2015 16:19:36
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @dipfinal_OpeningFcn, ...
'gui_OutputFcn', @dipfinal_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 dipfinal is made visible.
function dipfinal_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 dipfinal (see VARARGIN)
% Choose default command line output for dipfinal
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes dipfinal wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = dipfinal_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;
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
% --- Executes on button press in imageinput_pushbutton.
function imageinput_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to imageinput_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global filepath;
[filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' });
filepath=[pathname filename];
set(handles.edit1,'String', filepath);
% --- Executes on button press in confirm_pushbutton.
function confirm_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to confirm_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global filepath;
origraph=imread(filepath);
axes(handles.original_axes);
axis off;
imshow(origraph);
pgray=rgb2gray(origraph);
pbin=im2bw(pgray);
axes(handles.current_axes);
switch get(handles.imageprocessing_popupmenu,'value')
case 1
imshow(pgray);
case 2
imshow(pbin);
end
% --- Executes on selection change in imageprocessing_popupmenu.
function imageprocessing_popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to imageprocessing_popupmenu (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 imageprocessing_popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from imageprocessing_popupmenu
% --- Executes during object creation, after setting all properties.
function imageprocessing_popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to imageprocessing_popupmenu (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