【问题标题】:imwrite current image on axes in MATLAB GUIDE在 MATLAB GUIDE 中将当前图像写入轴上
【发布时间】:2012-12-14 14:52:40
【问题描述】:

我的 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 图像会发生变化。

问题是当我调用 save 函数时,它会保存旧的 img_new

如果我更改单选按钮,img_new 会刷新,但如果未更改,则不会刷新。应该就像加载图片自动调用单选按钮面板一样。

【问题讨论】:

    标签: matlab matlab-guide axes


    【解决方案1】:

    问题在于img2_Callback 末尾的guidata(hObject,handles); 将旧的句柄对象保存为最终的gui 状态,在uipanel3_SelectionChangeFcn 中所做的更新会丢失。您需要在调用uipannel3_SelectionChangeFcn 后手动更新句柄,方法是输入handles = guidata(hObject,handles);handles=guidata(hObject); (忘记了对 guidata 更新句柄的调用,请参阅帮助),或者只是删除 img2_callback 末尾的 guidata(hObject,handles); 行(如果代码稍后要更改,则安全性较低,在 uipannel3_SelectionChangeFcn 之后更新句柄是更安全的方法。 ..

    【讨论】:

    • 我把它放在你说的后面,但我不工作。旧的仍然保存。
    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 2014-08-20
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2015-11-03
    相关资源
    最近更新 更多