2013-03-17 65 views
1

我正在編程一個GUI,用於在圖像中選擇感興趣區域(ROI)。 幾種區域可以使用內置MATLAB函數來選擇,如impoly/imellipse中斷/禁用GUI中的ROI繪圖功能

下面,我提供了一個GUI的最小工作示例,可以解決我的問題。

問題是:假設一個用戶誤選其中一個ROI選擇按鈕(即,當目標是選擇一個多邊形時選擇一個橢圓)如何取消用於ROI選擇的交互工具避免工作區中的錯誤?

我知道按下「Esc」鍵會取消交互式工具,但我想避免錯誤。

一個想法是有另一個按鈕(中止)來執行中斷,但我一直無法拿出代碼來執行此操作。

function roiData = testGUI(sourceImage) 

% Initialize main figure 
hdl.mainfig = figure(); 

% Initialize roiData and roiCounter 
roiData = []; 
roiCounter = 0; 

% Select Elliptical ROI button 
hdl.selectEllipseButton = uicontrol(hdl.mainfig, 'Units', 'normalized', 'Position',[0.05 0.7 0.2 0.1], 'String', 'Select Ellipse', 'Callback', @selectEllipse); 
% Select Polygonal ROI button 
hdl.selectPolygonButton = uicontrol(hdl.mainfig, 'Units', 'normalized', 'Position',[0.05 0.6 0.2 0.1], 'String', 'Select Polygon', 'Callback', @selectPolygon); 
% Abort Button 
hdl.abort = uicontrol(hdl.mainfig, 'Units', 'normalized', 'Position',[0.05 0.2 0.2 0.2], 'String', 'Abort', 'Callback', @abort); 
% Axes 
hdl.axes = axes('Parent',hdl.mainfig, 'Units', 'Normalized', 'Position', [0.35 0.2 0.6 0.6]); 


    function selectEllipse(~, ~, ~) 
     imshow(sourceImage, [], 'Parent', hdl.axes); 
     roiCounter = roiCounter + 1; 
     h = imellipse(hdl.axes); 
     mask = uint16(createMask(h)); 
     wait(h);    
     roiData(roiCounter).mask = mask; 
    end 

    function selectPolygon(~, ~, ~) 
     imshow(sourceImage, [], 'Parent', hdl.axes); 
     roiCounter = roiCounter + 1; 
     h = impoly(hdl.axes); 
     mask = uint16(createMask(h)); 
     wait(h); 
     roiData(roiCounter).mask = mask; 
    end 

    function abort(~, ~, ~) 
     cla(hdl.axes) 
     % I need something else here... (see above) 
    end 

% Pause until figure is closed 
waitfor(hdl.mainfig); 

end 
+0

這是一個很好的問題,當我想要做類似的事情時,我無法想象這個問題。我會研究這個。當時我的工作是基本上強制用戶繪製他們選擇的任何東西,但是在完成後他們可以通過覆蓋「UIContextMenu」來刪除imroi。 – Justin 2013-03-17 23:33:13

回答

1

嘗試了這一點:

function roiData = testGUI(sourceImage) 
% Initialize main figure 
hdl.mainfig = figure(); 

% Initialize roiData and roiCounter 
roiData = []; 
roiCounter = 0; 

% Select Elliptical ROI button 
hdl.selectEllipseButton = uicontrol(hdl.mainfig, 'Units', 'normalized', 'Position',[0.05 0.7 0.2 0.1], 'String', 'Select Ellipse', 'Callback', @selectEllipse); 
% Select Polygonal ROI button 
hdl.selectPolygonButton = uicontrol(hdl.mainfig, 'Units', 'normalized', 'Position',[0.05 0.6 0.2 0.1], 'String', 'Select Polygon', 'Callback', @selectPolygon); 
% Abort Button 
hdl.abort = uicontrol(hdl.mainfig, 'Units', 'normalized', 'Position',[0.05 0.2 0.2 0.2], 'String', 'Abort', 'Callback', @abort); 
% Axes 
hdl.axes = axes('Parent',hdl.mainfig, 'Units', 'Normalized', 'Position', [0.35 0.2 0.6 0.6]); 

% Callbackrunning parameter 
callbackrunning = false; 

    function selectEllipse(hObject,eventdata) 
     if(callbackrunning) 
      return; 
     end 

     callbackrunning = true; 

     imshow(sourceImage, [], 'Parent', hdl.axes); 
     roiCounter = roiCounter + 1; 
     h = imellipse(hdl.axes); 
     if(~callbackrunning) 
      return; 
     end 
     mask = uint16(createMask(h)); 
     roiData(roiCounter).mask = mask; 

     callbackrunning = false; 
    end 

    function selectPolygon(hObject,eventdata) 
     if(callbackrunning) 
      return; 
     end 

     callbackrunning = true; 

     imshow(sourceImage, [], 'Parent', hdl.axes); 
     roiCounter = roiCounter + 1; 
     h = impoly(hdl.axes); 
     if(~callbackrunning) 
      return; 
     end 
     mask = uint16(createMask(h)); 
     roiData(roiCounter).mask = mask; 

     callbackrunning = false; 
    end 

    function abort(hObject,eventdata) 
     if(callbackrunning) 
      disp('Fire escape key to cancel previous imrect'); 
      % Create robot that fires an escape key 
      robot = java.awt.Robot; 
      robot.keyPress (java.awt.event.KeyEvent.VK_ESCAPE); 
      robot.keyRelease (java.awt.event.KeyEvent.VK_ESCAPE); 
      callbackrunning = false; 
     end 
    end 

% Pause until figure is closed 
waitfor(hdl.mainfig); 
end 

的機器人對象引發這將取消imrect escape鍵。

+0

謝謝你,它的作品:)。非常感謝我向java機器人班介紹我! – fnery 2013-03-18 13:52:14

+0

@fnery Dude我剛剛發現它,它看起來真的很酷。這裏有更多關於它的地方:http://undocumentedmatlab.com/blog/gui-automation-robot/。另外,你可能需要確保你沒有java機器人的內存泄漏。它可能需要在腳本的末尾使用'clear java'。 – Justin 2013-03-18 18:56:17