matlab 鼠标指针坐标怎样显示
用matlab编写界面程序时想实现一个功能:当鼠标在窗口滑动时在其周围能显示当先鼠标的坐标位置。请各位大侠不吝赐教!!先谢过了!!...
用matlab编写界面程序时想实现一个功能:当鼠标在窗口滑动时在其周围能显示当先鼠标的坐标位置。请各位大侠不吝赐教!!先谢过了!!
展开
2个回答
展开全部
MATLAB如何动态显示鼠标的坐标值和图像像素值
对于动态显示鼠标的坐标值和像素值,在其他语言如vc,vb中都比较方便,有直接的着方面的函数,那么在matlab图像处理里面又如何实现呢?
具体的实现方法很多,但归结起来就是获取坐标轴的current point 属性值,我这里给出的一个函数是从mathworks 获取柄稍作修改后的结果,相信对做图像处理的朋友有一定的作用。另一个就是自带的pixval函数。谁有不同的实现方法,请多多共享啊!
function dynpoint(arg,h)
% Show the coordinates of a plot dynamically
%
% To start use:
% dynpoint(h)
% where h is a handle to a figure, axes or e.g. line.
%
% To delete use:
% dynpoint('delete',h)
% where h is a handle to a figure, axes or e.g. line.
% (you may also use: dynpoint delete)
%
% There can only be one dynamic plotter in a figure at a time.
%
% Example:
% subplot(211), hline = plot(sin(1:10))
% subplot(212), plot(sin(1:100))
% dynpoint(hline)
% 2002,6.29
if ~exist('arg','var')
arg = gcf;
end
if ~isstr(arg)
handle = arg;
arg = 'init';
end
switch arg
case 'init'
if ~ishandle(handle)
error('h is not a handle')
end
[h,ax] = h2hax(handle);
% delete old dynamic text object
ht = findobj(h,'tag',[mfilename '_text']);
if any(ht)
delete(ht)
end
% text window at the bottom left corner
% text in centred
uicontrol(h,...
'style','text',...
'pos',[2 2 200 15],...
'tag',[mfilename '_text'],...
'userdata',ax(1))
% do the dynamic thing...
set(h,'windowbuttonmotionfcn',[mfilename ' move'])
case 'move'
ht = findobj(gcbf,'tag',[mfilename '_text']);
ax = overobj('axes');
if ~any(ax)
ax = get(ht,'userdata');
end
p = get(ax,'currentpoint');
set(ht,'string',sprintf('(%g, %g)', p(1), p(3)));
case 'delete'
if ~exist('h','var')
h = gcf;
end
[h,ax] = h2hax(h);
set(h,'windowbuttonmotionfcn','')
ht = findobj(h,'tag',[mfilename '_text']);
delete(ht)
end
% ----------
function [h,ax]=h2hax(handle)
typ = get(handle,'type');
if strcmp(typ,'figure')
h = handle;
ax = findobj(h,'type','axes');
elseif strcmp(typ, 'axes')
h = get(handle,'parent');
ax = handle;
elseif strcmp( get(get(handle,'parent'), 'type'), 'axes' )
ax = get(handle,'parent');
h = get(ax,'parent');
end
对于动态显示鼠标的坐标值和像素值,在其他语言如vc,vb中都比较方便,有直接的着方面的函数,那么在matlab图像处理里面又如何实现呢?
具体的实现方法很多,但归结起来就是获取坐标轴的current point 属性值,我这里给出的一个函数是从mathworks 获取柄稍作修改后的结果,相信对做图像处理的朋友有一定的作用。另一个就是自带的pixval函数。谁有不同的实现方法,请多多共享啊!
function dynpoint(arg,h)
% Show the coordinates of a plot dynamically
%
% To start use:
% dynpoint(h)
% where h is a handle to a figure, axes or e.g. line.
%
% To delete use:
% dynpoint('delete',h)
% where h is a handle to a figure, axes or e.g. line.
% (you may also use: dynpoint delete)
%
% There can only be one dynamic plotter in a figure at a time.
%
% Example:
% subplot(211), hline = plot(sin(1:10))
% subplot(212), plot(sin(1:100))
% dynpoint(hline)
% 2002,6.29
if ~exist('arg','var')
arg = gcf;
end
if ~isstr(arg)
handle = arg;
arg = 'init';
end
switch arg
case 'init'
if ~ishandle(handle)
error('h is not a handle')
end
[h,ax] = h2hax(handle);
% delete old dynamic text object
ht = findobj(h,'tag',[mfilename '_text']);
if any(ht)
delete(ht)
end
% text window at the bottom left corner
% text in centred
uicontrol(h,...
'style','text',...
'pos',[2 2 200 15],...
'tag',[mfilename '_text'],...
'userdata',ax(1))
% do the dynamic thing...
set(h,'windowbuttonmotionfcn',[mfilename ' move'])
case 'move'
ht = findobj(gcbf,'tag',[mfilename '_text']);
ax = overobj('axes');
if ~any(ax)
ax = get(ht,'userdata');
end
p = get(ax,'currentpoint');
set(ht,'string',sprintf('(%g, %g)', p(1), p(3)));
case 'delete'
if ~exist('h','var')
h = gcf;
end
[h,ax] = h2hax(h);
set(h,'windowbuttonmotionfcn','')
ht = findobj(h,'tag',[mfilename '_text']);
delete(ht)
end
% ----------
function [h,ax]=h2hax(handle)
typ = get(handle,'type');
if strcmp(typ,'figure')
h = handle;
ax = findobj(h,'type','axes');
elseif strcmp(typ, 'axes')
h = get(handle,'parent');
ax = handle;
elseif strcmp( get(get(handle,'parent'), 'type'), 'axes' )
ax = get(handle,'parent');
h = get(ax,'parent');
end
展开全部
用imtool函数显示
function main()
h0 = figure;
h1 = imshow('1.jpg');
h2 = uicontrol('style','text','Position',[30 15 100 15],'string','non');
set(h1,'ButtonDownFcn',@clicky);
function clicky(varargin)
a=get(gca,'Currentpoint');
set(findobj('style','text'),'String',strcat('x:',num2str(a(1,1)),'y:',num2str(a(1,2))));
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询