简体   繁体   中英

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'. 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

function figure1_WindowButtonMotionFcn(~, ~, ~)

Then the figure1 handle should have a property currentPoint that will describe the position of the mouse. If you write a click event function that has access to the figure1 handle, something like this:

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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