i have found this code over internet.But i need its output coordinates in x and y variables. How i can do that ?

1 view (last 30 days)
function [ x1,y1,x2,y2 ] = image_Coordinates
clc;clear;
imObj = imread('Finalized_Lhr_maps_Half.jpg');
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
% ah = get( imageHandle, 'Parent' );
% p = get( ah, 'CurrentPoint' )
end

Answers (2)

Walter Roberson
Walter Roberson on 28 Apr 2016
[x(1), y(1), x(2), y(2)] = image_Coordinates();
  4 Comments
Stephen23
Stephen23 on 29 Apr 2016
Edited: Stephen23 on 29 Apr 2016
You need to allocate values to x1,y1,x2,y2 inside the function. MATLAB does not just invent some values for these: if you do not define inside the function what value x1 has, then what value do you expect the x1 output variable to have?
function out = myFun()
out = 4; % define the output variable
end

Sign in to comment.


Image Analyst
Image Analyst on 29 Apr 2016
I suggest you just learn how to use impixelinfo(). It's better because it gives you a "live" readout as you move the cursor over the image. No clicking even needed at all.
If you need to get the values into variables for use in your code, then I suggest you have a mode where the user does that, like a button where the button callback calls ginput(1). But if it's just for visual inspection, then I recommend impixelinfo().

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!