Friday, 31 July 2015

Making a GUI interface in MATLAB

In reference to my last post regarding ping pong game with object tracking, I pushed my work a little bit more and incorporated Kalman Filter and GUI with it.

The following post contains about the GUI creation for beginners in MATLAB. I will post about Kalman Filter soon.


GUI in MATLAB

It is pretty simple to make a GUI in MATLAB. You can find tons of videos on YouTube and even on MATLAB central regarding this.

Type guide and press enter in the command window of MATLAB.
  1. A window will pop-up with "drag and drop" options of various components available for GUI you want to make. 
  2. You may add text-boxes, editable text boxes, sliders, radio buttons, push buttons etc. using panel on the left of guide window and double click on any component to open its property inspector where you can set various fields like Value, Max, Min, Name.
  3. Drag all that you need and save the *.fig file.
  4. To customize a component, right click on component and gotoView  CallBacks > CallBack
  5. This will direct you to the function where Callbacks of components are handled.
    In case of push buttons, type the code you want to be performed on click in that space.
    In case of Value returning components (sliders, radio-buttons), use get() function to get values from component used upon some action performed. For example, to get position of slider,

    variable=get(handles.slider_name,'Value');
where,
           variable is a global variable so that the scope of this variable is beyond a single Class and can be directly used outside the GUI codes in your further process. You can also use varagout() part in the GUI *.m file to get a value returned.
           slider_name is the name of component as specified in the tag field in the Property Inspector of the component.

     6.  If you want to plot a graph or preview an image on the GUI panel, you can add an axes component. To show images on this component, type imshow(image,axes_name)

In the following, video I used guide to make a GUI interface for setting parameters to filter my image for object tracking. On clicking OK, the parameters are transferred to my Tracking and Kalman Filter algorithms which provide a much more effective way of making a generalized object tracking.

Sliders used :
Brightness : Add a positive whole number to every element in image array to increase its brightness.
Contrast : Vary gamma value in imadjust() function for contrast. gamma>1, gamma=1 and  gamma<1 represent relation in intensity of input and output image. Check out Example on imadjust() to use gamma. P.S. : Do not name variable as gamma; it is also a function. :p
Rmin, Rmax, Gmin, Gmax, Bmin, Bmax : RGB minimum and maximum value for segmentation.
Lower Area and Upper Area : Perform connected component analysis and filter Area using lower and upper limit of area from here.

On pressing, a global flag is set high and the program comes out of the infinite loop to continue forward.



Friday, 17 July 2015

Integrating MATLAB and Visual Studio using Engine API

Tutorial 2 : Writing Engine scripts.

After having set Visual Studio to be integrated with MATLAB, you need to add some fragments of code along with your C/C++ program to call or send values to MATLAB.

Everything you need will be contained in the libraries engine.h and matrix.h

Every engine application will have a pointer of data type Engine that defines an engine session. We will be using variable addresses to copy values using pointers and using in either MATLAB or C or both.

Opening and closing engine sessions :

Following functions will be needed for its working :
  • ep = engOpen() - where engine session starts. ep of type for MATLAB Engine.
  • engClose(ep) - closes engine session defined by pointer ep
On Windows system, engOpen() will have argument "NULL"

Pointers to store values for transferring between MATLAB and C :

Remember, for transferring arrays and values with MATLAB, we need to have an intermediate data-type, mxArray. Creates MATLAB arrays.

Define pointers of mxArray data-type as an array using, mxCreateDoubleMatrix, mxCreateNumericMatrix, etc. (See documentation for further types and its arguments)
For example, mxArray *array = mxCreateDoubleMatrix(5,3,mxREAL); 
creates a pointer to a double matrix with 5 rows and 3 columns and has elements belonging to Real Numbers.

Performing the actual putting,getting and evaluation of values in MATLAB workspace :

Following functions might be used to get or put value in mxArray variables from/into MATLAB workspace :
  • engEvalString(ep,"MATLAB code") - Puts whatever written in string to MATLAB workspace opened by ep Engine session pointer.
  • engPutVariable(ep,"MATLAB variable",mxArray pointer) - Puts values from an address in C space to an address in MATLAB workspace.
  • mxArray pointer = engGetVariable(ep,"MATLAB variable") - Gets values from MATLAB workspace variable specified in " " and is stored in mxArray pointers.
  • mxGetPr(mxArray pointer) - Gets the starting address of the first real element of pointer.
We know how to start engine, how to make MATLAB array pointers, how to run commands on MATLAB workspace, how to put and get values in MATLAB array pointers from workspace but we still have the gap between MATLAB and C; to get values from mxArray pointers to int/double/etc. data type variables in C we use 
memcpy(address of destination,address of source,number of bytes to be copied)

Use (void *) , also called General Purpose Pointer as it can be used as any data-type pointer.

Moreover, along with engOpen you can add error messages with it.(Refer Documentation of MATLAB)

engwindemo.c is a good example to learn from. Below will be the screenshots of my code, describing stages of Engine session. For full code, you can visit my github repo HERE



Initialize Engine session and define MATLAB array pointers.


Sending commands to MATLAB workspace


Getting values from MATLAB workspace

Closing engine session