1

画像内の関心領域 (ROI) を選択するための GUI をプログラミングしています。impoly/などの組み込みの MATLAB 関数を使用して、いくつかの種類の領域を選択できますimellipse

以下に、私の問題に対処できる GUI の最小限の作業例を示します。

問題は、あるユーザーが ROI 選択ボタンの 1 つを誤ってクリックした場合 (つまり、多角形を選択することが目標であったときに楕円を選択することを選択した場合)、ワークスペースでエラーを回避して ROI 選択の対話型ツールをキャンセルするにはどうすればよいかということです。

「Esc」キーを押すとインタラクティブ ツールがキャンセルされることはわかっていますが、エラーを回避したいと考えています。

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]);


    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
4

1 に答える 1

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 をキャンセルするエスケープ キーを発行します。

于 2013-03-18T00:30:30.513 に答える