Current location - Training Enrollment Network - Mathematics courses - Mathematical interface problem
Mathematical interface problem
Run the GUIDE command in the command window of MATLAB to open the wizard interface, as shown below:

Then, select an empty template (Blang

GUI), click OK to open the guide design interface, as shown below:

Click Menu Editor (Menu) on the toolbar.

Editor), open the menu editor, as follows:

In the menu

Create a new menu item named File in the toolbar. See the figure below for other settings:

Add menu items under the File menu: Open, Save and Exit. See the figure below:

If you need to add a separator on the menu item Exit, select Delimiter.

The above item will do.

Save my interface as pjimage.fig

After saving, the pjimage.m file will be opened automatically, and all our programs will be written in this m file. In programming, every mouse action corresponds to a callback function. So is our menu.

On the interface, right-click and select Properties.

Inspector, you can open the properties window. When we click on different controls, their corresponding properties will be displayed here, and we can modify them. The most important attributes are the label attribute and the string attribute.

Set the Tag attribute of the current graphics window as: figure_pjimage, and the title (Name attribute) of the window as: Image Processing Instance. As follows:

Then, click the Save button in the toolbar. After that, click the Run button in the toolbar (Run

Figure). Note that the icon in the toolbar will be prompted, such as the "Run" button.

We will see the following interface:

This means that our catalogue has been saved. The fig file is not the current directory, but it doesn't matter. We just need to click "Change".

Directory "to change the current directory. Of course, if you want to add the current directory to the MATLAB path, click "Add to".

Path "is ok. I suggest clicking "Change" here.

Directory ",because there is no great need to add it to the MATLAB path. Generally, we need to add a toolbox or complete our function or program. When we can't find our function in the command window of MATLAB, we can add the directory where the function or program is located to the MATLAB path.

Anyway, clicking that button depends on personal hobbies. No matter which of the two buttons is clicked, the program will run correctly.

When our program runs, it looks like this:

We can see the menu items and shortcut keys under the file, but we didn't write the program, so even if we clicked, there was no response. If you don't want to set shortcut keys, you can set them in the menu.

Set it in the editor and select Ctrl+none, as follows:

In this case, there is no shortcut key to save the project. We can check the response function of this menu item through the button "View" above, that is, the callback function. You can also see in pjimage.m that if the saved Tag attribute is m_file_save, then the name of its corresponding callback function is m_file_save_Callback. And so on.

Let's write a function to open a menu item. To open a picture, of course, you need to open a dialog box. In interface programming, the function of opening dialog box is uigetfile.

You can use the help uigetfile command to view its detailed description. The following is the response function to open the menu:

function

m_file_open_Callback(hObject,eventdata,handles)

[file name, path name] =

uigetfile( ...

{'*.bmp*.jpg*.png*.jpeg',' Image file (*. bmp,

*.jpg,*。 png,*。 JPEG)’; ...

*. *' All documents

(*.*)'}, ...

Pick one

Picture');

Save. M file and run the program. Click "Open" under "File" to open the following open dialog box:

After selecting a file, the file name in the program is the file name of the file you selected, and pathname is the path of the directory where the file is located. For example: file name

=5.jpg, pathname = C:\ Documents and Settings \ Administrator \ My

Documentation \.

So how to read and display a picture after obtaining the path? The imread function can be used to read in the picture, and the display can be on the coordinate axis. Then we need to draw a coordinate axis on the interface. For comparison, we drew two coordinate axes, one before processing and the other after processing. And the Tag attribute of the machined front axle is changed to axes_src, and the Tag attribute of the machined axle is axes_dst. After making changes, save. As follows:

Then on the basis of the original m_file_open_Callback program, add the following program:

axes(handles . axes _ src); % Use the axes command to set the axis of the current operation to axes_src.

Fpath =[ pathname

File name]; % Combines the file name and directory name into a complete path.

im show(im read(fpath)); % read the picture with imread and display it on axes_src with imshow.

Run the program and open a picture through the "Open" menu item. The effect is as follows:

So how do you save a picture? Use the imwrite command. But the first parameter of the imwrite command is the picture data you read in, which is the return value of imread. In this case, we need to make some changes to the program in m_file_open_Callback. Change the last sentence (imshow(imread(fpath)) into two sentences as follows: img _ src = imread(fpath);); imshow(img _ src);

Moreover, how to get the img_src variable under the callback function of the save menu? Here, img_src will be used as * * * shared data. Many friends of interface programming like to declare with global. Personally, I don't like it, because there is a better way. That is to use the setappdata and getappdata functions. We can add application data for any space with Tag attribute on the interface. Of course, I prefer to add these * * * application data to the graphics window, because it is easy to remember, but it is not easy to remember if there is a control.

Yes. M file, you will find that besides the callback function of each menu item, there are two functions: pjimage_OpeningFcn and pjimage_OutputFcn, which are equivalent to the initialization function of the interface, and pjimage_OpeningFcn is the output function of the interface, that is, the call. M file instead of running fig.

Therefore, we need to add the following programs to pjimage _ OpeningFcn to enjoy this img_src matrix. The code is as follows:

setappdata(handles . figure _ pj image,' img_src ',0);

Then, write the following program at the end of the m_file_open_Callback function:

setappdata(handles . figure _ pj image,' img_src ',img _ src);

Then, we can extract img_src from the m_file_save_Callback function like this, as follows:

img _ src = getappdata(handles . figure _ pj image,' img _ src ');

Then when saving, the save dialog box will naturally be used. To use the save dialog box, you need to use the uiputfile function. For more information, please use the help.

Uiputfile view.

Then, save the program under the menu item (m_file_save_Callback), as shown below:

[file name,

Pathname] = uiputfile({'*. Bmp',' BMP file'; *.JPG',' jpg file'},' Select one.

Picture');

If isequal (file name, 0) || isequal (path name, 0)

Return; % If you click Cancel,

other

Fpath = full file (pathname,

File name); Another way to get the full path%.

end

img _ src = getappdata(handles . figure _ pj image,' img _ src '); % get the data of the open picture.

imwrite(img_src,fpath); % Save picture

The following is the procedure to exit the menu item. Exit the interface and use the close function, but there will always be a prompt. For example, if you have processed the picture without saving it, you should give a prompt when closing it and ask whether to save it. However, I won't do this work here for the time being, and I'll write it later when I need it. Therefore, the process of exiting the menu item here is only one sentence, as shown below:

close(handles . figure _ pj image);

In fact, you can also use the delete function, namely: delete (handles. figure _ pjimage); Depends on your mood.

But when you run the program, you will find that if you click the "Cancel" button while opening the picture, an error will pop up in the command window of MATLAB, because we didn't handle the cancellation. Let's deal with this problem, as long as the following program of m_file_open_Callback is changed to the following program:

[file name,

Pathname] = uigetfile ( ...

{'*.bmp*.jpg*.png*.jpeg',' image file

(*.bmp,*。 jpg,*。 png,*。 JPEG)’; ...

*. *' All documents

(*.*)'}, ...

Choose an image');

if isequal(filename,0) ||

Isequal (pathname, 0),

Return;

end

axes(handles . axes _ src);

Fpath =[ pathname

File name];

img _ src = im read(fpath);

imshow(img _ src);

setappdata(handles . figure _ pj image,' img_src ',img _ src);

Let's do a binary image processing. Add an Image Processing menu as above, as shown below:

Add the menu item "Image Binarization" below it, as follows:

Then, click OK to close the menu editor and save the entire interface. If our. M file, we can click the "View" button in the above figure to generate a callback function. There is a threshold for image binarization, so we can create a new interface and put a slider on this interface to set the threshold for image binarization. At the same time, there is a text showing the value of the current slider. Then let's create a new blank interface and draw a static picture on it.

Text and slider controls, and then use the toolbar alignment tool (alignment

Object) to two spaces. As follows:

Then, save this interface as im2bw_args.fig The whole design is as follows:

You can set static.

Text's FontSize property is 10, so the font will be larger. Set static

The Tag property of the text is txt_display, and the Tag property of the scroll bar is set to slider_val. In order to be able to scroll on the scroll bar, static

Text displays the value of the scroll bar. You need to write the following program in the callback of the scroll bar. You can right-click the scroll bar and select View.

"Callback" under "Callback" goes directly to the callback function of the scroll bar (slider_val_Callback).

val=get(hObject,' Value ');

set(handles.txt_display,' String ',num2 str(val));

Save, run the program, you can slide the scroll bar, static

The text will display the corresponding value. Double-click figure to open the property window of figure (the window with a box at the bottom), set its Tag property to "figure_im2bw" and its Name property to "Set the threshold of image binarization". Then, save the interface. At run time, it looks like this:

Then, what we think is that when the slider slides, a binary image will be displayed on the axes_dst axis in pjimage. fig So what should we do? First, when you click "image binarization" under "image processing" in the PJI image. The FIG menu, im2bw_args.fig will be opened. This is the time for us to call im2bw_args.m. When calling, we will return a handle, which points to the open im2bw_args.fig For more details, please refer to the comments in front of the IM2BW _ args.m file, which reads:

%

H = IM2BW_ARGS returns a handle to a new IM2BW_ARGS or a handle to.

% the

existent

Single case *.

That is to say, we can open im2bw_args.fig as shown above. So under the callback function of "image binarization" (m_image_2bw_Callback), write the following program:

h = im2bw _ args

Then, save pjimage.fig. Also, it is better to save im2bw_args.fig and pjimage.fig in a directory. Then run pjimage.fig, and you can see that clicking "image binarization" will open im2bw_args.fig, and the response value will also be displayed when the slider slides.

Let's talk about how to display the sliding binary image to the axes_dst axis of pjimage when the slider slides.

First of all, we need to get the handle of the figure of pjimage, which can be done through the findobj function, and then use guihandles to convert the return value into a handle. You can then use this converted handle to refer to any control in pjimage.fig Therefore, we added the following functions to the callback function of slider under im2bw_args.fig:

h _ pj image = getappdata(handles . figure _ im2bw,' h _ pj image ');

axes(h _ pj image . axes _ dst);

img _ src = getappdata(h _ pj image . figure _ pj image,' img _ src ');

bw=im2bw(img_src,val);

im show(bw);

Then, add the following in im2bw_args_OpeningFcn:

h_pjimage=findobj('Tag ',' figure _ pj image ');

h _ pj image = GUI handles(h _ pj image);

setappdata(handles . figure _ im2bw,' h_pjimage ',h _ pj image);

Then, save and run. The effect is as follows:

But what happens if we click on "image binarization" without opening the image? You can see that the displayed image is completely black and completely meaningless. Therefore, when the "Open" menu item is not clicked, we can make the "Image Processing" menu unavailable.

Then in the OpeningFcn of pjimage.m, add the following program:

set(handles.m_image,' Enable ',' off ');

At the end of the callback function of the Open menu item, add the following program:

set(handles.m_image,' Enable ',' on ');

In this case, as long as you don't click Open, you can't use the commands in the Image Processing menu. The effect is as follows:

Click "Open" and you can use it.

Next, let's talk about the previous question, that is, do you want to save the picture? First of all, we need to set two flags: one is whether the picture has been processed and the other is whether the picture has been saved. Then we add the following two application data in pjimage_OpeningFcn.

setappdata(handles . figure _ pj image,' bSave ',false);

setappdata(handles . figure _ pj image,' bChanged ',false);

Then, in the callback function of the "image binarization" menu item, change the value of bChanged to true, that is, add the following program:

setappdata(handles . figure _ pj image,' bChanged ',true);

Because we want to save the image in axis axes_dst, and "Save" under "File" is essentially to save the image in axis axes_src, what should we do? We must add another "Save" menu item. This time, we added a right-click menu to axis axes_dst.

Open the menu editor in the toolbar and select the context.

Menu (context menu), as shown below:

Then, create a new context.

Menu, whose label attribute is axes_dst_menu, as shown below:

Then add a menu item to it: Save, and its Tag attribute is axes_dst_menu_save, as shown above. Then, right-click axis axes_dst and select Properties.

Inspector. change the UIContextMenu property of the axis to axes_dst_menu.

As shown in the figure below:

Then, save and run. Right-click axes_dst, and you will see the Save menu. Let's write about its function.

[file name, path name] =

uiputfile({'*。 Bmp',' BMP file'; *.JPG',' jpg file'},' Select an image');

if

Isequal (file name, 0) || isequal (path name, 0)

Return;

other

Fpath = full file (pathname,

File name);

end

img _ dst = getimage(handles . axes _ dst);

imwrite(img_dst,fpath);

setappdata(handles . figure _ pj image,' bSave ',true);

But you will find that right-clicking axes_dst before looking at the picture has a menu. Once binarized, there is no menu when you right-click again.

However, when we add the right-click menu axes_dst_menu to the UIContextMenu in the figure window (there is no control here, so double-click to open the figure's property window), the above problems will not happen and everything will run normally. Because when adding to axes_dst, once the content of the axis changes, the right-click menu will be attached to the parent object. Therefore, if the right-click menu needs to be displayed on the coordinate axis, it will be created through the program. How to create, let alone. Let's talk about what to do when saving axis axes_dst and exiting the program.

Change the original m_file_exit_Callback to the following program:

b changed = getappdata(handles . figure _ pj image,' b changed '); % change?

b save = getappdata(handles . figure _ pj image,' b save '); % get saved?

if

B changed = = true & amp & Ampbsave = = false,% changed but not saved.

BtnName=questdlg ('You have changed the picture but didn't save it. Do you want to keep it? ,' prompt',' save',' don't save',' save'); % Use the Question dialog box

Switch btnName,

Case "Save",% performs the function of axes_dst_menu_save_Callback.

Fe val(@ axes _ dst _ menu _ save _ Callback,handles.axes_dst_menu_save,eventdata,handles);

The case "Don't save",% do nothing.

end

end

h=findobj('Tag ',' figure _ im2bw '); % Determines whether to open the window for setting image binarization parameters.

if

~isempty(h),% If found, close it.

Close (h);

end

close(findobj('Tag ',' figure _ pj image '); % Close the main window

Let's add a toolbar to the program. Click the toolbar on the toolbar.

Editor, open the following:

Select "predefined"

Open it under Tools and click Add. Select Save again and click Add. And change the label attribute of the Open button to TBL _ Open, and change the label attribute of the Save button to TBL _ Open, Save, as shown below:

Click "View" to find the callback of the open button, and call the callback of the open menu item in the menu under it. You need to write the following program under the callback of the open button:

feval(@m_file_open_Callback,handles.m_file_open,eventdata,handles);

Similarly, find the callback of the save button and write a save program under it, but we need to judge whether it is the first time to save, if so, use the save dialog box; If not, we can save it directly in the first saved path. Then, we need to set up several application data. The first is whether the record is saved for the first time, and the second is the path where the record is saved for the first time. In this case, we add the following code in pjimage_OpeningFcn:

setappdata(handles . figure _ pj image,' fstSave ',true);

setappdata(handles . figure _ pj image,' fstPath ',0);

Then, under the Save button callback, write down the following program:

FST save = getappdata(handles . figure _ pj image,' FST save ');

if(fstSave==true)

[file name, path name] = uiputfile({'*. Bmp',' BMP file'; *.jpg',' jpg file'},

Choose an image');

if isequal(filename,0) ||

Isequal (pathname, 0)

Return;

other

Fpath=fullfile (path name, file name);

end

img _ dst = getimage(handles . axes _ dst);

imwrite(img_dst,fpath);

setappdata(handles . figure _ pj image,' fstPath ',fpath);

setappdata(handles . figure _ pj image,' bSave ',true);

setappdata(handles . figure _ pj image,' fstSave ',false);

other

img _ dst = getimage(handles . axes _ dst);

fpath = getappdata(handles . figure _ pj image,' FST path ');

imwrite(img_dst,fpath);

end

Moreover, we also need the "Save" button under the file and the "Save" button in the toolbar to be unavailable before opening the picture, and only available when clicking "Open" under the file or "Open" under the toolbar. Then you need to add the following code in pjimage_OpeningFcn:

set(handles.tbl_save,' Enable ',' off ');

set(handles.m_file_save,' Enable ',' off ');

And under m_file_open_Callback, add the following code:

set(handles.tbl_save,' Enable ',' on ');

set(handles.m_file_save,' Enable ',' on ');

Such a small program is over.

If you can follow the above steps to complete this small program, then you have mastered the problems in interface programming.