图5.3 新建画布
新建画布代码如下:
function newfile_Callback(hObject, eventdata, handles)
set(0,'currentfigure',gui2); %保留当前界面,并跳转至新建绘图界面“gui2”
Gui2部分相关代码如下:
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
global down pos;
if strcmp(get(gcf,'SelectionType'),'normal')
down=1;
pos=get(handles.axes1,'CurrentPoint');
end
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
global down pos; %设置全局变量
down=0;
% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
global down pos;
if down==1
p=get(handles.axes1,'CurrentPoint');
line([pos(1,1) p(1,1)],[pos(1,2) p(1,2)],'LineWidth',4);
pos=p;
end
图5.4 打开图像
打开图像的相关代码如下:
function open_Callback(hObject, eventdata, handles)
global name path
[filename, pathname] = uigetfile({'*.jpg;*.bmp;*.tif;','MATLAB Files (*.bmp,*.tif,*.jpg)'}, 'Choose a File');
f = imread( strcat(pathname,filename));
path=pathname;
name=filename;
subplot(1,2,1);
imshow(f);
title('原图像','color','g','fontsize',14);
5.3.3 保存图像
保存我们的操作结果几乎在所有的软件中都可以看到,就是将结果保存到指定的路径下,方便我们查看和修改。
运行结果如下图5.5所示
图5.5 保存结果
相关代码如下:
function SaveResult_Callback(hObject, eventdata, handles)
% hObject handle to SaveResult (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uiputfile({'*.bmp';'*.jpg';'*.jpeg'}, 'Pick an image-file');