본문 바로가기

Linux

OpenCV cross compile for arm linux

NXP I.MX6Q에서 사용할 opencv3.0 build

============================================= opencv 3.0 build ============================================

$ mkdir opencv_for_imx6

$ unzip opencv-3.4.0.zip -d opencv_for_imx6

$ unzip opencv_contrib-3.4.0.zip -d opencv_for_imx6

$ cd opencv_for_imx6/opencv-3.4.0

 + 다음의 위치에 아래와 같이 변경한다.

 $ vi platforms/linux/arm-gnueabi.toolchain.cmake

set(GCC_COMPILER_VERSION "" CACHE STRING "GCC Compiler version")

set(GNU_MACHINE "arm-poky-linux-gnueabi" CACHE STRING "GNU compiler triple")


set (CMAKE_SYSTEM_NAME Linux)

set (CMAKE_SYSTEM_PROCESSOR arm)

set (MY_SYSROOT /home/erato/src/projects/I.MX6Q/toolchain/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi CACHE PATH "ARM cross compilation system root")

set (MY_CROSS_DIR /home/erato/src/projects/I.MX6Q/toolchain/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi CACHE PATH "ARM cross compiler path")

set (CMAKE_C_COMPILER ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-gcc)

set (CMAKE_CXX_COMPILER ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-g++)

#set (CMAKE_AR ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-ar)

#set (CMAKE_LINKER ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-ld)

#set (CMAKE_NM ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-nm)

#set (CMAKE_OBJCOPY ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-objcopy)

#set (CMAKE_OBJDUMP ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-objdump)

#set (CMAKE_RANLIB ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-ranlib)

#set (CMAKE_STRIP ${MY_CROSS_DIR}/arm-poky-linux-gnueabi-strip)


set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --sysroot=${MY_SYSROOT} -march=armv7-a -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9" CACHE INTERNAL "" FORCE)

set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} --sysroot=${MY_SYSROOT} -march=armv7-a -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9" CACHE INTERNAL "" FORCE)


include("${CMAKE_CURRENT_LIST_DIR}/arm.toolchain.cmake")

$ mkdir build

$ cd build

$ export PATH=/home/erato/src/projects/I.MX6Q/toolchain/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi:$PATH

$ cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -DOPENCV_ENABLE_NONFREE=True -DENABLE_CXX11=ON -DSOFTFP=ON -DENABLE_NEON=ON -DENABLE_VFPV3=ON -DBUILD_TESTS=OFF -DBUILD_EXAMPLES=OFF -DCMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake ../

$ make

$ make install



만약 아래와 같은 에러가 발생한다면 dash를 bash로 바꾸고 다시 make한다.

/home/erato/src/projects/I.MX6Q/toolchain/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/bin/arm-poky-linux-gnueabi-ar: 1: /home/erato/src/projects/I.MX6Q/toolchain/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa9hf-neon-poky-linux-gnueabi/usr/bin/arm-poky-linux-gnueabi-ar: Syntax error: word unexpected (expecting ")")

make[2]: *** [3rdparty/lib/liblibjasper.a] 오류 2

make[1]: *** [3rdparty/libjasper/CMakeFiles/libjasper.dir/all] 오류 2

$ sudo dpkg-reconfigure dash

+ 메뉴창에서 'no'라고 답변.


[출처] : http://www.ridgesolutions.ie/index.php/2013/05/24/building-cross-compiling-opencv-for-linux-arm/

This article outlines the steps necessary for building OpenCV for a Linux ARM target. OpenCV is an open-source, cross-platform computer vision and machine vision library.

 

I cross compiled OpenCV for a Xilinx Zinq ARM system yesterday by more-or-less following the steps outlined in the above document – I had to change a few things to get the build to work. I am documenting the exact steps that I took in case anybody is having trouble (probably future me!).

 

I started with a fairly clean Ubuntu install on a virtual machine (it already had git installed) , first I installed some build tools, feel free to skip any that you already have:

Then I installed the GNU ARM tool-chain:

This installed version 4.7 of the compilers while the build process calls for version 4.6, to get around this I modified the cmake platform file slightly, see below.

 

Create a build directory, cd into it and get the OpenCV source from github:

 

To fix the version problem I modified the compiler version in the platform file (this may not be the most correct way to do this but it worked for me!):

 

I edited this file:

 

./opencv/platforms/linux/arm-gnueabi.toolchain.cmake

 

I changed the GCC_COMPILER_VERSION variable’s value from 4.6 to 4.7 to match my installed compiler. I had to edit the file as I wasn’t able to override these variables from the command line.

 

Now create a sub-directory called build and cd into it:

And configure the build:

This should complete without errors if you have the compiler that it is looking for installed etc.

 

Now run ‘make’ and ‘make install’:

Make will take a few minutes to run, make install should copy the output files to a subdirectory called ‘install’.

 

All being well you should now have all the include and lib files that you need to build an OpenCV app for your ARM device.

 



다음 명령으로 OpenCV 라이브러리 설정 파일을 포함해서 기존에 설치된 OpenCV 패키지를 삭제하고 진행해야 합니다. 

$ sudo apt-get purge  libopencv* python-opencv

$ sudo apt-get autoremove


To install opencv by building from source:

OpenCV can be used in python as C++ python binding. I am going to explain how to build OpenCV from source and generate python binding.


The steps needed to be taken in order to install OpenCV 3.4.0 are:


Update and Upgrade and Cleanup (Required for error-free installation)

Install Dependencies

Download and Build OpenCV

Update and Upgrade and Cleanup (Required for error-free installation)

$ sudo apt-get update

$ sudo apt-get upgrade

Now that we have the updates ready, we are ready to install dependencies.


Install Dependencies

$ sudo apt-get install build-essential 

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev


The following command is needed to process images:

$sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev


To process videos:

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

$ sudo apt-get install libxvidcore-dev libx264-dev


For GUI:

$ sudo apt-get install libgtk-3-dev


For optimization:

$ sudo apt-get install libatlas-base-dev gfortran pylint


To build OpenCV binding for both python 2 and 3.

$ sudo apt-get install python2.7-dev python3.5-dev


We are now ready to download and build OpenCV on our Ubuntu machine.

Download and Build OpenCV

To Download OpenCV 3.4.0, go to terminal and type:

$ wget https://github.com/opencv/opencv/archive/3.4.0.zip -O opencv-3.4.0.zip


To Download OpenCV Contrib 3.4.0 (Contrib has very useful algorithms which is a must for anyone working on Computer Vision), go to terminal and type:

$ wget https://github.com/opencv/opencv_contrib/archive/3.4.0.zip -O opencv_contrib-3.4.0.zip


We need to unzip to extract the zip files. Install unzip if not installed using the command:

$ sudo apt-get install unzip


Now extract OpenCV and OpenCV Contrib:

$ unzip opencv-3.4.0.zip

$ unzip opencv_contrib-3.4.0.zip


Make a directory named build inside OpenCV-3.4.0:

$ cd  opencv-3.4.0

$ mkdir build

$ cd build

Now we are going to configure cmake:

$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -DOPENCV_ENABLE_NONFREE=True ..

The command will take some time to execute. Wait for few seconds.


If the configuration is done without error, we will use make command to build.

$ make -j4 (where 4 is number of cores)

This command will take a few minutes to complete. On my PC, it took about 20 minutes.


Install in the location /usr/local using command

$ sudo make install

'Linux' 카테고리의 다른 글

라즈베리파이 sysroot 만들기  (0) 2019.03.28
Makefile : Clock skew detected  (0) 2018.08.23
TFTP 설정  (0) 2017.07.20
vim에서 explorer 기능  (0) 2016.07.26
diff 폴더 비교하기  (0) 2015.12.08