我使用GUIDE制作了GUI。我有一个轴(标记=axes1)。图像显示在轴上。当我点击图像(轴内)时,我需要得到坐标。当图像没有添加到轴上时,我会得到坐标值。但是图像显示在轴上并没有得到轴。如何获取坐标?
让我们假设您已经用句柄imageHandle
绘制了图像,该句柄为:
imageHandle = imshow(imageObj);
您应该将ButtonDownFcn
分配给图像句柄,而不是轴句柄:
set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
并从该函数中获得鼠标坐标,如下所示:
function ImageClickCallback ( objectHandle , eventData )
axesHandle = get(objectHandle,'Parent');
coordinates = get(axesHandle,'CurrentPoint');
coordinates = coordinates(1,1:2);
%// then here you can use coordinates as you want ...
end
你可以试试这个小演示来检查我的答案:
function demoOnImageClick
clc;clear;
imObj = rand(500,500);
figure;
hAxes = axes();
imageHandle = imshow(imObj);
set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
function ImageClickCallback ( objectHandle , eventData )
axesHandle = get(objectHandle,'Parent');
coordinates = get(axesHandle,'CurrentPoint');
coordinates = coordinates(1,1:2);
message = sprintf('x: %.1f , y: %.1f',coordinates (1) ,coordinates (2));
helpdlg(message);
end
end
您可以通过从图像句柄中获取轴句柄
ah = get( imageHandle, 'Parent' );
然后你可以通过获得鼠标点击位置
p = get( ah, 'CurrentPoint' );