### Contour Identification and next steps.

My algorithm now compares contours of the same size and returns 1 if it’s of the same class and -1 if they are not from the same class. For achieving that I need to train the algorithm so it can create a SVM for the class.

The problem
The problem is that contours need to be the same size and I need to identify the center of the image to identify it as being as the same class.

The (not) solution
I will resize the image that represents  the contour to the size of the template. Then I will compare them.

But this solution does not work as I would expect. Here’s why:

When resizing the contour our intuition will not be the same as what actually resizing do FOR CONTOURS.

For example (in matlab):

Suppose you have the following matrix that represents the contour:

```>> A = [1,0; 1,1]

A =

1 0
1 1
```

When using imresize, you get the following:

```>> B = imresize(A, 2)

B =

1.0753 0.7826 0.1471 -0.1456

1.0560 0.8381 0.3650 0.1471

1.0143 0.9587 0.8381 0.7826

0.9951 1.0143 1.0560 1.0753

```

This is not the resized contour that we expected. First of all, the entries are not only either 0s or 1s. Because the default method for resizing the image is the bicubic method, meaning that each pixel is a weighted average of pixels in the nearest 4-by-4 neighborhood.

Therefore if the input image is only of 0s and 1s, the output image will not necessarily be of only 0s and 1s.

One can tweak this by using other methods for resizing the image, but none of them will provide a satisfactory answer. For example using the method of Nearest Neighbor will give the following:

```B = imresize(A,2,'nearest')

B =

1 1 0 0

1 1 0 0

1 1 1 1

1 1 1 1
```

The REAL solution:

The real solution is to get the input image (RGB) resize it to the size of the template model and then find the contour of the image and compare it to the template.

### Generating C code from MATLAB code and the Art of Speeding up MATLAB Applications.

To generate C code from Matlab code is not as simple as it looks. MATLAB code does not map directly to c code, so it’s necessary to modify the code before transforming it to a C library.

Therefore there is a lot of rewriting the MATLAB code before transforming it to a C standalone library. For that it’s necessary to:

– Initialize each variable before its used by specifying its size and data type.

In the case that the variable needs to vary its size during execution, then you have 3 options:

1. Static allocation with fixed maximum size

2. Variable sizing with maximum size allocation.

3. Variable sizing with dynamic memory allocation.

In any case, this seems like a troublesome experience. For a more detailed description on how to do this, take a look here.

If the reason why you wanna generate C code from MATLAB code is that you want to speed up your application,  take a look here.

### Dynamic libraries and make install

I finally solved the problem that has been hunting me since the beginning of this week. There are some lessons that I’ve learned doing this work:

Dynamic linking libraries at compiling time is different than dynamic linking then at runtime. Both need to be done.

To link during compile time you need to do the following:

Add the header files to the directories of header files that are included, the option -I does that.

For adding dynamic libraries, the option -L adds directories to the list of directories that the compiler will look for the libraries. The -l will add the library itself (the name)

For resolving the linking issues when running the binary:

Either you can create a install option, by doing the following:

1. Copying the libraries to the common library directorye

>> install:
>> cp ./lib/libsegmentImage.dylib /usr/lib

Or:

2. Change the \$DYLD_LIBRARY_PATH to the directory of the libraries:

>> export DYLD_LIBRARY_PATH=/Applications/MATLAB/MATLAB_Compiler_Runtime/v81/runtime/maci64/

Other tips:

To debug problems when running a binary file, the following tool can help to find what and where the problem is happening:

>> dtruss ./KinectVideoHandler 2> out
>> less out

Another way of doing that is to:

Create a text file:

>>  #!/bin/sh
>>  # set dynamic preload library path
>>  export DYLD_LIBRARY_PATH=/Applications/MATLAB/MATLAB_Compiler_Runtime/v81/runtime/maci64/
>>  ./KinectVideoHandler

After linking all the libraries and set pathnames I ran the compiled version of the code and I started getting the following error:

Referenced from: /opt/local/lib/libopencv_highgui.2.4.dylib
Reason: Incompatible library version: libopencv_highgui.2.4.dylib requires version 8.0.0 or later, but libtiff.5.dylib provides version 6.0.0
Trace/BPT trap: 5

The error is that the library that is being referenced libtiff (or the tiff library) is old and needs to be updated. There are some few tricks that helped me solve the problem, first I use the following code:

http://trac.macports.org/ticket/15554

But instead of png I awas looking for the tiff library, thus I used the following code:

>> sudo port installed | grep tiff

For loading the libraries I have set the path of the dynamic libraries using the variable:

`DYLD_LIBRARY_PATH`

The DYLD_LIBRARY_PATH path in my case was set to be

:/Applications/matlab/MATLAB_Compiler_Runtime/v81/runtime/maci64/:/Applications/matlab/MATLAB_Compiler_Runtime/v81/sys/os/maci64/:/Applications/matlab/MATLAB_Compiler_Runtime/v81/sys/os/maci64/:/Applications/matlab/MATLAB_Compiler_Runtime/v81/bin/maci64/:/Users/giu/Documents/GRobot/Code/video/lib/

where

/Applications/matlab/MATLAB_Compiler_Runtime

is the <mcr_installed> path.

But when I set the DYLD_LIBRARY_PATH I get the following error:

Referenced from: /opt/local/lib/libopencv_superres.2.4.dylib
Expected in: /Applications/matlab/MATLAB_Compiler_Runtime/v81/bin/maci64//libopencv_core.2.4.dylib
in /opt/local/lib/libopencv_superres.2.4.dylib

I imagine that there is some function that is called inside the library libopencv and it is not found.

It might be that the library that I install or link does not have the function that it’s required or has another name.

## Solution:

The solution for the problem can be found here:

https://raulzito234.wordpress.com/2013/05/02/dynamic-libraries-and-make-install/

Take a look at:

http://stackoverflow.com/questions/13252852/dyld-symbol-not-found-jpeg-resync-to-restart

Edit:
In the installation process, it’s advised to do the following:

On the target computer, append the following to your DYLD_LIBRARY_PATH environment variable:

/Applications/MATLAB/MATLAB_Compiler_Runtime/v81/runtime/maci64:/Applications/MATLAB/MATLAB_Compiler_Runtime/v81/sys/os/maci64:/Applications/MATLAB/MATLAB_Compiler_Runtime/v81/bin/maci64:/System/Library/Frameworks/JavaVM.framework/JavaVM:/System/Library/Frameworks/JavaVM.framework/Libraries

Next, set the XAPPLRESDIR environment variable to the following value:

/Applications/MATLAB/MATLAB_Compiler_Runtime/v81/X11/app-defaults

### Dynamic Libraries vs Static Libraries.

When coding and trying to port MATLAB code to a C++ application I ran into some problems loading some dynamic libraries. See last post for more information

I never understood what was the difference between dynamic libraries and static libraries and the need for it. So I decided to learn it.

What is the difference between a dynamic library and a static library:

Dynamic libraries are not loaded to the executable code of a binary application. They are loaded in the fly. Dynamic libraries are not loaded into the heap.

Static libraries is compiled with the application and is kept together with the application. Wasting precious heap memory. [1]

I’ve seen many times articles referencing the heap and stack and I never understood the difference between the two, so I decided to google it.

What is the difference between stack and heap?

The stack is where memory is allocated for automatic variables.

Heap is a more stable storage than the stack. Memory allocated in the heap is there until the application is closed.

PS: For a more in depth explanation please visit the following websites:

[1]https://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/OverviewOfDynamicLibraries.html#//apple_ref/doc/uid/TP40001873-SW1

### Using compiled matlab compiled code in c++ program

After compiling the code I found the following error when I ran:

>> make

The problem listed was the following:

Undefined symbols for architecture x86_64:
“_mlfSegmentImage”, referenced from:
_main in KinectVideoHandler-KFLNAR.o
“_mxCreateString_proxy”, referenced from:
_main in KinectVideoHandler-KFLNAR.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [KinectVideoHandler] Error 1

Apparently (according to description here the problem is the following:

1) The object is present but the linker doesn’t look for
it in the right place.

2) The object is not present, because the matlab system
is set up to work with a 32-bit compiler.

3) The object is not present because the compiler doesn’t
support architecture

### Makefile tutorial

I did not understand until today how exactly a MAkefile of c++ code worked until today. I’m glad I finally nailed it. If you want to understand it a little bit better how to do it, here is a nice tutorial of how to write a Makefile:

http://www.cs.bu.edu/teaching/cpp/writing-makefiles/

### How to transform MATLAB code into C++ code.

When you compile matlab code using `mcc` (by default or when using the `-m option`), you get an executable. So from your C++ file, you can call the matlab executable with the C/C++ command `exec`.

If you use the `-l option` (using `mcc`), you get a shared library, and header. For instance if you type (in matlab):

mcc -l test.m -W cpplib:test.h

This should produce a shared library `test.lib` or `test.so`, and a header `test.h`

In `test.h` you should have line similar to that:

bool MW_CALL_CONV mlxTest(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);

You can call your matlab function using that.

### Architecture of GRobot

This is a premature view of how the architecture of Rosie v1.0

### libfreenect Drivers for Mac OS X

Here a quick — and when I say quick, I mean quick — guide to get you up and running with libfreenect drivers on a Mac OS X.

First up, I made the switch from MacPorts to HomeBrew a while ago and if you decide to take this road, this guide is super easy. If you aren’t using HomeBrew yet, it might be a good time to check it out. It’s a great alternative to MacPorts. You can download it at https://github.com/mxcl/homebrew

I’ll bust this tut (ew?) up into two categories, the HomeBrew installation method and MacPorts. First up, HomeBrew.

## Installing libfreenect using HomeBrew

All commands you see in this guide will need to be done using a Terminal Application. You can find the Terminal in Applications -> Utilities -> Terminal

If you currently have HomeBrew installed, run brew update in order to make sure everything is up to date.

>> brew update

NOTE – You may have to use the ‘sudo’ command if you haven’t ran this script yet.

For the time being, you’ll need to manually retrive a few formulas in order to successfully install libfreenect. Invoke the following commands:

To install libfreenect, simply type the following:

>> brew install libfreenect

Once it’s finished, you should be able to simply type in glview — make sure your Kinect is plugged in, that goes for both the USB and into power outlet — and run the following:

>> glview

## Installing libfreenect using MacPorts

Using MacPorts to install libfreenect will require a few additional steps, mainly compiling the build using cmake. Here’s what you need to do to get things started.

Install git, cmake and these libraries:

>> sudo port install git-core
>> sudo port install cmake
>> sudo port install libtool
>> sudo port install libusb-devel

I created a working directory for all my Kinect junk aptly name ‘Kinect’ in my Home directory. You don’t need to do this next step but if you plan on following the guide closely, it may come in handy in terms of keeping track of where things go.

>> mkdir ~/Kinect
>> cd ~/Kinect

>> git clone
>> git://github.com/OpenKinect/libfreenect.git

Navigate into the root of the libfreenect directory and create a build directory in order to place the end results of the build:

>> cd ~/Kinect/libfreenect/
>> mkdir build
>> cd build
>> ccmake .

You should now be able to run make and make install

>> sudo make
>> sudo make install

That’s it. change your working directory to /libfreenect/build/bin and run the glview program by invoking the following command:

>> sudo ./glview

For more instructions realting to the installtion of libfreenect on Mac OS X — or Linux and Windows for that matter, check out OpenKinect.org’s Getting Started section.

Originally posted by Jared St. Jean here: http://developkinect.com/resource/mac-os-x/install-libfreenect-drivers-mac-os-x