(CMake) Gently install official OpenCV on WSL (C / C ++, Python2, Python3)

Related article: Installation procedure for easy Windows Subsystem for Linux (WSL)

Introduction

I have ** installed the official release of OpenCV 4.1.2 on Ubuntu of WSL (Windows Subsystem for Linux) **, so I will keep it as a memorandum.

The target is ** for those who want to use OpenCV in C ++ **. I will install it so that it can be used with Python2 and Python3, but if you want to use it only with Python, it seems easy if you install ʻopencv-python`. Also, I will try to explain the commands that appear in the middle as much as possible.

Please refer to ** OpenCV official page ** (Installation in Linux).

version

The environment I worked on this time was as follows. For your reference. Windows 10 Home Version 1809 Ubuntu 18.04.3 LTS OpenCV4.1.2-dev Python 2.7.15+ Python 3.6.8

Overview

--1 Package installation --1.1 Required packages --1.2 Python-dev and numpy --1.3 Options ―― 2. Obtaining OpenCV source code ―― 3. Creating a makefile with CMake --4 make installation --5 Operation check (version display) - 5.1 Python2 - 5.2 Python3 - 5.3 C++ --Build using 5.3.1 cmake --5.3.2 Build using pkg-config [](-6 Operation check (face recognition) - 6.1 Python2 - 6.2 Python3 - 6.3 C++) ―― 6. Other ―― 7. Reference

Please note that the work of ** 4. ** takes a waiting time (about 30 minutes), so please do it when you have time.

1. Package installation

Execute the part corresponding to ** Required Packages ** on the official page (Introduction to OpenCV).

Click here if you want to install from ** 1.1 ** to ** 1.3 ** below
$ sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libvtk5-qt4-dev libqt4-dev libqt4-opengl-dev libgtkglext1 libgtkglext1-dev python-dev python-numpy python3-dev python3-numpy

1.1 Required packages

For the time being, if you have this, you can ** install OpenCV ** and use it with ** C ++ **. Execute the following command to install the package.

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

** Description of each package ** ** build-essential: ** * Compilers such as gcc and g ++, make, etc. * ** cmake: ** This time, it is used for the purpose of creating a makefile when doing a make build. It is also used for operation check ** 5.3.1 **, so please refer to this (https://kamino.hatenablog.com/entry/cmake_tutorial1). ** git: ** This time, I will use it only when I get the source code of OpenCV, but if you are interested, [here](https: //) Please refer to qiita.com/jesus_isao/items/63557eba36819faa4ad9). ** libgtk2.0-dev: ** GTK Library development It seems to be a package for, but maybe it is related to the GUI framework of OpenCV ** pkg-config: ** Facilitates compilation with gcc, g ++. Operation check ** 5.3.2 ** will explain briefly. ** libavcodec-dev, libavformat-dev, libswscale-dev: ** FFmpeg and Libav development packages.

1.2 Python-dev and numpy

Install Python2 and Python3, and their corresponding numpy.

$ sudo apt install python-dev python-numpy python3-dev python3-numpy

: warning: ** Note: python and python-dev are different. Even if you already have python installed, please execute the above command! ** ** (Python-dev is a developer package that includes python. You can install it more than once.)

Python version check
#Python2 version check
$ python -V

#Python3 version check
$ python3 -V
You can check the version of numpy with \ _ \ _ version__ as well as ** 5.1 ** and ** 5.2 **.

1.3 Options

You can use OpenCV without installing the following packages. Select the one you need and install it. If you get lost, install it for the time being.

$ sudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libvtk5-qt4-dev libqt4-dev libqt4-opengl-dev libgtkglext1 libgtkglext1-dev

** Description of each package ** ** libtbb2, libtbb-dev: ** Enable parallel processing by TBB (Threading Building Blocks) ** libjpeg-dev, libpng-dev, libtiff-dev: ** Enabled to handle jpeg, png, tiff format image files ** libdc1394-22-dev: ** High-level programming interface for IEEE1394 digital cameras ** libvtk5-qt4-dev, libqt4-dev, libqt4-opengl-dev, libgtkglext1, libgtkglext1-dev: ** GUI related packages such as QT, OpenGL, GTK

2. Get OpenCV source code

OpenCV and OpenCV contrib from this repository (OpenCV) and this repository (OpenCV contrib) I will put the source code of. (Reference) Introduction to opencv_contrib

#Move to home directory
$ cd ~

#Get OpenCV source code
$ git clone https://github.com/opencv/opencv.git

#Get the OpenCV contrib source code
$ git clone https://github.com/opencv/opencv_contrib.git

If you want to switch branches (for example, if you want to change the version of OpenCV you want to install), please do git checkout 2.4.0 under ~ / opencv.

3. Creating a makefile with CMake

3.1 Create directory for build

Execute the following command to create a directory for build (here, the directory named build) directly under ~ / opencv.

$ cd ~/opencv
$ mkdir build
$ cd build

3.2 Running CMake

CMake (create a makefile when doing a make build) while setting options referring to CMakeLists.txt. Execute the following command.

$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D OPENCV_GENERATE_PKGCONFIG=ON ..

: warning: ** Note 1: Don't forget the last two dots .. of the CMake command! ** ** (This represents the hierarchy one level above the current directory. It is for specifying the location of CMakeLists to refer to when executing cmake.)

: warning: ** Note 2: If the above command doesn't work, remove all spaces after -D like -DCMAKE_BUILD_TYPE = Release and try again ** (-D is an option of CMake and means rewriting.)

** Option description ** ** CMAKE_BUILD_TYPE **: Specify build mode ** CMAKE_INSTALL_PREFIX **: Specify installation destination ** OPENCV_EXTRA_MODULES_PATH **: Specify the path of OpenCV_contrib ** OPENCV_GENERATE_PKGCONFIG **: Create a .pc file in pkgconfig (used when compiling C ++) ** WITH_TBB **: Use of TBB (Threading Building Blocks) (execute C ++ in parallel) ** WITH_OPENGL **: Use of OPENGL ** WITH_VTK **: Using VTK

CMake execution result
If you follow the above procedure so far, you will get the following execution result.
-- General configuration for OpenCV 4.1.2-dev =====================================
--   Version control:               4.1.2-121-g5dd3e6052e
--
--   Extra modules:
--     Location (extra):            /home/(username)/opencv_contrib/modules
--     Version control (extra):     4.1.2-32-g61cbb445
--
--   Platform:
--     Timestamp:                   2019-11-10T16:00:51Z
--     Host:                        Linux 4.4.0-17763-Microsoft x86_64
--     CMake:                       3.10.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               Release
--
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (16 files):         + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (6 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++  (ver 7.4.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--gc-sections
--     Linker flags (Debug):        -Wl,--gc-sections
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt /usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so
--     3rdparty dependencies:
--
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 quality reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv java js matlab ovis sfm
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--
--   GUI:
--     GTK+:                        YES (ver 2.24.32)
--       GThread :                  YES (ver 2.56.4)
--       GtkGlExt:                  YES (ver 1.2.0)
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
--     VTK support:                 YES (ver 7.1.1)
--
--   Media I/O:
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
--
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (57.107.100)
--       avformat:                  YES (57.83.100)
--       avutil:                    YES (55.78.100)
--       swscale:                   YES (4.8.100)
--       avresample:                NO
--     GStreamer:                   NO
--     v4l/v4l2:                    YES (linux/videodev2.h)
--
--   Parallel framework:            TBB (ver 2017.0 interface 9107)
--
--   Trace:                         YES (with Intel ITT)
--
--   Other third-party libraries:
--     Intel IPP:                   2019.0.0 Gold [2019.0.0]
--            at:                   /home/(username)/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2019.0.0)
--               at:                /home/(username)/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.4)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
--
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/(username)/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.15)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.15+)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
--     install path:                lib/python2.7/dist-packages/cv2/python-2.7
--
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.6.8)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.8)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.13.3)
--     install path:                lib/python3.6/dist-packages/cv2/python-3.6
--
--   Python (for build):            /usr/bin/python2.7
--
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/(username)/opencv/build
If you look at ** ↑ **, you can see that cv2 is installed under `dist-packages /` of python2 and python3 respectively.

4. make installation

make Build. -j * is an option of the make command to process make builds in parallel. Execute the following command. It may take about 30 minutes. (Parallel option is set to 7 according to Official page)

$ make -j7

Then make install. Execute the following command.

$ sudo make install

Installation is complete!

5. Operation check (version display)

As for Python2 and Python3, it's easy because I think the path is already in place. For C ++, create a Makefile with CMake or pass it with pkg-config.

The following assumes that you are running directly under ~ / opencv. Go to your home directory with cd ~ / opencv.

5.1 Python2 Launch Python from the command line to view the version in interactive mode. (Run Python from the command line)

First, start Python 2 interactive mode.

$ python2 #Invoking interactive mode

Next, import OpenCV in interactive mode and display the OpenCV version.

>> import cv2 #OpenCV import
>> print(cv2.__version__) #View version

When the installed version of OpenCV is displayed, the operation check is complete. Exits interactive mode.

>> exit() #End of interactive mode

5.2 Python3 ** 5.1 ** It is OK if you can execute exactly the same thing as checking the operation of python2 with python3.

5.3 C++ When I wrote how to use CMake and how to use pkg-config, it became unexpectedly long, so I will post it in another article. Compile C ++ code using OpenCV (CMake, GCC, pkg-config)

[](\ # 6. Operation check (face recognition) ###6.1 Python2 ###6.2 Python3 ###6.3 C++)

6. Other

It is good that using OpenCV not only improves image recognition, but also makes it very easy to handle image files in various formats.

Actually, I tried to do something more like opencv in addition to the version display to check the operation, but I am tired so I will do it this time

7. Reference

Search the contents of the package (https://packages.ubuntu.com/ja/) How to use CMake 1-3 (https://qiita.com/shohirose/items/45fb49c6b429e8b204ac#_reference-27dce2bd429efef9488e)

Recommended Posts