Matlabの図をクリックして、クリックした位置のxとyの位置を確認したいと思います。
グラフでは、線上の点をクリックして、そのx座標とy座標を取得する方法があると思います。グラフがプロットされていない場合、どうすれば同じことができますか?
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
Figure を作成したら、試してみてください
[x_coord, y_coord]=ginput(1);
そのため、Figure を 1 回クリックするだけで (引数が 1 である理由です)、関数ginput()によって返される座標を取得できます。
多分これもうまくいくでしょう:
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()
- は「Image Processing Toolbox」機能です。- ginput()
- マウスのクリックを待ち、クリックするまで実行を停止し、呼び出されたときにのみ機能します。
一方get(h, 'currentpoint')
、プログラムが実行されている限り、いつでも機能します。