2016-06-12 85 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'); 
相关问题