Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

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


Saturday, 11 July 2015

Ping-Pong game using Object Tracking

I thought of this project way back but had to pause working on it for about a month. So when I sat free again, I finally made it work the way I wanted. (Needs debugging though)

This project has three modules:
1. A C program of Ping Pong
2. Real time Object Tracking using MATLAB
3. Integration of MATLAB and Visual Studio using Engine API.

If you visit my older posts, you may find enough description of Object Tracking and Engine API (I remember I have to write the other part of Integration tutorial, it will be soon). The remaining one, Ping Pong game, was not very hard to make. You just need to make four independent objects, two bats, a ball and a game arena and keep them displaying at every loop.

The important logic behind the motion of balls is the direction x and direction y. When the ball hits the walls, its y direction will negate but its x direction is same. On the other hand when hit by a bat its x direction changes while y remains same. This is enough to design a simple Ping Pong game. You can go through the code at my github link posted at the end.


This video was taken a few hours after my code gave the first desired output. The results have a lot of errors still, but it is just a prototype. Given some time I can try to make it as flawless as possible.
The only part to explain is the integration of both softwares, which I shall post soon. Hit a +1 if you liked it and do comment out your views and suggestions.

Here's the github link, Ping-Pong-with-finger-tracking-

Tuesday, 7 July 2015

Integrating MATLAB and Visual Studio using Engine API

This one made me scratch my head to the scalp; literally. So I am going to provide a thorough tutorial on "Using MATLAB Engine API for C++(C)".

TUTORIAL 1 : SETTING IT ALL UP

Requirements :

1. Microsoft Visual Studio (This tutorial will be based on version 2010, moreover they are all similar)
2. MATLAB

Things to know before starting?

  • Find out on what platform is your MATLAB running, that is 32 bit or 64 bit. To do so, go to Start > Right Click on Computer >Properties > Advanced system settings > Environment Variables > In System Variables scroll down to Path > Double click and check whether it has MATLAB bin under win64 directory or win32.

Steps :

  1. Open Visual Studio and create a new project. You might probably choose "Win32 Console Application" and check the "Empty Project" option in dialog box.
  2. Visual Studio by default runs on a 32 bit platform. If your MATLAB is running on a 64 bit platform (most probably this will be the case) then you will have to make sure that VS(Visual Studio) is going to build applications on a 64 bit platform otherwise skip this step. To do so, click on Project menu > project-name properties (this will open Property Pages dialog box)> Configuration Properties > Configuration Manager > Click on drop down menu of "Active solution platform" > New > from drop down select x64 > close and click ok.
  3. Now your VS is capable of building files on 64bit platform. Open Property Pages and select VC++ directories and make sure you have something like "$(VCInstallDir)bin\x86_amd64;" at the beginning, which ensures the 64 bit platform if your MATLAB is on 64 bit too.
  4. In the Property Pages, click on Debugging and in the Environment field add this,

    PATH=(MATLAB installation directory)\bin\win64
  5. In the Property Pages, click on VC++ directories and add the path to include directories by opening,

     (MATLAB installation directory)\extern\include

    I will suggest copy pasting the path from properties dialog box.
  6. Now add library path in Library directories field by opening,

    (MATLAB installation directory)\extern\lib\win64[or win32, whatever you are working on]\microsoft
  7. Now you have to add the executable directories which is as follows,

    (MATLAB installation directory)\bin
  8. Now click on Linker on the left side pane and add the same library path to the "Additional Library Directories" field. Make sure "Enable Incremental Linking" is set to "No" (It pops some error that I don't know why but read this fix somewhere and it works).
  9. In the Linker dropped down, select Input and add the following libraries by clicking on "Additional Dependencies" and move cursor to end and write with semicolons after every library as follows,

    libmx.h;libmex.h;libmat.h;libeng.h;
  10. Hit OK!
If all went well then this is it. Now you are ready to use Engine functions to call MATLAB from Visual Studio.

I will post the next part of this tutorial about writing a Engine file and calling MATLAB soon. If you face problems in setting this up, comment out and I will try to solve because I skipped few minor things.

To check it's working you can go to MATLAB documentation, search for MATLAB engine API and open C,C++ and Fortran and copy paste an example in VS project and see if it works!

Wednesday, 27 May 2015

Backtracking algorithm is a great tool.

So I wanted to solve the SUDOKU I just recognized from the image I fed in MATLAB and I tried various logic but nothing worked better than "Backtracking". You can find a great explanation of this on YouTube or  http://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/  .
Basically, a function is made to recur and return values as true or false if the consistency is maintained or not respectively.

Unlike N-Queen problem where you can output simultaneously, you might find need a global variable that stores the ANSWER matrix of SUDOKU if no empty boxes are possible; or else returning values and stepping out of recurring functions might result in losing of the solved matrix.
I could solve a 9x9 SUDOKU in 3 seconds. My program needs optimization.

Will keep posted on program optimization and better image processing techniques because the current one carries several cons.

Friday, 22 May 2015

Glitch in the "Bounding Box" method.

Regarding the last post. The method I posted had a major drawback.
I will explain with the help of an example.

I tried to feed my code a real sudoku problem. And it so happened that my OCR results storing array had a number "95126".

This was because it considered the less spaced numbers as a single number and hence stored in a single cell. This was a havoc for me; but I found an alternative. I will post that soon,
Keep smiling. :)

Found a solution to storing characters recognized from MATLAB in an array in a program to solve Sudoku.

The key were bounding boxes.
I found out the elements recognized having same bounding box row coordinates and hence stored their column coordinates in same row of an array and changed row of array when it didn't meet my condition of same row location.
This way I got an array of elements of my sudoku puzzle sorted row-wise.

Here's the trick for storing it column-wise.
I made a scale of pixels showing distance between a block and first block. You can find out prcatically using impixelinfo how many pixel difference corresponds to number of blocks.
Like in my case 150 pixels corresponded to center to center of two adjacent boxes. Hence I named a variable to store "what-multiple-of-150" is the non-empty character containing pixel.

This way you can sort it column-wise and hence get a complete Sudoku puzzle from page to matrix using basic knowledge of MATLAB.

P.S. This method has major drawbacks. I will post the alternative I found soon. Thank you.  

Wednesday, 20 May 2015

Stuck. Need help.

I am making a sudoku solver using MATLAB. So far I completed cropping my sudoku puzzle from image and even extracting numbers accurately. But the problem is the numbers are extracted in text file of OCR regardless of position of where they are. Need help to store my recognised characters in a matrix to further process for solution.