我的GUI出了问题。在我的开场函数中,我将img_new
变量定义为我存储的图像。
我的GUI有两个轴,一个显示原始图像,另一个显示已过滤的图像。我在4个无线电按钮的面板中有4个滤波器。在每个代码的末尾,img_new
=通过单选按钮过滤器创建的图像。
以下是一些代码:
% --- Executes when selected object is changed in uipanel3.
function uipanel3_SelectionChangeFcn(hObject, eventdata, handles)
handles.count = handles.count + 1;
% Change filter orientation depending on which radiobutton is chosen
switch get(eventdata.NewValue,'Tag')
case 'hte'
h_te = zeros(handles.rows, handles.colums);
# code of the filter...
axes(handles.axes2);
imshow(h_te);
handles.img_new = h_te;
case 'hc'
h_c = zeros(handles.rows, handles.colums);
# code of the filter...
axes(handles.axes2);
imshow(h_c);
handles.img_new = h_c;
case 'vlr'
v_lr = zeros(handles.rows, handles.colums);
# code of the filter...
axes(handles.axes2);
imshow(v_lr);
handles.img_new = v_lr;
case 'vc'
v_c = zeros(handles.rows, handles.colums);
# code of the filter...
axes(handles.axes2);
imshow(v_c);
handles.img_new = v_c;
end
guidata(hObject, handles)
这是imwrite
函数:
% --------------------------------------------------------------------
function save_img_ClickedCallback(hObject, ~, handles)
% writing the new image
imwrite(handles.img_new, strcat('filtered_image_', num2str(handles.count), '.png'));
guidata(hObject, handles)
以下是将图片转换为axes1
原始图片并将其过滤为axes2
(已过滤)的功能
% --- Executes on button press in img2.
function img2_Callback(hObject, ~, handles)
% Read image 2
img = imread('./coimbra_estadio.jpg');
handles.img_d = im2double(img);
% image size
size_img = size(handles.img_d);
handles.colums = size_img(2);
handles.rows = size_img(1);
if rem(handles.rows,2) == 0
handles.row_0 = ((handles.rows/2)+1);
else
handles.row_0 = ((handles.rows/2)+0.5);
end
if rem(handles.colums,2) == 0
handles.colum_0 = ((handles.colums/2)+1);
else
handles.colum_0 = ((handles.colums/2)+0.5);
end
axes(handles.axes1);
imshow(img);
% Generate eventdata to call the radiobuttons function
eventdata_new.EventName = 'SelectionChanged';
eventdata_new.OldValue = get(handles.uipanel3,'SelectedObject');
eventdata_new.NewValue = get(handles.uipanel3,'SelectedObject');
uipanel3_SelectionChangeFcn(handles.uipanel3, eventdata_new, handles);
guidata(hObject, handles)
正如您所看到的,最后我调用了面板功能,因此当图像加载时,它会自动过滤并且axes2
图像会发生变化。
问题是,当我调用保存功能时,它会保存旧的img_new
。
如果我更改了radiobutton,则img_new
会刷新,但如果没有更改,则不会刷新。{p>它应该是在加载图像时自动调用面板的无线电按钮功能。
答案 0 :(得分:2)
问题是guidata(hObject,handles);
末尾的img2_Callback
将旧句柄对象保存为最终gui状态,uipanel3_SelectionChangeFcn
中的更新将丢失。在调用uipannel3_SelectionChangeFcn
之后,您需要通过handles = guidata(hObject,handles);
来手动更新句柄
或handles=guidata(hObject);
(忘了调用guidata更新句柄,请参阅帮助),或者只是删除img2_callback末尾的行guidata(hObject,handles);
(如果代码稍后会更改,则更安全,在uipannel3_SelectionChangeFcn
之后更新句柄是更安全的方法...