【问题标题】:Matlab WindowButtonMotion callback stops working when image is changed当图像更改时,Matlab WindowButtonMotion 回调停止工作
【发布时间】:2016-09-10 16:39:20
【问题描述】:

在我的 GUIDE 生成的 gui 中,我有一个轴对象,当 gui 初始化时,我在其中 imshow() 一个位图。我有一个 WindowButtonMotion 回调定义为:

function theGui_WindowButtonMotionFcn(hObject, eventdata, handles)
  % handles.mode_manager is initialized in the gui startup code
  if (isempty(handles.mode_manager.CurrentMode))
    obj_han=overobj('axes');
    if ~isempty(obj_han)
      set(handles.theGui, 'Pointer','cross');
    else
      set(handles.theGui, 'Pointer','arrow');
    end
  end
end

我在工具栏上的打开文件按钮上有一个回调:

function openFile_ClickedCallback(hObject, eventdata, handles)
  % handles.image_handle received the handle from the imshow that 
  % opened the initial image
  tmp_handle = handles.image_handle;
  [name, path] = uigetfile({'*.bmp'; '*.jpg'});
  if (path == 0)
    return
  else
    filename = strcat(path, name);
  end

  % read the image into the axes panel
  hold on
  handles.image_handle = imshow(filename);
  set(handles.image_handle, 'ButtonDownFcn', @imageMouseDown);
  handles.mode_manager = uigetmodemanager();
  delete(tmp_handle);
  guidata(hObject, handles);
end

新图像显示在 gui 的坐标区对象中后,指针不再变为坐标区对象上的十字。问题与新显示的图像有关,因为如果我注释掉实际显示新图像的代码部分,则在调用 openFile 回调后指针显示为十字。

【问题讨论】:

    标签: matlab matlab-guide matlab-gui


    【解决方案1】:

    回调停止工作,因为使用 imshow 替换了坐标区对象。

    下面的代码演示了这个问题:

    imshow(zeros(100));
    h = gca;
    h.UserData = 123;  %Set UserData property value to 123
    imshow(ones(100)); %Use imshow again.
    h2 = gca;
    

    现在:

    h2.UserData
    
    ans =
    
         []
    
    h.UserData
    Invalid or deleted object.
    

    如您所见,使用imshow 再次将坐标区对象替换为新的坐标区对象。


    下面的例子,只修改图片数据,不修改坐标轴:

    image_handle = imshow(zeros(100));
    h = gca;
    h.UserData = 123;  %Set UserData property value to 123
    %imshow(ones(100), 'Parent', h); %Use imshow again.
    image_handle.CData = ones(100); %Modify only image data, without modifying the axes. 
    h2 = gca;
    

    现在:

    h2.UserData
    
    ans =
    
       123
    

    将您的handles.image_handle = imshow(filename); 代码修改为:

    I = imread(filename);
    handles.image_handle.CData = I;
    

    【讨论】:

    • 谢谢,这行得通。但我还是不明白为什么。 WindowButtonMotion 回调正在查看指针是否在 any 轴对象上,而不是特定轴对象上。因此,即使对象被替换,指针仍然在轴对象上。后续行动 - 我如何将原始图像读入在 GUIDE 中创建的轴对象?由于当前编写代码,原始坐标区对象正在被原始 imshow 替换。 (尽管如此,WindowButtonMotion 回调在开始时起作用。)
    • 我真的不明白为什么 WindowButtonMotion 在更换轴时停止工作...将原始图像读入轴对象:我将其留在评论中:imshow(ones(100), 'Parent', h);(h 是轴,并且one(100) 是图像矩阵)。
    • 我在打开原始图像时尝试了imshow(filename, 'Parent', axes_handle),其中axes_handle 是GUIDE 生成的句柄。轴对象仍被替换。但这更多的是一个困惑而不是一个问题。谢谢
    猜你喜欢
    • 2022-09-28
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多