Compiling OpenCV for ARM9 platform
Posted on : 31-03-2009 | By : Yuri Vashchenko | In : OpenCV
20
Compiling OpenCV
- Build platform: Windows XP SP3
- Target platform: ARM9 fixed point
Contents
- Download and prepare OpenCV library source code
- Download and prepare compiler
- Create/Modify Makefile
- Compile and link
- Run and Test
- Performance
- Profiling
Download and prepare OpenCV library source code
- 1. Download the latest version of OpenCV (http://sourceforge.net/project/showfiles.php?group_id=22870). As build platform is Windows, select Windows version (opencv-win).
- 2. Install OpenCV (e.g. C:\Program Files\OpenCV).
- 3. Copy the complete installation directory into work location (e.g. D:\BH\OpenCV)
- 4. Based on an OpenCV sample (e.g. D:\BH\OpenCV\samples\c\facedetect.c) create a test application which will run on the Target hardware. Save it in its own directory (e.g. D:\BH\OpenCV\fd\src\facedetect.cpp)
Download and prepare cross-compiler
- 1. Download the latest version of Sourcery G++ GNU Toolchain for ARM Linux (http://www.codesourcery.com/sgpp/lite/arm/portal/subscription3057).
- 2. Install it (e.g. D:\gcc)
- 3. Copy short name executables (gcc.exe, g++.exe, ld.exe, ar.exe, etc.) from D:\gcc\arm-none-linux-gnueabi\bin\ to D:\gcc\bin\
Create/Modify Makefiles.
All paths in all makefiles should be relative to the make directory (D:\BH\OpenCV\_make)
- 1. Use makefile_all_gnu.mak as a basis for the main Makefile. Save is as “Makefile”.
- 1.1. Comment libraries/binaries you are not going to compile/use (like cxcoretest, cvtest, etc.)
- 1.2. Rework “clean” target – remove lines containing *.dll and replace all *.lib with lib*.a; Remove or comment all lines with *.exe and add ..\bin\fd (the name and path of our test application)
- 1.3. Add lines to build application. E.g. @-mkdir ..\_temp\fd$(DR) and @$(MAKE) $(OPT) –directory=../_temp/fd$(DR) –makefile=../../fd/src/makefile.gnu
- 2. Use make_module_gnu.mak as a basis for the module makefile.
- 2.1. You may need to update CXXFLAGS_DBG variable to include necessary flags, like -D”NDEBUG” -O3 -ffast-math -pg -g -fno-bounds-check -c. The “-c” option says compiler to just compile the code (to not link)
- 2.2. All libraries’ makefiles specify BINTYPE=DLL, so update OUTBIN and OUTLIB paths accordingly. For DLL, OUTLIB should generate a library name like ../../lib/lib$(TARGET)$(DBG).a. OUTBIN should be OUTBIN := ../../bin/$(TARGET)$(DBG)
- 2.3. CXX variable should be “arm-none-linux-gnueabi-g++.exe” or just g++.
- 2.4. LINKFLAGS variable should include -Wl,-L../../lib $(LINKFLAGS_DLL)
- 2.5. LIBS variable should include all necessary libraries and should look like “-Wl,-lpthread,-ldl,-lstdc++,$(LIBS)”
- 2.6. AR variable should be equal to “arm-none-linux-gnueabi-ar.exe” or just ar.
- 2.7. LINK variable should be equal to $(CXX).
- 2.8. OUBLIB target $(OUTLIB): $(OBJS) should include @-mkdir ..\..\lib and $(AR) $(ARFLAGS) $@ $^.
- 2.9. OUTBIN target $(OUTBIN): $(OBJS) should include @-mkdir ..\..\bin and $(LINK) $(LINKFLAGS) $^ -g -o $@ $(LIBS).
- 2.10. .o: target should contain command “$(CXX) $(CXXFLAGS) $<“.
- 3. Use a Makefile.gnu from tests\cv\src directory to create Makefile.gnu for fd application. Save it in the fd source code directory (D:\BH\OpenCV\fd\src\)
- 3.1. TARGET should be fd
- 3.2. BINTYPE should be APP
- 3.3. At a minimum, the cxcore and cv includes should be available, so CV_INC and CXCORE_INC variables should be defined (../../cxcore/include and ../../cv/include)
- 3.4. CXXFLAGS should include -D”CVAPI_EXPORTS” and -I”$(CXCORE_INC)” -I”$(CV_INC)” and other include paths as necessary
- 3.5. LIBS should include at least -lcxcore$(DBG),-lcv$(DBG) and other libraries as needed.
- 3.6. After definitions of variables the make module makefile should be included (include ../../_make/make_module_gnu.mak)
Compile and link
- 1. Run Makefile (created on previous section). To do this just type make
- 1.1. Make will first compile all source files (-c compiler option does this) and create corresponding object files at corresponding folders (_temp/cv_Rls, _temp/cxcore_Rls, _temp/fd_Rls, etc.)
- 1.2. After compiling next library (like cxcore or cv, where the corresponding makefile contain BINTYPE := DLL) make will run “ar” command to create static library (libcv.a or libcxcore.a). Static library is just an archive of corresponding object (“*.o”) files. Object files in the static library are not linked, so they may contain errors which may show up later on the linking stage.
- 1.3. When all libraries are created, the application is compiled and linked. Make sure you give the linker all libraries needed to link the application (command should include options like -Wl,-L../../lib,-lcxcore,-lcv). When you specify “-lcxcore” the linker will look for library named libcxcore.a
- 1.4. If everything was right, the bin folder will contain built application fd.
Run and Test
- 1. The only way to run the application is to run it from MMC (Micro SD card). Copy fd to the mmc card then telnet to the device. Or, telnet to the phone and get binary application via ftp. It is recommended to create directory structure for the application like /mmc/mmca1/OpenCV/bin/facedetect (/mmc/mmca1 is a path to the root of MMC card)
- 2. cd to /mmc/mmca1/OpenCV/bin/facedetect and run the application by typing “./fd”.
- 2.1. If everything is correct, the application will display error message about missing cascade classifiers and exit. In this case put xml classifiers from OpenCV installation into the phone (../../data/haarcascades/) and re-run the application.
- 2.2. You may get the “Segmentation Fault” error. Typically, this error is occurred when the application is linked with (or to use) incorrect libraries or incorrect libraries versions (usually dynamically linked).
- 2.3. You may get the following error: “fd: /lib/libpthread.so.0: version ‘GLIBC_2.4’ not found (required by fd)”. It means that the application was linked with the pthread library that is not compatible with the one in the phone Linux installation. To fix this you need to replace corresponding libraries with the ones compatible with the phone. In my case, I had to replace pthread and stdc++ libraries, which caused multiple linker errors and necessity for replacing more libraries. The complete list of libraries I replaced is listed below:
- added gcc\arm-none-linux-gnueabi\libc\lib\ld-2.3.3.so
- replaced gcc\arm-none-linux-gnueabi\libc\lib\ld-2.8.so
- replaced gcc\arm-none-linux-gnueabi\libc\lib\libc-2.8.so
- replaced gcc\arm-none-linux-gnueabi\libc\lib\libc.so.6
- replaced gcc\arm-none-linux-gnueabi\libc\lib\libpthread-2.8.so
- replaced gcc\arm-none-linux-gnueabi\libc\lib\libpthread.so.0
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\crt1.o
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\crti.o
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\crtn.o
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\libc.a
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\libc.so
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\libc_nonshared.a
- added gcc\arm-none-linux-gnueabi\libc\usr\lib\libc_p.a
- added gcc\arm-none-linux-gnueabi\libc\usr\lib\libc_pic.a
- added gcc\arm-none-linux-gnueabi\libc\usr\lib\libc_pic.map
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\libstdc++.a
- added gcc\arm-none-linux-gnueabi\libc\usr\lib\libstdc++.la
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\libstdc++.so
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\libstdc++.so.6
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\libstdc++.so.6.0.10
- added gcc\arm-none-linux-gnueabi\libc\usr\lib\libstdc++.so.6.0.3
Performance
- 1. Only face detection time is displayed. In reality, application takes more time needed to load classifiers, load input file and save resulted output file. All these times are not counted.
- 1.1. It took on average about 6.4 seconds to process file sqa.jpg with resolution of 314×209 pixels.
- 1.2. It took on average about 41 seconds to process file pic.jpg with resolution of 640×476 pixels.
- 2. The application under performance test was compiled with maximum optimization level (-O3) and with the following options (-ffast-math -fno-bounds-check)
Profiling
- 1. To build application with enabled profiling give the “-pg” option to the compiler and linker, and re-build the whole project.
- 2. To make it compile, I had to replace the following libraries with ones compatible with the phone.
- replaced gcc\arm-none-linux-gnueabi\libc\usr\lib\gcrt1.o
- 3. Run application with profiling support on the phone. After completion, the gmon.out file should be created. Copy this file from the phone to the PC.
- 4. Run the gprof.exe with binary (fd) and gmon output (gmon.out) as command line arguments. Redirect standard output to a text file. This file will contain resulted profiling data, including timing (which function took how much time) and call graph.
- 5. In my case, the result was not quite correct – it shown total execution time of about 14 seconds while real execution time was over 45 seconds (about 41 seconds on face detection and other time on loading haar cascades, loading input file and saving output file).
hi!
please can some one answer me this question
can I use opencv library wilth a microblaze or niosII.?
the algorithm run in windowsXP
if yes which fpga should I have?
best regards
Hentati,
Personally, I have not tried to compile and use the OpenCV library for MicroBlaze or Nios II. Nevertheless, OpenCV library is written to be cross-platform and highly portable. So, if you have a cross compiler for your platform with corresponding platform-specific libraries chances are that OpenCV and your algorithm will compile and work. You may face some problems with Highgui part of the OpenCV library (graphical output, jpeg decoding, etc.), but this is another question.
If your are running Linux on your target platform, I recommend you to use a gcc compiler (such as one included into the CodeSorcery G++ Toolchain) it is available.
As for a specific FPGA – I am not sure I understand your question. Do you need to choose a target board for your application or you have a specific FPGA and you want to know if it will help you running OpenCV algorithms? Choosing a FPGA mostly depends on what algorithm are you going to run on it. Depending on your application, you may want to run a FPGA for a fast fourier transform or convolution on FPGA to reduce the load of your CPU and improve the overall performance.
Any chance you will just post up the files needed already followed through instructions. I can’t get past, “Create/Modify Makefiles”. I really need this for my project.
Hi Bryce,
Unfortunately, I cannot give you any source code files (including makefiles) as our management restricts it. If you really need any source code please contact our sales/marketing team (consult contacts page for more info).
Anyway, I can probably help you with an advice if you give more details on your project and problems you are facing. How many source code files do you have? Do all your source files compile or you are getting some compiler errors?
I’m making a robot that will recognize objects like a ball, cup, ect. I have the basic application down, but I need to actually need to test it on the interface to see if it works right. The basic application is it gets image from the camera, loads it into its RAM, analyzing it to find those objects, sends signals to the Arduino for where the servo’s to move.
I have the basic “seeing” application source now.
is it possible to compile the OpenCV code for Palm mobile device which uses ARM?
Hello, Santosh,
We did not compile OpenCV library for Palm. As for Mobile devices platform, we ported OpenCV on Android and Windows Mobile.
To compile OpenCV for Palm, you need:
1. Cross-compiler that can genrate binaries for your target Palm OS.
2. Standard libraries (like libc) compiled for your target platform.
3. Libraries openCV needs to work ( depending on your application may be GTK, FFMpeg, Libjpeg, etc).
Hope this helps,
Best Regards,
Yuri
I am working on optimizing and compiling components of OpenCV to ARM Cortex A8. It is very portable in that it will run on many platforms, but it is only set up to run *well* on Intel x86. ARMs and most other DSPs do not like double precision numbers, for one thing, but with OpenCV we are stuck with double. The other problem is that OpenCV is difficult to analyze with call graph tools, so it is difficult to extract just the components you need. I have had no luck with such tools.
Hi JPN,
We did not use any tools to change doubles to fixed point operation.
We rewrote the big part of opencv manually.
Unfortunately, it had not given significant improvement in performance on our target ARM platform.
So, if you need fixed point operations for your DSP of whatever your target platform is, you will probably have to rewrite a part of opencv code yourself.
Start with running a profiler on your target platform to understand which code takes most of the execution time.
Then, using opencv source code, try to understand how it works and start replacing double precision variables and operations with fixed point ones. In our experiments, we even had been able to rewrite some code to use 32 bit integers only. It affected precision in some cases, but until overall quality is acceptable, you can do such trade-offs.
Hope this helps,
Best regards,
Yuri
Hi
I am trying to cross-compile a armtest.c file using arm-linux-gcc ,
toolchain 4.3.2. The .c file is using open cv2.0.0 libraries. when I try to
cross compile the file, the following error is coming
root@tanushri-desktop:~# arm-linux-gcc -o2 -I/usr/local/include/
opencv armtest.c -L//root/Downloads/OpenCV-2.0.0/src/.libs -lcv –
lhighgui -lcxcore -lml -lcvaux -lrt -lpthread -ldl -lz -lpng12 -ljpeg –
o armtest
cc1: warning: include location “/usr/local/include/opencv” is unsafe
for cross-compilation
In file included from /usr/local/include/opencv/cxcore.h:70,
from /usr/local/include/opencv/cv.h:58,
from armtest.c:12:
/usr/local/include/opencv/cxtypes.h: In function ‘cvRound’:
/usr/local/include/opencv/cxtypes.h:240: warning: incompatible
implicit declaration of built-in function ‘lrint’
/opt/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/
4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lcv
HELP!
Hello Tan,
ld: cannot find -lcv means that your linker cannot find library libcv.a. Do you have this file built? What you probably need to do is to check if this library was actually created and if your path where linker looks for this lib contains a correct directory. See section “Compile and link, 1.3” above.
Hope this helps,
Best regards,
Yuri Vashchenko
actually i had given a wrong path…now the problem is solved.
Thanks
Hello,
I’m a beginner in embedded and I would like to ask you for support.
I’m trying to cross compile a test file using Sourcery_G++_Lite_for_ARM_GNU_Linux and the cross compilation fails in the following way:
ric@ubuntu:~/CodeSourcery/gnueabi/samples$ arm-none-linux-gnueabi-gcc -I/usr/local/include/opencv -L/usr/local/lib -lcv -lhighgui -lcxcore -lcvaux contours.c -o contours
cc1: warning: include location “/usr/local/include/opencv” is unsafe for cross-compilation
/home/ric/CodeSourcery/gnueabi/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/bin/ld: warning: library search path “/usr/local/lib” is unsafe for cross-compilation
/home/ric/CodeSourcery/gnueabi/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/bin/ld: skipping incompatible /usr/local/lib/libcv.so when searching for -lcv
/home/ric/CodeSourcery/gnueabi/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lcv
collect2: ld returned 1 exit status
The host is an ubuntu_10.04.1 VM on a Windows XP Laptop, target will be an ARM based IGEPV2 board.
CV libs seems to be correctly installed since I can compile using gcc in the host and execute and execute on it.
About Cross compilation tool, after installing there were created two folders :
/usr/local/lib :
lrwxrwxrwx 1 root root 15 2011-01-03 02:37 libcvaux.so -> libcvaux.so.2.0
lrwxrwxrwx 1 root root 17 2011-01-03 02:37 libcvaux.so.2.0 -> libcvaux.so.2.0.0
-rw-r–r– 1 root root 1162062 2011-01-03 02:34 libcvaux.so.2.0.0
lrwxrwxrwx 1 root root 12 2011-01-03 02:37 libcv.so -> libcv.so.2.0
lrwxrwxrwx 1 root root 14 2011-01-03 02:37 libcv.so.2.0 -> libcv.so.2.0.0
-rw-r–r– 1 root root 2057645 2011-01-03 02:33 libcv.so.2.0.0
lrwxrwxrwx 1 root root 16 2011-01-03 02:37 libcxcore.so -> libcxcore.so.2.0
lrwxrwxrwx 1 root root 18 2011-01-03 02:37 libcxcore.so.2.0 -> libcxcore.so.2.0.0
-rw-r–r– 1 root root 2711305 2011-01-03 02:31 libcxcore.so.2.0.0
lrwxrwxrwx 1 root root 17 2011-01-03 02:37 libhighgui.so -> libhighgui.so.2.0
lrwxrwxrwx 1 root root 19 2011-01-03 02:37 libhighgui.so.2.0 -> libhighgui.so.2.0.0
-rw-r–r– 1 root root 247528 2011-01-03 02:33 libhighgui.so.2.0.0
lrwxrwxrwx 1 root root 12 2011-01-03 02:37 libml.so -> libml.so.2.0
lrwxrwxrwx 1 root root 14 2011-01-03 02:37 libml.so.2.0 -> libml.so.2.0.0
-rw-r–r– 1 root root 446543 2011-01-03 02:33 libml.so.2.0.0
drwxr-xr-x 2 root root 4096 2011-01-03 02:37 pkgconfig
drwxrwsr-x 4 root staff 4096 2010-08-16 02:35 python2.6
and /usr/local/include/opencv :
-rw-r–r– 1 root root 63883 2010-12-31 08:28 cvaux.h
-rw-r–r– 1 root root 51258 2010-12-31 08:28 cvaux.hpp
-rw-r–r– 1 root root 40678 2010-12-31 08:28 cvcompat.h
-rw-r–r– 1 root root 71716 2010-12-31 08:28 cv.h
-rw-r–r– 1 root root 43897 2010-12-31 08:28 cv.hpp
-rw-r–r– 1 root root 12503 2010-12-31 08:28 cvtypes.h
-rw-r–r– 1 root root 2538 2010-12-31 08:28 cvver.h
-rw-r–r– 1 root root 41990 2010-12-31 08:28 cvvidsurv.hpp
-rw-r–r– 1 root root 20730 2010-12-31 08:28 cvwimage.h
-rw-r–r– 1 root root 87498 2010-12-31 08:28 cxcore.h
-rw-r–r– 1 root root 81321 2010-12-31 08:28 cxcore.hpp
-rw-r–r– 1 root root 8757 2010-12-31 08:28 cxerror.h
-rw-r–r– 1 root root 6937 2010-12-31 08:28 cxflann.h
-rw-r–r– 1 root root 152510 2010-12-31 08:28 cxmat.hpp
-rw-r–r– 1 root root 27967 2010-12-31 08:28 cxmisc.h
-rw-r–r– 1 root root 86570 2010-12-31 08:28 cxoperations.hpp
-rw-r–r– 1 root root 53713 2010-12-31 08:28 cxtypes.h
-rw-r–r– 1 root root 18165 2010-12-31 08:28 highgui.h
-rw-r–r– 1 root root 4652 2010-12-31 08:28 highgui.hpp
-rw-r–r– 1 root root 72650 2010-12-31 08:28 ml.h
Does anybody knows the cause of that problem?
Thanks in advance!
Hello,
As far as I understand, the problem is with your OpenCv library. It looks like you are trying to compile a sample for ARM using libraries compiled for x86. Your directive “-L/usr/local/lib” tells the linker to look for libraries at “-L/usr/local/lib”, where probably Ubuntu (x86) libraries are stored. You need to recompile the whole OpenCV for your target platform.
Hope this helps,
Best Regards,
Yuri Vashchenko
Hi,
I am using OpenCV libraries as part of my masters thesis work that’s related to Computer Vision. I used to work in Windows 7 and everything was fine. But of late, I started using Ubuntu 10.10 64-bit. Compilation of my source files in Ubuntu that were earlier written on Windows platform is going fine. But when I run the executables produced with the g++ compiler, they are producing the “Segmentation Fault” error. Could you tell me where I was going wrong? The following piece of information may be of some help.
‘g++ –version’ produced the following output:
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Appreciate any help in this regard as this is very critical for my thesis. If it doesn’t work, I’ll have to return to Windows which I don’t really like.
Thanks,
Ramaraju
Hi Ramaraju,
Segmentation fault may be caused by many different issues.
You provided too little information on you problem, so I cannot give you any specific solution. As this thread is related to compiling the OpenCV library, I’d like you to make sure you have correctly compiled OpenCV library itself.
Start with running OpenCV’s “configure” script, then “make”, then “install” the Library. If you get any errors during this process, you need to solve them first.
Then, write a very basic (just a few lines) application that uses OpenCV library. You may use any of the OpenCV’s examples found in distribution package. Compile it and execute. If you made everything right, it should run without any issues. When you verified that OpenCV itself is built and installed correctly, you may start to debug more complex applications.
Hope this helps,
Best Regards,
Yuri Vashchenko
I have to compile openCV for armv7 , i have g++-arm-linux-gnueabi 4.3 installed directly from synaptic package manager in ubuntu, how do i cross compile openCV for target?
Hello Amit,
First, you need to make sure you have the cross-compiler working. Try to create very simple “Hello world” application and compile it for your ARM7. This simple app should use only standard c library. When you successfully compiled, linked and executed your simple app on your target ARM hardware, use the guide in post to cross-compile the opencv library.
Best Regards,
Yuri Vashchenko
Hello sir,I am doing a project that involves hand gesture recognition using neural networks and controlling a robot using serial communication to an arduino based on gesture.Hence in addition to opencv I need to use openframeworks for serial communication.Is it possible to embed the whole thing inside a chip???Specifically can I use PSOC with ARM dsp for this application??If so please explain me the entire process of embedding an application.For instance we use usb port to attach a camera but how do we do that in a chip.Or it would be helpful if you could provide me a link to a complete novice…thank u…
My project involves hand gesture recognition using opencv and using openframeworks to do serial communication with arduino and manipulating a robot based on gesture…I am complete novice to this embedding vision code in uP…..Is it possible to do this in PSOC with ARM….For example we use usb to get image from the camera how can we implement this in a chip??If u could provide a link to a detailed tutorial regarding embedding vision software it would be useful…..