2016-06-12 87 views
0

我正在開發一個程序,它將顯示一個場景的圖像,用戶將點擊圖像上的任意點,程序將消除所有的像素圖像具有用戶選擇的像素的顏色。在MATLAB中通過鼠標單擊從圖像中獲取像素位置

我已經完成了我需要的所有工作,但我只有一個問題:當我使用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

您不能將ginput限制爲axes對象。可能更好的方法是使用圖像的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'); 
相關問題