在MATLAB GUIDE中在轴上覆盖当前图像

时间:2012-12-29 18:39:32

标签: matlab matlab-guide axes

我的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>它应该是在加载图像时自动调用面板的无线电按钮功能。

1 个答案:

答案 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之后更新句柄是更安全的方法...