Matlab gui与mfile中的工具栏

时间:2011-05-16 03:50:17

标签: matlab toolbar

我正在构建一个Matlab GUI,让用户使用一些交互式工具进行图像处理(一个没有图形文件的mfile), ,例如使用imrotate和角度参数旋转图像,并使用bwareaopen和区域参数清除像素。

起初,我正在考虑使用我的mfile绘制工具,例如

function myGUI( grayI )

h_fig = figure;
h_ax = imshow( grayI );
title('Drag line and press button to rotate image');

ImgSize = size(grayI);
h_lev = imline(gca, [ 0.2*ImgSize(2), 0.1*ImgSize(1); ...
0.8*ImgSize(2) 0.1*ImgSize(1) ] );
h_lev.addNewPositionCallback( @LineUpdateFcn );

% text for the angle
h_txt = uicontrol('Style','text','String','Angle = []', 'unit', 'norm', ...
'pos',[0 0.9 .1 .05]);

%add rotate button
h_btn = uicontrol('unit','norm','pos',[0 0.95 .1 .05]);
set(h_btn,'string','Rotate','callback',@RotateImageWithLever);

% save the elements data in figure
setappdata(h_fig,'h_lev',h_lev);
setappdata(h_fig,'h_ax',h_ax);
setappdata(h_fig,'h_txt',h_txt);

% wait for user to close figure
waitfor( h_fig );

然后我在FileExchange Fireworks中遇到了工具栏的设计作为内置类,并且想到也许我错过了使用类设计我的工具栏并且内置{{1}的正确方法命令。

有关从单个mfile设计工具栏的任何建议吗?

1 个答案:

答案 0 :(得分:1)

使用自定义工具栏可能是您描述的工具的一个很好的解决方案。使用UITOOLBAR创建工具栏,然后分别使用UIPUSHTOOLUITOOGLETOOL添加推送或切换按钮。这可以在GUI m文件的初始化阶段轻松完成。

我在下面给出了一个简单的例子。一些警告:

  1. 切换按钮与按钮的逻辑未正确实现,因为反转图像,翻转然后再次反转将无法提供正确的结果。但是,我只是想说明如何编码工具栏按钮而不是如何处理图像。
  2. 您可能希望使用更多创意图标,然后使用我在CData属性中提供的图标。
  3. 尽管我有程序性的例子,但我鼓励采用面向对象的方法解决这个问题。
  4. 这是:

    function myGUI(grayI)
    
    persistent grayICopy;
    
    %# Keep a persistent copy of the image data to be used in the toolbar tool
    %# callbacks. Other possibilities here are to not store this data
    %# persistently and instead read it from the plotted values or restructure
    %# this whole code as a class and store the raw image data in a class
    %# property.
    grayICopy = grayI;
    
    %# Create the figure window and show the image.
    hFigure = figure;
    hAxes = axes('Parent', hFigure);
    image(grayI, 'Parent', hAxes); %# I don't have the Image processing Toolbox
    
    %# Create toolbar
    hToolbar = uitoolbar('Parent', hFigure);
    
    %# Add a toolbar button for 90deg clockwise rotation
    uipushtool('Parent', hToolbar, ...
        'ClickedCallback', @flipVertical, ...
        'CData', ...
        repmat([0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
        0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0; ...
        0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], [1 1 3]));
    
    %# Add a toolbar toggle button for inverting image
    uitoggletool('Parent', hToolbar, ...
        'OnCallback', @toggleInverseOn, ...
        'OffCallback', @toggleInverseOff, ...
        'CData', ...
        repmat([0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; ...
        0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
        0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
        0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
        0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], [1 1 3]));
    
        function flipVertical(src, eventdata)
            grayICopy = flipdim(grayICopy, 1);
            image(grayICopy, 'Parent', hAxes);
        end
    
        function toggleInverseOn(src, eventdata)
            image(1-grayICopy, 'Parent', hAxes);
        end
    
        function toggleInverseOff(src, eventdata)
            image(grayICopy, 'Parent', hAxes);
        end
    end