An example of a MATLAB graphical user interface (2)

Source: Internet
Author: User

image binary processing GUI:
Then an example of a Matlab GUI (1) Let's do an image binary image processing. Add an image processing menu using the previous method, as follows:
Below it is a menu item that adds "image binary", as follows:
Then, click "OK" to close the menu editor and save the entire interface. If there is no corresponding callback in our. m file, we can click on the "View" button to generate a callback function.
Image binary, there is a threshold setting, then we can create a new interface, in this interface put a slider to set the image of the threshold value of the binary. At the same time, there is a text that shows the value of the current slider bar.
Then we create a new blank interface, draw a static text and slider control on it, and then use the toolbar's alignment tool (Align Objects) to align the two controls. As follows:
Then, save the interface as Im2bw_args.fig. The entire design is as follows:
You can set the FontSize property of static text to 10 so that the font size is larger. Set the static text's Tag property to Txt_display, and set the scroll bar's Tag property to Slider_val.
In order to be able to scroll in the scroll bar, Static text displays the value of the scroll bar, you need to write the following program in the scroll bar callback, you can click on the scroll bar right button, select "Viewcallbacks" under
"Callback" directly into the scroll bar's Callback function (Slider_val_callback).
Val=get (hobject, ' Value '); set (Handles.txt_display, ' String ', Num2str (Val));
Save, run the program, you can slide the scroll bar, and the static text will display the corresponding value. Double-click on the figure to open the Properties window of the figure (the underlying window with the block) and set its Tag property
Set to "FIGURE_IM2BW" and set its Name property to "set image binary Threshold". Then, save the interface. The runtime is as follows:
So what we're thinking is that when the slider slides, the two-valued image is displayed on the AXES_DST axis in the pjimage.fig. So what do we do? The first thing to do is,
When you click "Image binary" under "image processing" in the Pjimage.fig menu, the Im2bw_args.fig will open. This is the time for us to call IM2BW_ARGS.M.
When we call it, we return a handle that points to the open im2bw_args.fig. For more details, you can refer to the first note in the IM2BW_ARGS.M file, which has the following written:
% H = Im2bw_args Returns the handle to a new Im2bw_args orthe handle to% the existing singleton*.
That means we can open the im2bw_args.fig as above. So in the "image binary" Callback function (m_image_2bw_callback), write the following program:
H=im2bw_args;
Then, save the pjimage.fig. Also, it is best to save Im2bw_args.fig and Pjimage.fig under a directory. Then, running Pjimage.fig, you can see that when you click on "Image binary"
The Im2bw_args.fig is turned on, and the value of the response is displayed when the slide bar slides. Here's how to show the sliding two-valued image to the AXES_DST axis of Pjimage when the slider is sliding. First, we're going to get a handle to Pjimage's figure, which can be passed through the Findobj function
To complete, and then convert the return value to a handle with guihandles. After that, you can use the converted handle to refer to any of the controls in Pjimage.fig. So, we're under the im2bw_args.fig.
Add the following function to the callback function of the slider bar:
H_pjimage=getappdata (HANDLES.FIGURE_IM2BW, ' h_pjimage '); axes (H_PJIMAGE.AXES_DST); Img_src=getappdata (H_ Pjimage.figure_pjimage, ' img_src '); BW=IM2BW (Img_src,val); Imshow (BW);
Then, add the following in the IM2BW_ARGS_OPENINGFCN:
H_pjimage=findobj (' Tag ', ' figure_pjimage '); H_pjimage=guihandles (h_pjimage); Setappdata (HANDLES.FIGURE_IM2BW, ' H_ Pjimage ', h_pjimage);
Then, save, run. The effect is as follows:
But what happens if we click on "Image binary" if we don't have a picture open? You can see that the displayed image is completely black and has no meaning at all, such as. So, we
The 
 can make the image processing menu unavailable when you do not click the Open menu item. So in PJIMAGE.M's OPENINGFCN, add the following program: 
set (handles.m_image, ' Enable ', ' off '); 
At the end of the callback function for the Open menu item, add the following program:
set (handles.m_image, ' Enable ', ' on '); 
In this case, you cannot use the commands in the image processing menu as long as you do not click "Open".
summary:
What is the final summary of what is mastered by the above applet? The use of the Open dialog box is the use of the Uigetfile function. It is important to note that when obtaining the path to the file to be opened, our two methods: one is fpath=[pathname filename] and the other is Fpath=fullfile (Path,filename). Then FullFile is a function that MATLAB provides for us. Judging whether the user clicked the "OK" or "Cancel" button, you can see if the returned filename and pathname are 0. Second, read into the picture, is the use of the Imread function. After the Imread function reads the picture successfully, it returns the pixel matrix that reads the picture, if the pixel matrix is three-dimensional, it means that the image has three channels. Just like RGB pictures, there are r channels, g channels and B channels. The first pixel is the first value of a matrix for each dimension. Third, set up shared data, is the use of Setappdata and Getappdata. Their first argument is a handle to the control, and the second parameter is the name of the application data you want to set or read. If it is setappdata, then the third parameter is the value you want to set. Four, save the picture, is the use of imwrite function. The first parameter is the pixel matrix of the picture, which is the return value of the Imread, and the second parameter is the full path to the picture, including the file name. The use of the Save dialog box is the Uiputfile function. The use method is similar to Uigetfile. The use of Input dialog box is InputDlg. Note that his return value is a cell type of data, that is, through {} to visit his internal, rather than the matrix commonly used (). Seven, the use of Menu editor, including menu bar and right-click menu. Viii. Use of the toolbar editor. Nine, under the callback function of a function, call another function. With the Feval function, the first argument is a function handle, followed by the argument passed to the function. In interface programming, Hobject is the handle to the current object, such as Pbtn_exit_callback is a callback of a button with a tag property of Pbtn_exit, so in this callback, Hobject is equivalent to Handles.pbtn_exit. As for EventData is automatically managed by the system, we don't have to change it. The handles is a struct variable that can be referenced to the handle of the control through the Tag property of the control. How to operate between the ten and two GUI interfaces. Use Findobj to find another GUI, and then convert it to an available GUI handle with Guihandles. Then, just like in your own. m file, use the dot operation to drawA control that uses another GUI, including reading its application data. Xi. You can use the GetImage function to get the data for the picture currently being displayed in an axis. 12, through the axes function to switch the current axis, that is, on which axis to draw. 13. Gets the value of the slider bar. 14. Set the value of static text. The use of XV, set, and get, these two are most commonly used to set or get a property of a control.
Appendix:
IM2BW_ARGS.M Final Source code:
function varargout = Im2bw_args (varargin)% Im2bw_args MATLAB code for im2bw_args.fig% Im2bw_args, by itself, creates A new Im2bw_args or raises the existing% singleton*.%% H = Im2bw_args Returns the handle to a new Im2bw_args or      The handle to% the existing singleton*.%% Im2bw_args (' CALLBACK ', Hobject,eventdata,handles,...) calls the local% function named CALLBACK in Im2bw_args. M with the given input arguments.%% Im2bw_args (' property ', ' Value ',...) creates a new Im2bw_args or raises the% E  Xisting singleton*.  Starting from the left, property value pairs are% applied to the GUI before IM2BW_ARGS_OPENINGFCN gets called.  an% Unrecognized property name or invalid value makes property application% stop.  All inputs is passed to IM2BW_ARGS_OPENINGFCN via varargin.%% *see GUI Options on the guide's Tools menu. Choose "GUI allows only one% instance to run (singleton)". percent See Also:guide, guidata, guihandles% Edit the above Tex T to MoDify the response to help im2bw_args% last Modified by guide v2.5 21-may-2016 20:20:56% Begin initialization code-do not                   Editgui_singleton = 1;gui_state = struct (' Gui_name ', Mfilename, ...                   ' Gui_singleton ', Gui_singleton, ...                   ' GUI_OPENINGFCN ', @im2bw_args_OpeningFcn, ...                   ' GUI_OUTPUTFCN ', @im2bw_args_OutputFcn, ...                   ' GUI_LAYOUTFCN ', [], ... ' Gui_callback ', []); if Nargin && Ischar (varargin{1}) Gui_state.gui_callback = Str2func (Varargin{1}); EndIf NAR Gout [Varargout{1:nargout}] = GUI_MAINFCN (Gui_state, varargin{:}); else GUI_MAINFCN (Gui_state, varargin{:}); end% end Initialization code-do not edit%---executes just before Im2bw_args is made Visible.function IM2BW_ARGS_OPENINGFCN (HOBJ ECT, EventData, handles, varargin)% This function have no output args, see outputfcn.% hobject handle to Figure% EVENTDA   TA reserved-to bes defined in a future version of matlab% handles Structure with handles and user data (see guidata)% varargin command line arguments to Im2bw_args (see Varargin)% Choos  E default command line output for im2bw_argshandles.output = hobject;% Update handles Structureguidata (hobject, handles);% Uiwait makes Im2bw_args wait for the user response (see Uiresume)% uiwait (HANDLES.FIGURE_IM2BW); H_pjimage=findobj (' Tag ', ' Figure_pjimage '); H_pjimage=guihandles (h_pjimage); Setappdata (HANDLES.FIGURE_IM2BW, ' h_pjimage ', h_pjimage);%--- Outputs from this function is returned to the command line.function varargout = IM2BW_ARGS_OUTPUTFCN (Hobject, EventData, Handles)% Varargout cell array for returning output args (see varargout),% hobject handle to figure% eventdata Reser Ved-to be defined in a future version of matlab% handles structure with handles and user data (see guidata)% Get DEFA Ult command line output from handles Structurevarargout{1} = handles.output;%---Executes on slider movement.function SLI Der_val_callback (Hobject, EventData, HanDLEs)% Hobject handle to Slider_val (see GCBO)% eventdata reserved-to is defined in a future version of matlab% hand        Les structure with handles and user data (see GUIDATA)% Hints:get (hobject, ' Value ') returns position of slider% Get (hobject, ' Min ') and get (hobject, ' Max ') to determine range of sliderval=get (hobject, ' Value '); Set (Handles.text_ Display, ' String ', Num2str (Val)); H_pjimage=getappdata (HANDLES.FIGURE_IM2BW, ' h_pjimage '); axes (H_PJIMAGE.AXES_DST) ; Img_src=getappdata (h_pjimage.figure_pjimage, ' img_src '); BW=IM2BW (Img_src,val); Imshow (BW);%---executes during object creation, after setting all Properties.function SLIDER_VAL_CREATEFCN (Hobject, eventdata, handles)% Hobject Handl E to Slider_val (see GCBO)% eventdata reserved-to is defined in a future version of matlab% handles Empty-handles Not created until after all Createfcns called% Hint:slider controls usually has a light gray background.if isequal (Get (H Object, ' backgroundcolor '), get (0, ' DefaultuicontrolbackgroUndcolor ') Set (Hobject, ' backgroundcolor ', [. 9.9.9]); end 


An example of a MATLAB graphical user interface (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.