首页 > 解决方案 > 以编程方式取消 ROI 选择

问题描述

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

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

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

解决方案


您可以找到 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

推荐阅读