2011-05-16 110 views
2

我建立一個Matlab GUI,該GUI讓用戶使用一些交互式工具用於圖像處理(不具有圖文件中的單個M文件)的工具欄,
imrotate的圖像的旋轉和角參數和用bwareaopen和區域參數清除像素。matlab的GUI與M文件

起初,我想用我的M文件與somehthing繪製工具,如

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和想,也許我錯過以正確和簡潔的方式設計我的工具欄的類和內置uitoolbar命令。

從單個mfile設計我的工具欄的任何建議?

回答

1

使用自定義工具欄可能是您描述的工具的一個很好的解決方案。使用UITOOLBAR創建工具欄,並分別使用UIPUSHTOOLUITOOGLETOOL添加按鈕或切換按鈕。這可以在GUI m文件的初始化階段輕鬆完成。

我給出了一個簡單的例子。一些注意事項:

  1. 切換按鈕與按鍵的邏輯不自地反轉圖像,翻轉,然後倒正確實施再不會給出正確的結果。但是,我只是試圖展示如何編寫工具欄按鈕而不是如何處理圖像。
  2. 你可能會想要使用更多的創意圖標,然後我在CData屬性中給出了。
  3. 儘管有我的程序示例,但我仍鼓勵採用面向對象的方法來解決此問題。

這就是:

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