How to build OpenCV on ARM/Linux/Windows (with FFmpeg support) ? This is a brief introduction.

Prerequisites

  1. OpenCV source
  2. FFmpeg source
  3. Xilinx tools
  4. CMake


Install OpenCV on ARM

Step 1 Build FFmpeg (cross-compile)


In the FFmpeg source directory:
$ ./configure --enable-shared --disable-static --cross-prefix=arm-xilinx-linux-gnueabi- --arch=armv7l --target-os=linux --prefix=<local path>
$ make
$ make install
After FFmpeg installation, add library path and header file path to environment to make it visible:
$ export LD_LIBRARY_PATH=<local path>/lib:${LD_LIBRARY_PATH}
$ export C_INCLUDE_PATH=<local path>/include:${C_INCLUDE_PATH}
$ export CPLUS_INCLUDE_PATH=<local path>/include:${CPLUS_INCLUDE_PATH}

Step 2 Edit a toolchain file


Open a file (e.g. named toolchain.make) and edit it:
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm )
set( CMAKE_C_COMPILER arm-xilinx-linux-gnueabi-gcc )
set( CMAKE_CXX_COMPILER arm-xilinx-linux-gnueabi-g++ )
set( CMAKE_INSTALL_PREFIX <local path> )
set( CMAKE_FIND_ROOT_PATH <local path> )

Step 3 Generate makefile

$ mkdir release
$ cd release
$ cmake -D CMAKE_TOOLCHAIN_FILE=toolchain.make -D BUILD_opencv_nonfree=OFF <path to opencv source directory>
Check the configuration information of OpenCV to confirm the FFmpeg library is detected (for example):
...
--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  NO
--     FFMPEG:                      YES
--       codec:                     YES (ver 55.32.0)
--       format:                    YES (ver 55.10.2)
--       util:                      YES (ver 53.2.0)
--       swscale:                   YES (ver 2.1.2)
--       gentoo-style:              YES
...

Step 4 Customize build options

$ ccmake .
You might need to check if you need other cross-compiled libraries support.
For the least dependency, make sure that the following items are OFF. If it is ON, you can toggle it by scrolling to the option and hitting Enter.
  • WITH_1394
  • WITH_CUDA
  • WITH_CUFFT
  • WITH_EIGEN
  • WITH_GSTREAMER
  • WITH_GTK
  • WITH_JASPER
  • WITH_JPEG
  • WITH_OPENEXR
  • WITH_PNG
  • WITH_PVAPI
  • WITH_QT
  • WITH_TBB
  • WITH_TIFF
  • WITH_UNICAP
  • WITH_V4L
  • WITH_XINE

Hit 'c' then 'g' to generate new Makefile.
Or, you may edit CMakeCache.txt to modify build options.
Note: make sure WITH_FFMPEG=ON in CMakeCache.txt to enable FFmpeg support. Here is an example of my CMakeCache.txt.

Step 5 Build (cross-compile)

$ make
$ make install

Step 6 Test



Here is an simple example of an OpenCV application on ARM. It reads the first frame of a video file (h264 encoded) and save it to a bitmap file.
Test it on ARM with:
> export LD_LIBRARY_PATH=<path to your ARM libraries>:{LD_LIBRARY_PATH}
> ./test.elf
source code of test.cpp:
#include <stdio.h>
#include <opencv2/opencv.hpp>
 
using namespace cv;
 
int main( int argc, char** argv ) {
 
    VideoCapture cap("input_video");
    if (!cap.isOpened()) {
        printf("error: failed to open the video file.\n");
        return -1;
    }
 
    double fps = cap.get(CV_CAP_PROP_FPS);
    printf("Frame per seconds: %f\n", fps);
 
    Mat frame;
    bool bSuccess = cap.read(frame);
 
    if (!bSuccess) {
        printf("error: failed to read the frame from the video file.\n");
        return -1;
    }
    imwrite("first_frame.bmp", frame);
    printf("First frame saved.\n");
 
    return 0;
}


Install OpenCV on Linux


Step 1 Build FFmpeg


In the FFmpeg source directory:
$ ./configure --enable-shared --disable-static --prefix=<local path>
$ make
$ make install
After FFmpeg installation, add library path and header file path to environment to make it visible:
$ export LD_LIBRARY_PATH=<local path>/lib:${LD_LIBRARY_PATH}
$ export C_INCLUDE_PATH=<local path>/include:${C_INCLUDE_PATH}
$ export CPLUS_INCLUDE_PATH=<local path>/include:${CPLUS_INCLUDE_PATH}

Step 2 Generate makefile

$ mkdir release
$ cd release
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_opencv_nonfree=OFF -D CMAKE_INSTALL_PREFIX=<local path> <path to opencv source directory>
Check the configuration information of OpenCV to confirm the FFmpeg library is detected (for example):
...
--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  NO
--     FFMPEG:                      YES
--       codec:                     YES (ver 55.32.0)
--       format:                    YES (ver 55.10.2)
--       util:                      YES (ver 53.2.0)
--       swscale:                   YES (ver 2.1.2)
--       gentoo-style:              YES
...

Step 3 Customize build options


You might want to edit CMakeCache.txt to modify build options.
Note: make sure WITH_FFMPEG=ON in CMakeCache.txt to enable FFmpeg support. Here is an example of my CMakeCache.txt.

Step 4 Build

$ make
$ make install

Step 5 Test


Here is an simple example of an OpenCV application on Linux. It reads the first frame of a video file (h264 encoded) and save it to a bitmap file.
Test it on Linux with executing "test". (Note: make sure OpenCV libraries can be found in $LD_LIBRARY_PATH)
source code of test.cpp is the same as the one in ARM part.


Install OpenCV on Windows



Step 1 Build FFmpeg


Follow the instructions in <OpenCV_source>\3rdparty\ffmpeg\readme.txt to build FFmpeg on windows.
Note: 32-bit MSYS can be found at C:\Xilinx\Vivado_HLS\201x.x\msys, launch msys.bat there is ok to do the job.
At last, the newly built opencv_ffmpeg.dll (32-bit) and/or opencv_ffmpeg_64.dll (64-bit) can be found in <OpenCV_source>\3rdparty\ffmpeg

Step 2 Generate makefile


Start -> Xilinx Design Tools -> Vivado HLS command prompt (this runs a msys shell)
$ cd <OpenCV_source>
$ mkdir release
$ cd release
$ cmake -G "MSYS Makefiles" <path to opencv source directory>
Check the configuration information of OpenCV to confirm the FFmpeg library is detected (for example):
...
--   Video I/O:
--     Video for Windows:           YES
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  NO
--     FFMPEG:                      YES (prebuilt binaries)
--       codec:                     YES (ver 55.18.102)
--       format:                    YES (ver 55.12.100)
--       util:                      YES (ver 52.38.100)
--       swscale:                   YES (ver 2.3.100)
--       gentoo-style:              YES
...

Step 3 Customize build options


You might want to edit CMakeCache.txt to modify build options.
Note: make sure WITH_FFMPEG=ON in CMakeCache.txt to enable FFmpeg support. Here is an example of my CMakeCache.txt.


Step 4 Build

$ make
$ make install

Step 5 Test


Here is an simple example of an OpenCV application on Windows. It reads the first frame of a video file (h264 encoded) and save it to a bitmap file.
Test it on Windows with executing "test.exe" in Vivado HLS command prompt.
source code of test.cpp is the same as the one in ARM part.