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



在我的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

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

回调停止工作,因为使用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;

相关内容

  • 没有找到相关文章

最新更新