Alternative to ginput for selecting data from an image?

26 次查看(过去 30 天)
I've got an image with pixels that are 0.1 units in x and 0,1 units in y. I want to have a user select some number 'n' points on that image and assign the x,y coordinates of those selections to a variable. I have tried to use "ginput" to do this, but there is one issue. When using ginput, if I click in the pixel (1.1, 3.2), for example, I might get values of (1.12034, 3.259034) when what I want is (1.1, 3.2). If I use the data cursor, it provides (1.1, 3.2) but I don't know how to automatically get those values into a variable.
I suppose I could round the coordinates down to the nearest 0.1, but the pixel size will not always be the same and I'd prefer to have a slightly more elegant solution. Is there another function similar to ginput that provides the x,y values of a point in the same way that the datacursor does?
Thanks!

回答(2 个)

Image Analyst
Image Analyst 2016-8-26
Sorry if I'm asking the obvious, but did you try round()?
[x,y] = ginput(1);
x = round(x, 1);
y = round(y, 1);
  5 个评论
Image Analyst
Image Analyst 2016-8-26
ginput() will give you those values. If you want to see the values "live" as you mouse around, call impixelinfo() after you display it.
imshow(......
hp = impixelinfo();
h.Position = [left, top, width, height]. % You supply the actual numbers.
Emma
Emma 2016-8-26
No, ginput() does not give me [X,Y] = [-0.15, -0.08], it gives me [X,Y] = [-0.148972, -0.08330952] or [X,Y] = [-0.15110238, -0.084001210] or whatever else depending on where exactly within the pixel I click. That is what I do not want.

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-8-27
You need to use UpdateFcn. See this demo. This is all in my test3.m file:
function test3
sampleImage=[...
2 3 5 7 1
3 3 7 9 4
0 1 9 7 8]
imshow(sampleImage, [], 'InitialMagnification', 1600);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.5 1 .5]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
uiwait(msgbox('Please click on the data cursor icon on the toolbar, then click in the image and look in the command window.'));
dcm_obj = datacursormode(gcf)
set(dcm_obj,'UpdateFcn',@MyUpdateFunction)
function pos = MyUpdateFunction(~, event_obj)
% ~ Currently not used (empty)
% event_obj Object containing event data structure
% output_txt Data cursor text (string or cell array of strings)
pos = get(event_obj,'Position');
fprintf('You clicked at X = %.4f, Y = %.4f\n', pos(1), pos(2));

Community Treasure Hunt

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

Start Hunting!

Translated by