【问题标题】:How to not stop on imrect MATLAB GUI如何在不正确的 MATLAB GUI 上不停止
【发布时间】:2016-12-10 22:25:12
【问题描述】:

我想从正在显示的图像中获取矩形坐标。我想移动图像下一个和后退。这是我的 MATLAB GUI:

所以当我按下 Next 时,它应该会显示系列中的下一张图像,Back 按钮也是如此。我正在使用此代码:

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);

但是这段代码的缺点是当 Next 按钮被按下时它会停在h=imrect,所以等待用户绘制一个矩形。如果我不画一个矩形,它什么也做不了。即使我再次按下 BackNext 按钮,它也不会执行任何操作,因为它仍在等待用户绘制矩形。

如何不让程序停在imrect

【问题讨论】:

  • imrect制作一个单独的按钮...

标签: matlab user-interface callback


【解决方案1】:

一些注意事项:

  • 我认为最好的做法是将imrect 移动到一个单独的按钮上,就像提到的 Excaza 一样。
  • 我无法重复您的问题 - 我创建了一个 GUI 示例,所有按钮在 imrect 执行后都会响应。
  • 我还建议执行h = imrect(handles.axes1); 而不是只执行 'h = imrect;`(我不知道这是否与您的问题有关)。

当我遇到 MATLAB“阻塞函数”问题时,我通过在计时器对象的回调函数中执行该函数来解决它。 我不知道这是否是一个好习惯;这只是我解决它的方式...... 在定时器回调中执行函数允许继续执行主程序流。

下面的代码示例展示了如何创建一个定时器对象,并在定时器的回调函数中执行imrect。 该示例创建“SingleShot”计时器,并在执行时间start 函数后触发回调执行 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...');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多