Monday, July 23, 2007

My Experience with OpenCV 1.0 + Ubuntu 6.10 - UPDATED

With the help of my awesome brother, I figured out what causes the segmentation fault.

Apparently (as I feared), my whole OpenCV installation is messed up. My recent installation of OpenCV 0.9.7 got mixed up with my earlier OpenCV 1.0. So I had to remove my 0.9.7 (using the Package Manager -- since I installed it using the Package Manager) and uninstall OpenCV 1.0 using make uninstall from the directory where I extracted the OpenCV-1.0.0.tar.gz. Now that I had it cleaned out, this time I re-installed OpenCV 1.0.

To be safe, I used the steps described (once again) in the OpenCV wiki but this time I put the install directory in my home/*myusername* directory -- my profile directory.

./configure --prefix=/home/*myusername*/opencv/1.0.0/
make
make install

In addition to that, i set the LD_LIBRARY_PATH with the directory where my OpenCV library files are located, in this case: /home/*myusername*/opencv/1.0.0/lib

export LD_LIBRARY_PATH=/home/*myusername*/opencv/1.0.0/lib

I didn't have anything in my LD_LIBRARY_PATH before that, so the above is enough. But if I already have something, I want to make sure I don't screw up the existing paths, so I have to include the current paths by adding :${LD_LIBRARY_PATH} at the end. The colon ":" tells bash that whatever comes after it is another path; in this case, is whatever is inside the current LD_LIBRARY_PATH -- that's why I need to use "$" and the curly brackets to indicate the variable name "LD_LIBRARY_PATH".

export LD_LIBRARY_PATH=/home/*yourusername*/opencv/1.0.0/lib:${LD_LIBRARY_PATH}

Now, compile my loadimage.cpp program using the same g++ command as before:

g++ loadimage.cpp -o loadimage -I /home/*myusername*/opencv/1.0.0/include/opencv/ -L /home/*myusername*/opencv/1.0.0/lib -lhighgui -lcv -lml

Yay! It works! The picture showed up, and there was no segmentation fault. *phew* My head was already about to explode trying to figure out how to make this work.

Then, trying the inversion tutorial, and the facedetect (included in OpenCV installation) everything works! Yay! Now I can go on and play around some more. Now I just need to figure out how to use this to detect poses...

Friday, July 20, 2007

My Experience with Ubuntu 6.10 + OpenCV 1.0

FINALLY!!! I'm able to use the OpenCV library on my Ubuntu 6.10!

I've been trying to use the OpenCV 1.0 but apparently (for some reason) it does not work well with my PC. Bear in mind that there could be several reasons, i.e. I didn't set up my Ubuntu, C/C++ compiler, OpenCV correctly (although I already followed the instructions in the OpenCV Wiki... or at least I think I did.) According to the Wiki, to install OpenCV 1.0 I would need to have;
  • GTK+ 2.x
  • pkgconfig
  • libpngzlib ... etc (Please refer to the Wiki if you want to know the complete list)
and so... I did all the steps in the Wiki (including getting all those libraries in the list -- I used the package manager to get them, and the same install path/configurations), up to the point where it says: "If the installation is successful, you get below message after 'make install' command is finished.
----------------------------------------------------------------------
Libraries have been installed in:
/opencv_library_install_path/opencv-1.0.0/lib

... which I never did. So I wondered if it got installed correctly or not. The Wiki tells to do "make check" at the terminal to "... check whether the library is installed properly ..." But when it was finished, it didn't give any confirmation like "Yes, OpenCV is installed properly on your machine" or "No, your machine sucks" or something.

Regardless, I think it's installed so I tried a simple program to just load an image:

// loadimage.cpp
// load an image and display it on a window using OpenCV library

#include <stdlib.h>
#include <stdio.h>
#include <cv.h> // an OpenCV header file
#include <highgui.h> // another OpenCV header file

int main() {
IplImage* img = 0;
// img : variable for the image
// IplImage is a data type for images in OpenCV

img = cvLoadImage(“photo.jpg”, 1);
// load image “photo.jpg”

cvNamedWindow(“mainWin”, CV_WINDOW_AUTOSIZE);
// create a window named “mainWin” to display the loaded image – set on autosize so it matches the dimensions of the image

cvMoveWindow(“mainWin”, 100, 100);
// place the window in the coordinates (x: 100, y: 100) -- the origin (0, 0) is the top left corner of the screen

cvShowImage(“mainWin”, img);
// display the loaded image (photo.jpg) in the created window (mainWin)

cvWaitKey(0);
// do nothing/wait until a key is pressed – then continue to the next line

if (img) printf(“success!”);
// if the image is successfully loaded, print “success!” on the screen

cvReleaseImage(&img);
// clear the memory that was used to load the image
}
// end of program



I tried compiling my program using command such as:

g++ loadimage.cpp -o loadimage -I /usr/include/opencv -L /usr/lib -lhighgui -lcv -lml

..because the .h files are in the /usr/include/opencv folder, and the library files (libhighgui.*, libcv.*, libml.*, and libcvaux.*) are in the /usr/lib folder (I've checked). No compilation errors. All good...? Not quite.

When I run it using:

./loadimage

This is what I got:

OpenCV ERROR: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support)
in function cvNamedWindow, window.cpp(71)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)

Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)

What the...?? When i removed the cvNamedWindow, cvMoveWindow, cvShowImage, and cvWaitKey, it worked however -- no compilation error, and it displayed "success!" So what's wrong??

Searching through the web, I found several people had very similar problems... however, there's a slight difference (and I don't know how significant this is) that instead of saying:

in function cvNamedWindow, window.cpp(71)

they had:


in function cvNamedWindow, window_lnx.cpp(822)

One of the replies said they were able to solve the problem by installing some other libraries such as: libwxgtk2.4 and libgtk2.0-dev as was said here (but he used Knoppix 4.0.2) -- so I tried installing those (again from the package manager), to no avail.

So I asked my friend who is more familiar with Linux/UNIX environment (I'm still a newbie at Linux)... he recommended that I look into the environment variables and make sure that the directories where the OpenCV library and header files are listed in the PATH. I checked it using:

echo $PATH

Well, it's not there. So I added to the path:

/usr/include/opencv:/usr/lib:/opencv_library_install_path/opencv/lib

to my PATH; I modified the etc/environment file using gedit a
s root:

sudo su (to get into root)
(in etc folder): gedit environment

and add my path there and saved it. I'm curious though, most people said their library is in /usr/local/include/opencv and /usr/local/lib ... peculiar... is it Ubuntu specific? Or something's wrong with my configuration?

Nope, that didn't work -- I still get the same thing: no compilation error, and the same error during execution.

So i tried the next thing mentioned in the Wiki, when compiling the program, set the PKG_CONFIG_PATH variable. Here's the guide.
Nope, still didn't help.

I couldn't find many resources to solve the problem, so finally I tried searching for OpenCV through the package manager -- I found the OpenCV version
0.9.7
for Ubuntu. Being a bit disappointed that I can't get the 1.0, I tried it anyway and the package manager automatically installed it for me. The library and header files are located in: /usr/lib and /usr/include/opencv, respectively but with suffixes 0.9.7 on the file names; so, for example: libhighgui.so is libhighgui0.9.7.so and so on.

And so, now I compile my program using the following:

g++ loadimage.cpp -o loadimage -I /usr/include/opencv/ -L /usr/lib -lcv0.9.7 -lhighgui0.9.7 -lml

..no compilation error. OK... so let's run it

./loadimage

Lo and behold! There's my image, showing up in all its glory!!! It finally worked! I'm so moved that I decided to write this entry and share my excruciating experience with you.

So, what can we get from this experience? Does this mean OpenCV 1.0 does not work with Ubuntu 6.10 yet, and only 0.9.7 works so far? Or, I messed up the installation process and my configurations... If somebody could point that out, I would be so very grateful. It's been a puzzling pain this past few weeks.

However, I don't think it's 100% working well... I still get

Segmentation fault (core dumped)

..after I hit Enter after the image loaded. Not sure what causes this yet.
I guess not many people encountered this problem, so I really would like to know what causes this. I refer to the Wiki a lot and said it didn't work, but don't think I'm blaming it or saying that it is useless ... I refer to it because I think that is the most standard guide for using OpenCV, it should work with properly configured machines -- it just turns out that it didn't work for me and my machine.

I tried some other things under Eclipse as well (setting include & library paths)... but didn't work. But I haven't tried it again after it worked with 0.9.7.

Sorry if the formatting is messed up. I still have yet to figure out how this blogging tool works.