Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
X Y location in image with mouse click?

Subject: X Y location in image with mouse click?

From: Tom Sutherland

Date: 9 Aug, 2012 15:34:13

Message: 1 of 5

I have an image that I display in a figure using imshow(),
I want to click the image with the mouse and get the [y,x] location in the image frame.
How do I do this?
Thanks...Tom

Subject: X Y location in image with mouse click?

From: Barry Williams

Date: 9 Aug, 2012 17:09:15

Message: 2 of 5

"Tom Sutherland" wrote in message <k00l9l$10e$1@newscl01ah.mathworks.com>...
> I have an image that I display in a figure using imshow(),
> I want to click the image with the mouse and get the [y,x] location in the image frame.
> How do I do this?
> Thanks...Tom

Tools --> Data Cursor
Barry

Subject: X Y location in image with mouse click?

From: Tom Sutherland

Date: 9 Aug, 2012 17:51:17

Message: 3 of 5

"Barry Williams" <barry.r.williamsnospam@saic.com> wrote in message <k00qrr$lc7$1@newscl01ah.mathworks.com>...
> "Tom Sutherland" wrote in message <k00l9l$10e$1@newscl01ah.mathworks.com>...
> > I have an image that I display in a figure using imshow(),
> > I want to click the image with the mouse and get the [y,x] location in the image frame.
> > How do I do this?
> > Thanks...Tom
>
> Tools --> Data Cursor
> Barry

Sorry, I forgot to add the I want to use the xy coordinates in a calculation inside a GUI.

Subject: X Y location in image with mouse click?

From: Barry Williams

Date: 9 Aug, 2012 19:07:17

Message: 4 of 5

"Tom Sutherland" wrote in message <k00tal$lq$1@newscl01ah.mathworks.com>...
> "Barry Williams" <barry.r.williamsnospam@saic.com> wrote in message <k00qrr$lc7$1@newscl01ah.mathworks.com>...
> > "Tom Sutherland" wrote in message <k00l9l$10e$1@newscl01ah.mathworks.com>...
> > > I have an image that I display in a figure using imshow(),
> > > I want to click the image with the mouse and get the [y,x] location in the image frame.
> > > How do I do this?
snip
> Sorry, I forgot to add the I want to use the xy coordinates in a calculation inside a GUI.

How about using ginput?
Barry

Subject: X Y location in image with mouse click?

From: William Frane

Date: 9 Aug, 2012 20:29:17

Message: 5 of 5

"Tom Sutherland" wrote in message <k00tal$lq$1@newscl01ah.mathworks.com>...
> "Barry Williams" <barry.r.williamsnospam@saic.com> wrote in message <k00qrr$lc7$1@newscl01ah.mathworks.com>...
> > "Tom Sutherland" wrote in message <k00l9l$10e$1@newscl01ah.mathworks.com>...
> > > I have an image that I display in a figure using imshow(),
> > > I want to click the image with the mouse and get the [y,x] location in the image frame.
> > > How do I do this?
> > > Thanks...Tom
> >
> > Tools --> Data Cursor
> > Barry
>
> Sorry, I forgot to add the I want to use the xy coordinates in a calculation inside a GUI.

If you intend to show the image in a GUI, you may find the WindowButtonMotionFcn and WindowButtonDownFcn figure properties and the CurrentPoint axes property useful.

For example, if, in your GUI, you have a set of axes called axesImage within which an image will be displayed, you could do something along the lines of the following code:

Inside the GUI's OpeningFcn or the callback function for a button:
set(gcf, 'WindowButtonDownFcn', @getMousePositionOnImage);
set(src, 'Pointer', 'crosshair'); % Optional
pan off % Panning will interfere with this code

Then have a mouse callback function along the lines of:
function getMousePositionOnImage(src, event)

handles = guidata(src);

cursorPoint = get(handles.axesImage, 'CurrentPoint');
curX = cursorPoint(1,1);
curY = cursorPoint(1,2);

xLimits = get(handles.axes1, 'xlim');
yLimits = get(handles.axes1, 'ylim');

if (curX > min(xLimits) && curX < max(xLimits) && curY > min(yLimits) && curY < max(yLimits))
disp(['Cursor coordinates are (' num2str(curX) ', ' num2str(curY) ').']);
else
disp('Cursor is outside bounds of image.');
end

end

Hope that helps.

Will

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us