0

在我的 GUIDE 生成的 gui 中,我有一个轴对象,imshow()当 gui 初始化时,我将位图放入其中。我有一个 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 回调后指针显示为十字。

4

1 回答 1

1

回调停止工作,因为 usingimshow替换了轴对象。

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

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;
于 2016-09-10T08:39:20.830 回答