• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

MATLAB制作GUI—GUI中图像坐标获取②(datacursormode)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

上次讲的是通过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公司的帮助文档~

祝大家在学习的道路上一帆风顺~

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi中用beep函数拼出节奏和歌曲 - 癫狂编程发布时间:2022-07-18
下一篇:
Delphi中beep函数发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap