2017-04-07 28 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 
相關問題