[英]How to get the mouse click position on Matlab figure axes?
我想點擊一個Matlab圖,找出點擊位置的x和y位置。
在圖表中,我認為有一種方法可以點擊線上的點並獲得它的x和y坐標。 如果沒有繪制圖表,我該怎么做?
以下是如何最優雅地完成它:
function test
% create test figure
f = figure(1);
% set function to call on mouse click
set(f, 'WindowButtonDownFcn', @clicker);
end
% function called on mouse click in the figure
function clicker(h,~)
get(h, 'selectiontype')
% 'normal' for left moue button
% 'alt' for right mouse button
% 'extend' for middle mouse button
% 'open' on double click
get(h, 'currentpoint')
% Current mouse location, in pixels from the lower left.
% When the units of the figure are 'normalized', the
% coordinates will be [0 0] inb lower left, and [1 1] in
% the upper right.
end
也許這也會奏效:
function [loc] = get_image_point (I)
figure('name','Doubleclick to set location');imshow(I);
[c r] = getpts(1);
loc = int32([c r]);
if size(loc,1)>1
loc = [loc(1,1) loc(1,2)];
end
close all;
end
斯蒂芬
注意: - getpts()
- 是一個“圖像處理工具箱”功能。 - ginput()
- 等待你的鼠標點擊,停止執行直到你點擊,只有在被叫時才有效。
而只要你的程序正在運行get(h, 'currentpoint')
將隨時工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.