2017-04-07 30 views
1

我附加了一个示例GUI代码,该代码具有两个轴和两个图像,当我使用ginput选择种子点时,我可以在任一轴上进行选择,Is反正到ginput限制到特定的轴如何将ginput限制为当前坐标轴以选择种子点

% --- Executes on button press in open. 
function open_Callback(hObject, eventdata, handles) 
% hObject handle to open (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 
global img1; 
global img2; 

img1 = imread('peppers.png'); 
img2 = imread('rice.png'); 

axes(handles.axes1); 
imshow(img1,[]); 
axes(handles.axes2); 
imshow(img2,[]); 


% --- Executes on button press in seedpointSelect. 
function seedpointSelect_Callback(hObject, eventdata, handles) 
% hObject handle to seedpointSelect (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 
global img1; 
global img2; 
global x; 
global y; 

axes(handles.axes1); 
imshow(img1,[]); 
[y,x] = ginput(handles.axes1); 
y = round(y); x = round(x); 
set(handles.xcord,'String',num2str(x)); 
set(handles.ycord,'String',num2str(y)); 

上限制ginput到一个特定的轴任何帮助,

感谢, 戈皮

回答

0

不要使用ginput,创建一个鼠标点击回调相反(ButtonDownFcn)。您可以将回调设置为,例如,从轴中删除回调函数。在你的主程序中,设置回调,然后你要waitfor那个属性改变。只要用户点击,您就可以控制回去,然后您可以读取最后一次鼠标点击的位置(CurrentPoint)。请注意,您读出的位置在轴坐标中,而不是屏幕像素。这是一件好事,它很可能对应于所显示图像中的一个像素。

0

在旧版本的MATLAB的你曾经是能够改变axesHitTest属性忽略来自ginput

set(handles.axes2, 'Hittest', 'off') 

更好的方法,虽然是使用ButtonDownFcn因为你有更多的控制权点击带有axes对象的鼠标事件。

从你OpeningFcn

set(handles.axes1, 'ButtonDownFcn', @mouseCallback) 

内然后,你需要创建一个回调函数

function mouseCallback(src, evnt) 
    handles = guidata(src); 

    % Get the current point 
    xyz = get(src, 'CurrentPoint'); 

    x = xyz(1,1); 
    y = xyz(1,2); 

    % Store x/y here or whatever you need to do 
end 
相关问题