How-to build opencv libraries for SFOS
asked 2016-11-23 09:59:58 +0200
This post is a wiki. Anyone with karma >75 is welcome to improve it.
How-to build and install opencv libraries for SFOS
This how-to shows a way how the opencv libraries can be build and used for a SFOS app.
1. Connection to build engine
The most comfortable way is to compile the library on the installed emulator that is delivered by the SFOS SDK, i.e. the Qt Creator. Hence, first of all we need to login into the build engine by means of a ssh connection. For that, consider either the very well over all SFOS documentation "Developing with SailfishOS" (chp. 5.2.3 SSH login) respectively the answer of the FAQ "How do I login into the emulator or build engine?". In a Windows environment you can use a terminal program such as PuTTY or TeraTerm for a connection to the build engine by using the followig connection settings:
- Host: mersdk@localhost
- TCP port: 2222
- SSH version: SSH2
- Key (RSA): C:/ ... /SailfishOS/vmshare/ssh/private_keys/engine/mersdk
Note: In order to login into the build engine, it must be running (either started within the SDK or by the VM VirtualBox Manager). * On Linux* you can login via SSH with the following command:
ssh -i /opt/SailfishOS/vmshare/ssh/private_keys/engine/mersdk mersdk@localhost -p 2222
2. Download opencv sources
Before you start downloading and compiling the opencv libraries, ensure that the git and cmake tools are installed on the build engine. You can check it and necessarily install the packages by means of the package manager zypper (see "Developing with SailfishOS" in chp. 5.2.1 zypper or the answer of the together contribution "Porting SDL 2.0 game to Sailfish"). But for an update of the package manager or in order to install any additional package on the build engine without the usage of the Scratchbox2 (sb2), the command has to be executed as root (sudo).
Note: If it is not possible to reach any host on the internet from the build engine (checked with a ping), you probably have to change the Promiscuous Mode of the VM MerSDK. Change it under Machine -> Settings... -> Network -> Adapter 2 -> Advanced -> Promiscuous Mode to Allow All.
The appropriate commands are:
/* if a package is installed, on the output of the following command */
/* there should be an 'i' on the left of the package name */
sudo zypper update // update zypper
zypper se git && zypper se cmake // check if insatlled
sudo zypper in git-all // install git tools
sb2 -t SailfishOS-i486 -m sdk-install -R zypper in cmake // inst cmake for i486
sb2 -t SailfishOS-armv7hl -m sdk-install -R zypper in cmake // inst cmake for arm7hl
Now, create and enter into a new folder (e.g. "opencv") in order to keep the build and install files in a separate directory. Within that new folder, create a working directory (build) and an install path for both, the i486 (inst-i486) and the armv7hl (inst-arm) cores. Afterwards, download the sources from github into a separate folder (e.g. sources). All the directories can be created with the following commands:
mkdir opencv && cd $_ // make root dir
mkdir build ocv-inst-i486 ocv-inst-arm // make install dirs
git clone https://github.com/opencv/opencv.git sources // clone opencv from git
3. Compile and install opencv for i486
First of all, ensure that cmake is installed on the build engine (see chp. 2). If so, the libraries can be compiled and installed by means of a sequentially executing of the commands cmake make and make install. The commands, however, are used in combination with the sb2 tool for cross compilation and hence appended to sb2 -t SailfishOS-i486 -m sdk-build as depicted in the following lines:
Note: Either you edit the cmake file first (see subsequent) or you pass all options to the cmake call.
cd /home/mersdk/opencv/build // change to working dir
sb2 -t SailfishOS-i486 -m sdk-build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/mersdk/opencv/ocv-inst-i486 -DSFOS=ON ../sources
sb2 -t SailfishOS-i486 -m sdk-build make
sb2 -t SailfishOS-i486 -m sdk-build make install
All the parameters with the prefix -D are cmake options, whereby, the last option is self introduced into the make-file CMakeLists.txt that is part of the downloaded sources. The SailfishOS specific options are derived from the existing Android options. Unfortunately I can't yet attach the used make file due to the low "karma". But, one of the most important option is the definition of the kind of the library files, whether they are packed into shared (.so) or static (.a) libraries. If you wanna publish tha app in the jolla harbour, you should ensure that the libraries are build as static libraries, because only the certain libraries on that list are accepted in the harbour. In common, all required options can be identified by the error messages from the cmake. Here a summary of all changes in the CMakeLists.txt file.
// the following options are explicitly enabled
OCV_OPTION(WITH_CAROTENE ON IF(SFOS OR ARM OR AARCH64) AND NOT IOS AND NOT (CMAKE_VERSION VERSION_LESS "2.8.11"))
OCV_OPTION(ENABLE_NEON "${NEON}" IF CMAKE_COMPILER_IS_GNUCXX AND (SFOS OR ARM OR AARCH64 OR IOS) )
// the following options are explicitly disabled i.e. removed from cache
OCV_OPTION(WITH_1394 ON IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_VTK ON IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT AND NOT CMAKE_CROSSCOMPILING))
OCV_OPTION(WITH_FFMPEG ON IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_GSTREAMER ON IF(NOT SFOS AND NOT ANDROID))
OCV_OPTION(WITH_GTK ON IF(NOT SFOS AND UNIX AND NOT APPLE AND NOT ANDROID))
OCV_OPTION(WITH_GTK_2_X OFF IF(NOT SFOS AND UNIX AND NOT APPLE AND NOT ANDROID))
OCV_OPTION(WITH_OPENGL OFF IF(NOT SFOS AND NOT ANDROID AND NOT WINRT))
OCV_OPTION(WITH_OPENNI OFF IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_OPENNI2 OFF IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_PVAPI OFF IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_GIGEAPI OFF IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_QT OFF IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_UNICAP OFF IF(NOT SFOS AND UNIX AND NOT APPLE AND NOT ANDROID))
OCV_OPTION(WITH_V4L ON IF(NOT SFOS AND UNIX AND NOT ANDROID))
OCV_OPTION(WITH_LIBV4L OFF IF(NOT SFOS AND UNIX AND NOT ANDROID))
OCV_OPTION(WITH_XIMEA OFF IF(NOT SFOS AND NOT ANDROID AND NOT WINRT))
OCV_OPTION(WITH_XINE OFF IF(NOT SFOS AND UNIX AND NOT APPLE AND NOT ANDROID))
OCV_OPTION(WITH_OPENCLAMDFFT ON IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_OPENCLAMDBLAS ON IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_MATLAB ON IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_VA OFF IF(NOT SFOS AND UNIX AND NOT ANDROID))
OCV_OPTION(WITH_VA_INTEL OFF IF(NOT SFOS AND UNIX AND NOT ANDROID))
OCV_OPTION(WITH_GDAL OFF IF(NOT SFOS AND NOT ANDROID AND NOT IOS AND NOT WINRT))
OCV_OPTION(WITH_GPHOTO2 ON IF(NOT SFOS AND UNIX AND NOT ANDROID))
OCV_OPTION(WITH_LAPACK ON IF(NOT SFOS AND NOT ANDROID))
OCV_OPTION(BUILD_PERF_TESTS ON IF(NOT SFOS AND NOT APPLE_FRAMEWORK))
OCV_OPTION(BUILD_TESTS ON IF(NOT SFOS AND NOT APPLE_FRAMEWORK))
// the following options are set (ON)
OCV_OPTION(WITH_OPENCL (NOT ANDROID AND NOT SFOS) IF(NOT IOS AND NOT WINRT))
OCV_OPTION(BUILD_TIFF WIN32 OR SFOS OR ANDROID OR APPLE)
OCV_OPTION(BUILD_JASPER WIN32 OR SFOS OR ANDROID OR APPLE)
OCV_OPTION(BUILD_JPEG WIN32 OR SFOS OR ANDROID OR APPLE)
OCV_OPTION(BUILD_PNG WIN32 OR SFOS OR ANDROID OR APPLE)
OCV_OPTION(BUILD_OPENEXR (WIN32 OR SFOS OR ANDROID OR APPLE) AND NOT WINRT)
// the following options are reset (OFF)
OCV_OPTION(BUILD_SHARED_LIBS NOT (SFOS OR ANDROID OR APPLE_FRAMEWORK))
OCV_OPTION(BUILD_opencv_apps (NOT SFOS AND NOT ANDROID AND NOT WINRT) IF(NOT APPLE_FRAMEWORK))
If all commands executed without any issue, the build libraries are stored in the install folder defined by the cmake option -DCMAKE_INSTALL_PREFIX and can now be copied to the host computer e.g. with the cp command. Don't forget to add the -L options (as shown in the following example) otherwise symbolic links (if there are any) are not copied. By executing the following example command, the files and links will be copied to the from the build engine to the mounting point on the host system.
// example is indep. on host system - dest. folder on windows: C:\Users\*username*\
cp -Lrf /home/mersdk/opencv/ocv-inst-i486/ /home/mersdk/share/
4. Compile and install opencv for armv7hl
The compilation for the arm processor (target system) is quite simmilar as for an i486 processor (develpment system SDK). Hence, additionally please consider the previous section that also contains information about changes (i.e. used options) in the CMakeLists.txt file. But actually, the only difference is to use the other cross compiler type sb2-t SailfishOS-armv7hl -m sdk-build. Moreover, it is recommended to use another install folder (created in chp. 2) so that the libs for the other target wont get overwritten. With that changes, the following commands are to apply:
cd /home/mersdk/opencv/build // change to working dir
sb2 -t SailfishOS-armv7hl -m sdk-build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/mersdk/opencv/ocv-inst-arm -DSFOS=ON ../sources
sb2 -t SailfishOS-armv7hl -m sdk-build make
sb2 -t SailfishOS-armv7hl -m sdk-build make install
Equally to the installation procedure for the i486 processor, all generated libraries can now be copied to the host system (see chp. 3).
5. Binding the libs into the SDK
To use the libraries in the software project they should be copied into a subfolder of the project directory, such as .../harbour-xxx/ocv-inst-arm or .../harbour-xxx/ocv-inst-i486. The references in the project are finally done in the project file .pro itself. Thereby, as static libraries are used, it is important to observe the order of the listing of reffered libraries. Libraries that depend on another one also need to be behind the other in the list order. Following a snippet from the .pro file how the libraries are included as well as a possible order for all opencv libraries:
// set path depending if the app runs on the VM (debug) or a real device (release)
CONFIG(debug,debug|release){ OCV_PATH = ocv-inst-i486 }
CONFIG(release,debug|release){ OCV_PATH = ocv-inst-arm }
QMAKE_LFLAGS += -Wl,-lc -Wl,-lz
// set include path
INCLUDEPATH += $$OCV_PATH/include
// link the static libraries
LIBS += \
-L$$PWD/$$OCV_PATH/lib \
-lopencv_videostab \
-lopencv_superres \
-lopencv_stitching \
-lopencv_video \
-lopencv_objdetect \
-lopencv_ml \
-lopencv_calib3d \
-lopencv_features2d \
-lopencv_highgui \
-lopencv_flann \
-lopencv_imgcodecs \
-lopencv_imgproc \
-llibpng \
-lopencv_core \
-lopencv_photo \
-lopencv_shape \
-lopencv_videoio \
-L$$PWD/$$OCV_PATH/share/OpenCV/3rdparty/lib \
-ltegra_hal \
-llibwebp \
-lIlmImf \
-llibjasper \
-llibjpeg \
-llibtiff
// register libraries to variable lib
lib.files += $$OCV_PATH/lib/libopencv_core.a \
$$OCV_PATH/lib/libopencv_imgproc.a \
$$OCV_PATH/lib/libopencv_highgui.a \
...
// set install path and register the variable to INSTALLS
lib.path = /usr/share/harbour-xxx/lib
INSTALLS += lib
After that, the libraries should be visible in the entire development environment and can be included in the C/C++ sources as common libs by means of an include assignment: #include <opencv2/xxx.hpp>
6. Finish
If all is done you can try to compile and run your application. Good luck!
In our company we compile it by qmake and qmake projects files ( not from ./configure - make - install ), if need, i can drop here pro files ))
sashikknox ( 2017-07-19 13:46:51 +0200 )edit