更改图像时,Matlab WindowButtonMotion回调停止工作

时间:2016-09-10 05:11:48

标签: matlab matlab-guide matlab-gui

在我的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的axis对象中显示新图像后,指针不再变为轴对象的交叉。该问题与新显示的图像有关,因为如果我注释掉实际显示新图像的代码部分,则在调用openFile回调后指针显示为十字形。

1 个答案:

答案 0 :(得分: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;