【问题标题】:Programmatically cancel ROI selection以编程方式取消 ROI 选择
【发布时间】:2019-11-12 10:25:34
【问题描述】:

在 GUI 中,我想提供几个按钮来在图像中绘制不同形状的 ROI。用户选择“多边形”按钮,当他将鼠标悬停在轴上时,他可以绘制多边形-ROI。但是,如果用户决定从多边形更改为圆形(或其他)没有绘制 ROI,则必须取消最后的 ROI 绘制过程。我想,这归结为一个问题,如何以编程方式取消由例如drawpolygon 初始化的 ROI 绘制过程。

在下面的代码中,我希望drawpolygon-进程在用户绘制 ROI 之前单击取消按钮时停止。有任何想法吗? (请注意,drawpolygon 是图像处理工具箱的一部分,是在 Matlab R2018b 中引入的,因此两者都是必需的)。

function roi2mask()
    [ax, cancel] = local_roi2mask_gui();
    cancel.Callback = @(~,~) display('Cancel was clicked, but how to finish the ROI selection?');
    roi = drawpolygon(ax);
    cancel.Callback = @(~,~) display('ROI was already selected...');
end

function [ax, cancel] = local_roi2mask_gui()
    f = figure('menubar','none','toolbar','none');
    f.Position(3:4) = [300 400];
    p1 = uipanel(f, 'Units', 'normalized', 'Position', [0, 0, 1, 2/3], 'Title', 'Axis');

    ax = axes(p1);
    imagesc(ax, peaks(64));

    cancel = uicontrol(f, 'String','Cancel', 'Position', [10, 350, 100, 20]);

end

【问题讨论】:

    标签: matlab


    【解决方案1】:

    您可以找到 ROI 对象,将其删除,然后调用uiresume

    这是一个可执行代码示例:

    h_fig = figure; %Create a figure, an keep the handle to the figure.
    ax = axes(h_fig); %Create an axes in the figure, an keep the handle to the axes.
    
    %Add a button, add set the callback to pb_call.
    Button = uicontrol('Parent',h_fig,'Style','pushbutton','String',...
        'Cancel','Units','normalized','Position',[0.8025 0.82 0.1 0.1],'Visible','on',...
        'Tag', 'CancelPushbutton', 'Callback', @pb_call);
    
    roi = drawpolygon(ax, 'Tag', 'MyDrawPolygon'); %Set the tag to 'MyDrawPolygon', so object can be found in pb_call.
    if isvalid(roi)
        roi.Tag = ''; %Reset the tag (if not canceled).
    end
    
    
    function pb_call(src, event)
        %Callback function (executed when button is pressed).
        h_fig = src.Parent; %Get handle to the figure (the figure is the parent of the button).
        h_roi = findobj(h_fig, 'Tag', 'MyDrawPolygon'); %Find the Polygon currntly drawn.
        if ~isempty(h_roi)
            h_roi.delete(); %Delete the ROI object.
            uiresume() %Resume execution of blocked program
        end
    end
    

    【讨论】:

    • 谢谢!我记得一个讨论 Tag 属性今天是否仍然有用。就是这样!
    猜你喜欢
    • 1970-01-01
    • 2014-04-05
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    相关资源
    最近更新 更多