2016-06-12 11 views
0

MATLABでシーンの画像を表示するプログラムを開発しています。ユーザーは画像上の任意の点をクリックし、プログラムはユーザーが選択したピクセルの色を持つ画像。MATLABでマウスをクリックして画像からピクセル位置を取得します

私が必要とするすべてのことをやったことがありますが、私はただ1つの問題しか持っていません:ginput機能を使用して、ユーザーが好きなポイントをクリックすると、画像の外側を含むので、彼がクリックした正しい座標を得ることが難しくなります。これは私が現時点で持っているものです:

figure; 
imshow(img) 
[x,y] = ginput(1); 
close all 
v = img(int8(x),int8(y)); 

% Put all the pixels with the same value as 'v' to black 
... 
% 

クリック可能な領域を画像の領域だけに制限する方法はありますか?

答えて

1

ginputaxesオブジェクトに限定することはできません。おそらくこれを達成するより良い方法は、画像のButtonDownFcnを使用することです。

hfig = figure; 
him = imshow(img); 

% Pause the program until the user selects a point (i.e. the UserData is updated) 
waitfor(hfig, 'UserData') 

function clickImage(src) 
    % Get the coordinates of the clicked point 
    hax = ancestor(src, 'axes'); 
    point = get(hax, 'CurrentPoint'); 
    point = round(point(1,1:2)); 

    % Make it so we can't click on the image multiple times 
    set(src, 'ButtonDownFcn', '') 

    % Store the point in the UserData which will cause the outer function to resume 
    set(hfig, 'UserData', point); 
end 

% Retrieve the point from the UserData property 
xy = get(hfig, 'UserData'); 
関連する問題