【问题标题】:Interrupting/Disabling ROI drawing functions in a GUI在 GUI 中中断/禁用 ROI 绘制功能
【发布时间】:2014-07-14 11:43:49
【问题描述】:

我正在编写一个用于选择图像中感兴趣区域 (ROI) 的 GUI。 可以使用内置的 MATLAB 函数(例如 impoly/imellipse)选择几种区域。

下面,我提供了一个可以解决我的问题的 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

【问题讨论】:

  • 这是一个很好的问题,当我想做类似的事情时我无法弄清楚。我会调查一下。我当时的工作基本上是强制用户绘制他们选择的任何内容,但完成后他们可以通过覆盖 'UIContextMenu' 来删除 imroi。

标签: matlab user-interface selection roi


【解决方案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

机器人对象触发取消即时的退出键。

【讨论】:

  • 谢谢伙计,它有效:)。非常感谢您向我介绍 java 机器人类!
  • @fnery Dude 我刚刚发现了它,它看起来真的很酷。这里有更多关于它的信息:undocumentedmatlab.com/blog/gui-automation-robot。此外,您可能需要确保 java 机器人没有内存泄漏。您可能需要在脚本末尾使用clear java
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
相关资源
最近更新 更多