User input from clicking on a position on a diagram/image
18 views (last 30 days)
Show older comments
Hello,
I was wondering whether it was possible to get a use an image to get an input for matlab.
Take a football pitch for example. Would it be possible to get a birds eye image of a pitch and split it up into a grid, the user pics a particular square in the grid which then returns a x,y co-ordinate to matlab.
Its sort of possible using VBA in excel, however I'm not as handy with Matlab as I am excel.
Thanks for your time.
0 Comments
Accepted Answer
Image Analyst
on 12 Mar 2012
Yes. Use ginput() to get a point. Then convert that x,y point into calibrated units using your knowledge of how many pixels correspond to how many real world units (such as meters).
3 Comments
RAGAV K
on 9 Jul 2012
I did the same, below is the piece of code:
axis ij;
axis manual;
[m n] = size(d1); %%d1 is the image which I want to display
axis([0 m 0 n])
imshow(d1);
coordinates_input = round(ginput(1));
But when I checked the value of coordinates_input it was [25.5000,91.5000];
How do I change this so that I get pixel position in whole numbers?
Image Analyst
on 9 Jul 2012
That will happen if your image doesn't have unit magnification. For example if it's magnified so that one image pixel takes up 9 screen pixels, you can click "in the middle of a pixel" and the coordinates will give you that fractional location. Rounding, as you've done, seems like a reasonable way to return the pixel closest to where the user clicked. See this demo to understand:
d1 = rand(40);
axis ij;
axis manual;
[m n] = size(d1); %%d1 is the image which I want to display
axis([0 m 0 n])
imshow(d1);
zoom(8)
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0.5 1 0.5]);
coordinates_input = ginput(1)
row = round(coordinates_input(2));
column = round(coordinates_input(1));
fprintf('You clicked on coordinates (row, column) = (%f, %f)\nWhich is the pixel in row %d, column %d\n', ...
coordinates_input(1), coordinates_input(2), row, column);
More Answers (0)
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!