今天,将介绍GUI中获取图像坐标的最后一种方法—通过axes坐标轴的回调函数实现坐标的获取。
今天使用的回调函数是axes中的ButtonDownFcn函数,当鼠标在axes中点击时,MATLAB就会调用ButtonDownFcn函数。
下面是该GUI的gif图:
下面贴出实现代码:
function varargout = Getposition3(varargin)
% GETPOSITION3 MATLAB code for Getposition3.fig
% GETPOSITION3, by itself, creates a new GETPOSITION3 or raises the existing
% singleton*.
%
% H = GETPOSITION3 returns the handle to a new GETPOSITION3 or the handle to
% the existing singleton*.
%
% GETPOSITION3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GETPOSITION3.M with the given input arguments.
%
% GETPOSITION3('Property','Value',...) creates a new GETPOSITION3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Getposition3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Getposition3_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 Getposition3
% Last Modified by GUIDE v2.5 14-Apr-2019 12:00:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Getposition3_OpeningFcn, ...
'gui_OutputFcn', @Getposition3_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 Getposition3 is made visible.
function Getposition3_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 Getposition3 (see VARARGIN)
% Choose default command line output for Getposition3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Getposition3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Getposition3_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pic = imread('trees.tif');
handles.im = imshow(pic);
axis on
axes(handles.axes1);
set(handles.im,'Pickableparts','none');
set(handles.axes1,'ButtonDownFcn',@mycallbackfcn);
function mycallbackfcn(hObject,eventdata,handles)
handles = guidata(hObject);
pos = get(hObject,'Currentpoint');
set(handles.edit1,'string',pos(1,1));
set(handles.edit2,'string',pos(1,2));
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 during object creation, after setting all properties.
function pushbutton1_CreateFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
一开始实现这个功能的时候,自己没有发现这种方式实现,后面是在师兄的指导下实现的,其中最重要的就是设置图像显示句柄中的Pickableparts参数。
其中Pickableparts参数是确实对象是否捕获鼠标点击,同时HitTest参数是确定对象是否响应鼠标点击或将其传递给最近的父级。
下面是这两个参数设置时实现的功能:
具体详情请看MATLAB公司的帮助文档:https://ww2.mathworks.cn/help/matlab/creating_plots/capturing-mouse-clicks.html
通过这种方法实现虽然比前两种方法都要好,但是还是存在一些缺点:
①因为使用的是axes的回调函数,因此axis必须打开,当使用imshow函数时,必须使用axis on将axis打开(imshow默认关闭axis),而使用imagesc时axis默认打开;
②从gif图中可以看出当获取图像外的坐标区域时同样也可以获取坐标信息,因为坐标区域仍然也是axes区域。
---------------------------------------------------------------------------------------------------------------------
虽然这三种方法都可以获取GUI图像上的坐标,但是都存在缺点,如果大家有更好的办法,希望能分享,最后祝大家在学习的道路上一帆风顺~