MATLAB製作GUI—GUI中的座標獲取②(datacursormode)

上次講的是通過ginput函數進行GUI中圖像座標的獲取,但是該方法存在很多缺點,如美觀、可以獲取圖像外部區域等問題。今天,我將介紹一個較ginput函數較好的GUI上圖像座標獲取函數:datacursormode函數

今天介紹的函數datacursormode其實是圖窗上數據遊標模式的啓用函數,詳細使用方式見MATLAB幫助文檔:

其實數據遊標模式大家肯定經常見,下面就是這種模式打開的狀態:

 圖像上面會顯示遊標所在點的一個信息。

下面的Gif圖就是使用datacursor函數獲取圖像座標的GUI:

下面是實現代碼:

function varargout = Getposition2(varargin)
% GETPOSITION2 MATLAB code for Getposition2.fig
%      GETPOSITION2, by itself, creates a new GETPOSITION2 or raises the existing
%      singleton*.
%
%      H = GETPOSITION2 returns the handle to a new GETPOSITION2 or the handle to
%      the existing singleton*.
%
%      GETPOSITION2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GETPOSITION2.M with the given input arguments.
%
%      GETPOSITION2('Property','Value',...) creates a new GETPOSITION2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Getposition2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Getposition2_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 Getposition2

% Last Modified by GUIDE v2.5 11-Apr-2019 16:36:13

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Getposition2_OpeningFcn, ...
                   'gui_OutputFcn',  @Getposition2_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 Getposition2 is made visible.
function Getposition2_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 Getposition2 (see VARARGIN)

% Choose default command line output for Getposition2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Getposition2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Getposition2_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');
axes(handles.axes1)
imagesc(pic);
for i=1:10
    dcm_obj =datacursormode(gcf);
    set(dcm_obj,'DisplayStyle','datatip','Enable','on')
    pause
    c_info = getCursorInfo(dcm_obj);
    pos = c_info.Position;
    set(handles.edit1,'string',pos(2));
    set(handles.edit2,'string',pos(1));
end


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

通過這種方法獲取GUI圖像上的點的座標有優點也有缺點:

優點:

①這個方法只可以獲取顯示圖像範圍內的座標點,同時顯示的座標爲圖像上點的真實座標。上一篇介紹ginput函數時,大家可能注意到了獲取的座標爲小數,因爲ginput函數獲取的座標是figure上的連續座標,而本文中介紹的datacursormode函數獲取的是顯示圖像上的離散座標點;

②選取的點的信息可以通過datatip顯示在圖像上

缺點:

①使用datacursormode函數前必須預先設置選取點的個數,因爲在獲取datacursormode函數的信息前,必須通過pause等函數進行中斷才能正確獲取其信息。

②也可以通過while和try  catch來實現,但是使用死循環,會可能使程序崩了;

③當使用pause中斷時,沒有點夠相應數目的點就退出程序會出現錯誤。

--------------------------------------------------------------------------------------------------------------------------

因爲自己沒有很好的利用MATLAB公司推出的幫助文檔,該方法的實現過程很坎坷~

因此希望大家在嘗試使用新函數時,一定要仔細查看MATLAB公司的幫助文檔~

祝大家在學習的道路上一帆風順~

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章