Matlab gui:如果多次单击,包含imrect的回调会卡住

时间:2014-08-27 15:19:12

标签: matlab user-interface

我在Matlab中编写GUI,用户可以在其中处理图像。打开图像后,用户可以通过“裁剪”按钮指定ROI(下面的回调代码)。裁剪后,可以进行进一步的分析。

除非用户在没有选择矩形的情况下意外点击“裁剪按钮”,否则一切正常。然后,再次单击按钮,我可以绘制矩形,但不再确认我的选择。我认为“之前的”回调仍然停留在wait(h)函数上,该函数应该允许用户重新定义矩形(根据http://www.mathworks.ch/ch/help/images/ref/imrect.html

我还试图包含一个重启按钮并重新打开GUI,但是仍然无法在图像中选择ROI。

% --- Executes on button press in cropButton.
function cropButton_Callback(hObject, eventdata, handles)

% Read image
I = handles.I;
hold(handles.axes1,'on');

% Let user choose rectangle to crop
h = imrect(handles.axes1);
position = round(wait(h));
% Crop image
I = imcrop(I,position);

hold(handles.axes1,'off');

% Show cropped image
imshow(I, 'Parent', handles.axes1);

handles.I = I;
guidata(hObject, handles);

% --- Executes on button press in resetButton.
function resetButton_Callback(hObject, eventdata, handles)
clear all;
close all;
clc;
MyGUI; % restart GUI

我该如何解决这个问题?任何想法都将不胜感激。

2 个答案:

答案 0 :(得分:0)

而不是"重置"按钮,你可以使用findobj来寻找一个矩形(即一个hggroup对象)。如果没有找到矩形(即你没有选择矩形就按下了裁剪按钮),请拨打' return'并再做一次。

例如:

hFindROi = findobj(gca,'Type','hggroup');

if isempty(hFindRoi) % i.e. no rectangle found

msgbox('Please select a rectangle before pressing the Crop button');
return
end

另一种选择是使用某种标志来知道是否已执行某些回调。您可以将这些标志存储在GUI的句柄结构中,以便可以从任何回调中访问它们。

例如,让我们说你宣布

handles.SelectRectangleFlag = true;
guidata(hObject,handles);
选择矩形后

。然后当你按下裁剪按钮时,你可以检查标志的值,如果它是假的(不要忘记在开始时初始化它),那么返回并且不做任何事情

EG。在cropButton_Callback

if handles.SelectRectangleFlag == false
msgbox('Please select a rectangle before pressing the Crop button');
    return
end

希望有所帮助!我希望它足够清楚。如果没有,请问:)

答案 1 :(得分:0)

您可以在点击时禁用该按钮,并在回调结束时重新启用它:

% --- Executes on button press in cropButton.
function cropButton_Callback(hObject, eventdata, handles)
set(handles.cropButton,'enable','off');

% Read image
I = handles.I;
hold(handles.axes1,'on');

% Let user choose rectangle to crop
h = imrect(handles.axes1);
position = round(wait(h));
% Crop image
I = imcrop(I,position);

hold(handles.axes1,'off');

% Show cropped image
imshow(I, 'Parent', handles.axes1);

handles.I = I;
guidata(hObject, handles);
set(handles.cropButton,'enable','on');

不那么复杂。