I'm building a Matlab GUI which let the user use some interactive tools for image processing (a single mfile with no fig file),
like rotation of the image withimrotate
and angle parameter and clearing out pixels with bwareaopen
and area parameter.
At first, I was thinking of plotting the tools with my mfile with somehthing like
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开发者_StackOverflow中文版*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 );
but then I came across the design of toolbar as built-in class in FileExchange Fireworks and thought maybe I'm missing the right-and-neat way to design my toolbar with classes and built in uitoolbar
command.
Any advice on designing my toolbar from single mfile?
Using a custom toolbar would probably be a good solution for the tool you describe. Create the toolbar using UITOOLBAR and add push or toggle buttons using UIPUSHTOOL and UITOOGLETOOL, respectively. This can readily be done in the initialization stage of your GUI m-file.
I've given a simple example below. Some caveats:
- The logic of the toggle button vs. push button is not implemented correctly since inverting the image, flipping it and then inverting again will not give the correct result. However, I am just trying to show how to code toolbar buttons not how to process images.
- You will probably want to use more creative icons then what I've given in the CData property.
- I encourage a object-oriented approach to this solution despite my procedural example.
Here it is:
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
精彩评论