更改图像时,Matlab WindowButtonMotion 回调停止工作
Matlab WindowButtonMotion callback stops working when image is changed
在我的 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 回调后指针显示为十字。
回调停止工作,因为使用 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;
在我的 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 回调后指针显示为十字。
回调停止工作,因为使用 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;