Alternative to ginput for selecting data from an image?
7 次查看(过去 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!
0 个评论
回答(2 个)
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
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.
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));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computer Vision with Simulink 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!