简体   繁体   English

如何使用Matlab获取图像中的光标位置

[英]How to get cursor position in an image with Matlab

I need to get the cursor position after a click on the image to obtain the corresponding pixel coordinates. 单击图像以获得对应的像素坐标后,我需要获取光标位置。 This is what I've done so far, which works as long as I click on the empty part of the figure (if I click on the image, the callback is not triggered). 到目前为止,这就是我所做的,只要单击该图的空白部分即可正常工作(如果单击该图像,则不会触发回调)。

image(my_image);
set(gca, 'ButtonDownFcn', @click);

function click(o, event)
    pt = get(o, 'CurrentPoint')
end

So afterward, I tried this one : 所以之后,我尝试了这个:

image(my_image, 'ButtonDownFcn', @click);

function click(o, event)
    pt = get(o, 'CurrentPoint')
end

But then, it tells me that the image class does not contain any field named 'CurrentPoint'. 但是,这告诉我图像类不包含任何名为“ CurrentPoint”的字段。 I suppose that I need to retrieve some kind of axes from the image, but I don't know how to do that. 我想我需要从图像中检索某种轴,但是我不知道该怎么做。

I've had to solve a similar problem before. 我不得不解决类似的问题。

If you add an empty callback like the following the gui will track the cursor position 如果您添加如下所示的空回调,则gui将跟踪光标位置

function figure1_WindowButtonMotionFcn(~, ~, ~)

Then the figure1 handle should have a property currentPoint that will describe the position of the mouse. 然后,Figure1句柄应具有一个currentPoint属性,该属性将描述鼠标的位置。 If you write a click event function that has access to the figure1 handle, something like this: 如果编写一个可以访问Figure1句柄的click事件函数,则如下所示:

image(my_image, 'ButtonwDownFcn', ...
    @(hObject,eventdata)myGui('click',hObject,eventdata,guidata(hObject))

include the following line in it to access the mouse position 在其中包含以下行以访问鼠标位置

mouseLocation = get(handles.figure1, 'currentPoint');

Then you'll have to translate the mouse position to pixel position using the position of the axes inside the figure. 然后,您必须使用图形内轴的位置将鼠标位置转换为像素位置。

Well, I found the solution to my problem. 好吧,我找到了解决问题的方法。 Just needed to specify an axis to the image and get it through the 'Parent' attribute. 只需指定图像的轴并通过'Parent'属性即可。

im = image(0, 0, my_image);
set(im, 'ButtonDownFcn', @click);

function click(o, event)
    pt = get(get(o, 'Parent'), 'CurrentPoint')
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM