【问题标题】:How to add a button to a GUI如何将按钮添加到 GUI
【发布时间】:2014-05-13 12:39:29
【问题描述】:

我想在此代码中添加一个按钮,该按钮将关闭窗口并在我旋转时保存图片。

function rotationGUI(a)
    I = imread(a);

    %# c
    hFig = figure('menu','none');
    hAx = axes('Parent',hFig);

    uicontrol('Parent',hFig, 'Style','slider', 'Value',0, 'Min',0,...
        'Max',360, 'SliderStep',[1 10]./360, ...
        'Position',[150 5 300 20], 'Callback',@slider_callback) 
    hTxt = uicontrol('Style','text', 'Position',[290 28 20 15], 'String','0');
    %# show image
    imshow(I, 'Parent',hAx)
    %# Callback function
    function slider_callback(hObj, eventdata)
        angle = round(get(hObj,'Value'));        %# get rotation angle in degrees
        imshow(imrotate(I,angle), 'Parent',hAx)  %# rotate image
        set(hTxt, 'String',num2str(angle))       %# update text
    end
function ok_Callback(hObject, eventdata, handles)
 set(hTxt, 'String','save')
end

end

【问题讨论】:

    标签: matlab user-interface image-processing matlab-guide


    【解决方案1】:

    这里和那里很少有黑客攻击,就这样 -

    主要功能

    function rotationGUI(a)
    I = imread(a);
    
    %# c
    hFig = figure('menu','none');
    hAx = axes('Parent',hFig);
    
    hTxt = uicontrol('Style','text', 'Position',[290 28 20 15], 'String','0');
    uicontrol('Parent',hFig, 'Style','slider', 'Value',0, 'Min',0,...
        'Max',360, 'SliderStep',[1 10]./360, ...
        'Position',[150 5 300 20], 'Callback',{@slider_callback,I,hAx,hTxt,hFig})
    
    uicontrol(hFig,'Style','pushbutton','String','Save and Close',...
        'Position',[10 20 120 40],'Callback',{@ok_Callback,I,hTxt,hFig});
    
    %# show image
    imshow(I, 'Parent',hAx)
    %# Callback function
    
    return;
    

    关联函数-1

    function slider_callback(hObj, eventdata,I,hAx,hTxt,hFig)
    
    global Irot
    angle = round(get(hObj,'Value'));        %# get rotation angle in degrees
    Irot = imrotate(I,angle);
    imshow(Irot, 'Parent',hAx)  %# rotate image
    set(hTxt, 'String',num2str(angle))       %# update text
    
    return;
    

    关联函数-2

    function ok_Callback(hObj, eventdata,I,hTxt,hFig)
    
    global Irot
    
    set(hTxt, 'String','save')
    
    [filename, pathname] = uiputfile('*.jpg', 'Save Image as');
    imwrite(Irot,strcat(pathname,filename));
    
    delete(hFig);
    return;
    

    编辑 1:如果您想将旋转后的图像保存为原始图像,即覆盖它,请使用这些更改 -

    编辑函数rotationGUI -

    uicontrol(hFig,'Style','pushbutton','String','Save and Close',...
        'Position',[10 20 120 40],'Callback',{@ok_Callback,I,hTxt,hFig,a});
    

    编辑函数ok_callback -

    function ok_callback(hObj, eventdata,I,hTxt,hFig,path1)
    
    global Irot
    
    set(hTxt, 'String','save')
    imwrite(Irot,path1);
    
    delete(hFig);
    end
    

    【讨论】:

    • @user2347210 结束函数的一个好习惯是使用RETURN 而不是END
    • 如果我想将它保存在原始图片上,我需要更改什么?
    • @user2347210 查看编辑 1。
    • TNX!!你帮了我很多
    • 最后一个问题朋友...我尝试从函数中获取我选择旋转的最终角度的输出...你能帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2016-12-29
    • 2018-07-01
    • 1970-01-01
    相关资源
    最近更新 更多