Saturday, April 9, 2011

Juste pour Moi: Manuel d'Installation d'OpenCV pour CentOS 32-bit 5.x

Log for self.
Worked on CentOS 5.3 and 5.5

64-bit CentOS has problems with g++, so will need to fix that before can even start installing OpenCV. Run it, and it'll give you errors. Google that error and fix it first.

Output log is in <my local drive>/opencv_linuxInstall.txt.



How to set up OpenCV in Linux:


(when the manual says ldconfig, for centos, it's /sbin/ldconfig. ldconfig by itself won't work, command not found. man ldconfig for more info).

gcc:
$ yum install gcc
make sure gcc is installed. most likely it is already


pkg-config:
$ yum install pkgconfig
most likely it is already installed


cmake:
Download cmake from cmake website. Say we download the .sh file.

$ cd <wherever you put the downloaded .sh file>
$ chmod +x cmake-2.8.4-Linux-i386.sh
$ ./cmake-2.8.4-Linux-i386.sh
It'll unzip in the current folder, with a subdirectory (if you answered yes to the second question).

Can just move it to home directory, or anywhere is fine.
that directory will be your directory that cmake is installed in
the cmake executable is in
<unzipped dir>/bin/cmake
so when need to run cmake, just run that
(I have it in ~/cmake-2.8.4-Linux-i386/bin/cmake)


OpenCV:

Unzip anywhere.

Might need to use sudo on some of these. Just run
$ su
ahead of time to log into root, and then run the following.

Go into the directory where you unzipped Opencv-2.2... whatever

cd ~/OpenCV-2.2.0 # the directory containing INSTALL, CMakeLists.txt etc.
mkdir release
cd release
<cmake unzipped dir>/bin/cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..

It'll config and put files in
/home/master/OpenCV-2.2.0/release

It'll say so with a msg at the end:
-- Build files have been written to: /home/master/OpenCV-2.2.0/release



2.
Still in the opencv/release directory.
Run
$ make
or
$ nmake
not sure what nmake is for, but if make doesn't work, try nmake, I guess.

This will build a bunch of .o files.
This will take a while, half an hour or more. Go do something else.

Then run
$ sudo make install
or just
$ make install
if you're already in root user.

Installation DONE!



Testing OpenCV

1.
To test OpenCV, run cmake again with BUILD_EXAMPLES.
(If you run opencv_test without including the BUILD_EXAMPLES=ON flag in the build, it'll say can't find opencv_test.sum. So run this first.)

may need to log into root first, because the installation was done with root, it might not let you write to some files.
$ cd <opencv dir>
$ cd release
$ su
$ <cmake unzipped dir>/bin/cmake -D BUILD_EXAMPLES=ON

It'll say:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/master/OpenCV-2.2.0/release


2.
Copy tests/ data files.
Copy the data files used for testing, else tests will fail with something like:
[c29e21ad0b37f6f1]
calibrate-stereo-c:     FAIL(No test data)
>>>
The file ../../../../tests/cv/testdata//stereo/case1/stereo_calib.txt can not be opened or has invalid content
context: test case = -1, seed = c29e21ad0b37f6f1

Copy images from source directory tests/ dir to bin/ directory
$ su
$ cd <opencv dir>
$ cp -r tests/* release/bin


3.
Copy opencv_extra/ data files.
Still need to copy data files from opencv_extra/testdata/cv/, else some tests will fail with something like:
[9215677204570357]
calibrate-camera-c:     FAIL(No test data)
>>>
Could not open file with list of test files: /../../opencv_extra/testdata/cv/cameracalibration/datafiles.txt
context: test case = -1, seed = 9215677204570357

(Most of the fail cases in this round are associated with opencv_extra/testdata/cv)

It doesn't seem like opencv_extra/testdata/cv came with the standard Opencv download archive. Just download that directory manually from opencv repository.

Install svn if you don't have it yet (I didn't... to the jungle's surprise.)
$ su
$ yum install subversion

cd into whatever dir you want to put the repository files:
$ cd /home/master
$ mkdir OpenCV-repository
$ cd OpenCV-repository
$ svn co https://code.ros.org/svn/opencv/trunk/opencv_extra

(or maybe you can just do trunk/opencv_extra/testdata. seems like that'll cover all of the test files already)

This will check out latest opencv_extra/ from the repository into the current directory. It'll say something like:

Checked out revision 4818.

(Don't check out trunk/opencv/ unless you want to! Just do trunk/opencv_extra/ is sufficient for the tests. If you checked out trunk/opencv/ by accident, just remove it with
$ rm -rf opencv
)

Now we have all the test files in opencv_extra.
Copy it to where the test binaries want it, in /../../:
$ cp -r opencv_extra/ /../../
or simply
$ cp -r opencv_extra/ /
Either way is the same. Parent dir .. from root / is just root / itself.

(It's a bit weird that they want the directory to be in root / dir, I guess they put a slash in the beginning where they didn't mean to.
Anyway, this is the easiest way that works.
Just remove opencv_extra from your root / directory after finish running all the tests.

OR, probably easier, you can go into the shell script and just remove that slash!
$ vim <opencv dir>/release/bin/test_cv.sh

Remove the slash after $srcdir
so it'd be
  ./opencv_test -d $srcdir../../opencv_extra/testdata/cv
instead of
  ./opencv_test -d $srcdir/../../opencv_extra/testdata/cv
or just change it to ./opencv_extra/testdata/cv, if you have opencv_extra in the bin/ directory.

You'd probably also have to change it for the other shell script files, like test_cxcore.sh, etc.
)


4.
Now can run test, there are three .sh files:
$ cd <opencv dir>/release/bin
$ ./test_cv.sh        # this was copied from the tests/ dir in Step 2

All the tests should be passing, no more "FAIL(No test data)".
(All tests passed for me! :D )

(By the way all the tests are alphabetized. So if you want to check something, just find it in the output by first letter, e.g. canny, color-lab, shape-*, etc.)


NOW HERE-------------------------> Failed on z-highgui.
No idea how to fix it. Doesn't say what file source it is, which means it's not missing a file. So probably just unexpected output.
Why? environment variables? What do the ext= lines that it printed out mean?
Maybe i didn't install highgui libraries?
Seems like highgui is in release/modules/highgui and release/lib/libopencv_highgui.so.2.2. So seems like highgui is installed.





If they aren't passing for you... I don't know what to tell you, something went wrong with the installation, or you missed a step, missed an error message and didn't fix it during installation? etc.
Hence this is called Testing OpenCV! That's why we test. If something failed, go fix your installation. Reinstall or whatever.

Error log is in opencv_test.log. That might tell you what error was thrown at the failure.



Run the other two shell script files, all tests should pass:

$ ./test_cxcore.sh
$ ./opencv_test_core
=================================================
Summary: 0 out of 78 tests failed
Running time: 00:08:35
This just runs opencv_test_core with the right directory for opencv_extra.
opencv_test_core actually doesn't use anything in opencv_extra, so if you just run it by itself, it'll also run all 78 tests successfully (that was the case for me at least, although I did have a copy of opencv_extra in current dir, don't know if it looks at that).

(i.e. ./opencv_test_ml)
$ ./test_ml.sh
=================================================
Summary: 0 out of 15 tests failed
Running time: 00:07:04
This just runs opencv_test_ml with the right directory for opencv_extra (you can see this if you just open up test_ml.sh).
So if you can't run opencv_test_ml by itself, no worries, it's probably just missing the test data files, just run it using this shell script.



5.
There are other binaries in the directory that you can run, if suitable. I don't think I need any of them, so it didn't matter if I ran the extra ones.
For example, this failed for me:

$ ./opencv_test_gpu
OpenCV Error: No GPU support (The library is compilled without GPU support) in throw_nogpu, file /home/master/OpenCV-2.2.0/modules/gpu/src/precomp.hpp, line 84
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/master/OpenCV-2.2.0/modules/gpu/src/precomp.hpp:84: error: (-216) The library is compilled without GPU support in function throw_nogpu

Aborted

I don't really care, I probably didn't install OpenCV with GPU support, and I'm not using opencv for any gpu related stuff.

Run these other tests if needed.



6.
After all the tests are done, if you put the opencv_extra in your root directory, can remove it now.
$ rm -rf /opencv_extra




7. Use OpenCV for projects, using pkg-config

http://opencv.willowgarage.com/wiki/CompileOpenCVUsingMacOSX

The opencv.pc is in
/usr/local/lib/pkgconfig

NOT in your release/lib. Important that you get this dir right and export it to the environment variable PKG_CONFIG_PATH. Then everything else follows.

$ cd <your source code dir>

For Bash:
$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
$ export LD_LIBRARY_PATH=/usr

Check it by running these two commands, should have something similar, in your /usr/local/...:

$ pkg-config --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include 

$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann 


To compile, use:
gcc `pkg-config --cflags --libs opencv` -o mytest mytest.c

/// i'm guessing args are same?
or, for cpp programs, use:
g++ `pkg-config --cflags --libs opencv` -o mytest mytest.cpp


When run the program, if you get this error:
./a.out: error while loading shared libraries: libopencv_core.so.2.2: cannot open shared object file: No such file or directory

Then need to add /usr/local/lib to /etc/ld.so.conf.d/opencv.conf:
$ vim /etc/ld.so.conf.d/opencv.conf
It probably doesn't exist yet, so it'll be a new file.
In the first line of the file, insert:

/usr/local/lib

Save the file. Then run:

$ su
$ /sbin/ldconfig

Now it should run!

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home