如何在imrect MATLAB GUI上不停止
我想从显示的图像中获取矩形坐标。我想前后移动图像。这是我的MATLAB GUI: 所以当我按下下一步时,它应该会显示系列中的下一个图像,类似的是后退按钮。我正在使用以下代码:如何在imrect MATLAB GUI上不停止,matlab,user-interface,callback,Matlab,User Interface,Callback,我想从显示的图像中获取矩形坐标。我想前后移动图像。这是我的MATLAB GUI: 所以当我按下下一步时,它应该会显示系列中的下一个图像,类似的是后退按钮。我正在使用以下代码: function next_Callback(hObject, eventdata, handles) % hObject handle to next (see GCBO) % eventdata reserved - to be defined in a future version of M
function next_Callback(hObject, eventdata, handles)
% hObject handle to next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.mypointer ~= length(handles.cur_images)
handles.mypointer = handles.mypointer + 1;
pic = imread(fullfile('images', handles.cur_images(handles.mypointer).name));
handles.imageName=handles.cur_images(handles.mypointer).name;
imshow(pic);
h = imrect;
getMyPos(getPosition(h));
addNewPositionCallback(h, @(p) getMyPos(p));
fcn = makeConstrainToRectFcn('imrect', get(gca, 'XLim'), get(gca, 'YLim'));
setPositionConstraintFcn(h, fcn);
handles.output = hObject;
handles.posi = getPosition(h);
guidata(hObject, handles);
但此代码的缺点是,当按下“下一步”按钮时,它会在h=imrect
上停止,因此等待用户绘制矩形。如果我不画一个长方形,它什么也做不了。即使我再次按下“后退”或“下一步”按钮,它也不会执行任何操作,因为它仍在等待用户绘制矩形
如何不让程序停止?一些注意事项:
- 我认为最好的做法是将
移动到单独的按钮,如前面提到的Excazaimrect
- 我不能重复您的问题-我创建了一个GUI示例,所有按钮在执行
后都有响应imrect
- 我还建议执行
h=imrect(handles.axes1)代码>而不仅仅是'h=imrect;`(我不知道这是否与你的问题有关)
imrect
。
示例创建“SingleShot”计时器,并在执行timestart
函数200毫秒后触发要执行的回调
function timerFcn_Callback(mTimer, ~)
handles = mTimer.UserData;
h = imrect(handles.axes1); % Call imrect(handles.axes1) instead just imrect.
% getMyPos(getPosition(h));
% ...
% Stop and delete the timer (this is not a good practice to delete the timer here - this is just an example).
stop(mTimer);
delete(mTimer);
function next_Callback(hObject, eventdata, handles)
% if handles.mypointer~=length(handles.cur_images)
% ...
% h = imrect(handles.axes1);
% Create new "Single Shot" timer (note: it is not a good practice to create new timer each time next_Callback is executed).
t = timer;
t.TimerFcn = @timerFcn_Callback; % Set timer callback function
t.ExecutionMode = 'singleShot'; % Set mode to "singleShot" - execute TimerFcn only once.
t.StartDelay = 0.2; % Wait 200 msec from start(t) to executing timerFcn_Callback.
t.UserData = handles; % Set UserData to handles - passing handles structure to the timer.
% Start timer (function timerFcn_Callback will be executed 200 msec after calling start(t)).
start(t);
disp('Continue execution...');
为
imrect
制作一个单独的按钮。。。