A nerd blog about cs

Month: November, 2012

Segmenting image.

2. Separating the image from the background

This parts consists of identifying the segment of the part of the image that corresponds to the object of interest. For this part I had to download and compile code found at


For porting the code to my computer I had to modify some headers and change the settings for the compiler.

First I had to install gfortran, which can be found on the following website:


But because the gfortran is linked to a newer version of the c++ compiler, I had to follow delete Xcode, install g++ and reinstall Xcode. I found the answer for my problem in the following website:


I learned a lesson: I should follow other’s people advices if I’m not moving on.

Then, after being able to compile the code with gfortran I had to modify the compiler used for c++, instead of using c++, I started using g++.

Even tough I didn’t use some of the knowledge that I’ve learned in the process of solving this problem. I think it’s interesting to remember some of the material.

How the Makefile works:


And a header is actually included inside a cpp file, i.e. the file is inserted at the beginning of the file.

Another useful information is the -i and -l flags.

The -l flag is used to add libraries when compiling and the -i flag is to add pathnames when including headers from other directories:

-l library
Search the library named library when linking. (The second
alternative with the library as a separate argument is only for
POSIX compliance and is not recommended.)

It makes a difference where in the command you write this option;
the linker searches and processes libraries and object files in the
order they are specified. Thus, foo.o -lz bar.o searches library z
after file foo.o but before bar.o. If bar.o refers to functions in
z, those functions may not be loaded.

The linker searches a standard list of directories for the library,
which is actually a file named liblibrary.a. The linker then uses
this file as if it had been specified precisely by name.

The directories searched include several standard system
directories plus any that you specify with -L.

Normally the files found this way are library files—archive files
whose members are object files. The linker handles an archive file
by scanning through it for members which define symbols that have
so far been referenced but not defined. But if the file that is
found is an ordinary object file, it is linked in the usual
fashion. The only difference between using an -l option and
specifying a file name is that -l surrounds library with lib and .a
and searches several directories.

For the -Ldir of the file we have:

Add directory dir to the list of directories to be searched for -l.

The -I flag:

-I dir
Add the directory dir to the list of directories to be searched
for header files. Directories named by -I are searched before the
standard system include directories. If the directory dir is a
standard system include directory, the option is ignored to ensure
that the default search order for system directories and the
special treatment of system headers are not defeated .

Then I encountered a new problem: The c++ files didn’t contain the definition of types used at the code. And all the headers that were included in the file didn’t contain the definition of the structure being used.

To solve this problem I looked for similar files that used the same structure and tried to determine what headers did these files use and I discover that one of these headers contained in the file using the same structure had the definition as it should.

Lesson: Try to identify similar code that does the same thing that you are trying to do. It gives a lot of insight.

After I compiled the code I run to a new problem, I tried to run the command:

./segment -image image.jpg -segfile image.seg -numsuperpixels 8

But I ran into the problem:

entering segment_main
reading image giu.jpg
Wrong JPEG library version: library is 80, caller expects 62

After googling the possible problem I found out that (http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=21131):

This error occurs when one of the delegate libraries include an older version of JPEG than what is currently on your system.

Therefore to solve this problem I need to install a older version of the jpeg library and link the code to the older version of the jpeg library.


Image Recognition

Imagine if computers could create 3D models from a single image. That’s exactly what humans do everyday all the time. How do we do it? What are the processes that we use to understand the world?

Feynman explains the physical process behind the process:

How can we extract the useful information from the sea of information out there?

1. Identify which parts of the image contain the image of the person.

2. Separate the figure of the person and the background.

3. Create a shape model of the person.

With the information of the shape of the person we can detect the
possible pose that the person.

1 – Identify parts of the image containing the object.

To identify the possible parts of the image that contain the object I will use feature recognition.

A feature that is widely used nowadays is HOG (histogram of gradients). An histogram of gradients is a record of all the possible angles of gradients of a certain part of the figure (a feature).

With many images of a certain object there is enough of a sample for determining how many times an angle of a gradient may appear in a figure.

When a new image is shown, then to determine if the object is present. For each set of pixels we determine the angles of the feature and compare it with the saved histogram of gradients. If the similarity passes a certain threshold, then the part of the image is considered to be the object of interest.

To achieve the goal of identifying where the parts of the image that may contain the object of interest I used the software and algorithm Pedro Felzenszwalb. His software can be found in the following website:



The figure above shows the possible use of running the code of Pedro trying to identify a bicycle.