DEV Community

SolBeen Kwon
SolBeen Kwon

Posted on

3 2

how to install OpenCV 4.2.0 on Ubuntu 18.04

Install required packages before compilation of OpenCV

  • build-essential

The package includes a C/C++ compiler, related libraries, and tools such as make.

  • cmake

Required for compilation options, settings for OpenCV modules to be included in the built library, etc.

sudo apt-get install build-essential cmake
Enter fullscreen mode Exit fullscreen mode

Packages required to import or record image files in a particular format

sudo apt-get install libjpeg-dev libtiff5-dev libpng-dev
Enter fullscreen mode Exit fullscreen mode

Packages that are required to read or record video files from a particular codec.

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libxvidcore-dev libx264-dev libxine2-dev
Enter fullscreen mode Exit fullscreen mode

The Video4Linux package includes device drivers and APIs to support real-time video capture on Linux.

sudo apt-get install libv4l-dev v4l-utils
Enter fullscreen mode Exit fullscreen mode

GStreamer is a library for streaming video

sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev 
Enter fullscreen mode Exit fullscreen mode

OpenCV can window itself using the highgui module to show images or videos.

sudo apt-get install libgtk2.0-dev
Enter fullscreen mode Exit fullscreen mode

Library required to support OpenGL.

sudo apt-get install mesa-utils libgl1-mesa-dri libgtkgl2.0-dev libgtkglext1-dev  
Enter fullscreen mode Exit fullscreen mode

Libraries used for OpenCV optimization.

sudo apt-get install libatlas-base-dev gfortran libeigen3-dev
Enter fullscreen mode Exit fullscreen mode

The python2.7-dev and python3-dev packages are required for OpenCV-Python binding.

Numpy is used in OpenCV because it can process matrix operations quickly.

sudo apt-get install python2.7-dev python3-dev python-numpy python3-numpy
Enter fullscreen mode Exit fullscreen mode

OpenCV Install (이제시작)

Download and extract the OpenCV 4.2.0 source code from the homepage.

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

$ unzip opencv_contrib.zip
Enter fullscreen mode Exit fullscreen mode

Download and extract the opencv_contrib (extramodules) source code

$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip
$ unzip opencv_contrib.zip
Enter fullscreen mode Exit fullscreen mode
cd opencv-4.0.1/
mkdir build
cd build
Enter fullscreen mode Exit fullscreen mode

Set the opencv compilation using cmake.
/usr/local 에 opencv4/opencv2 install

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=OFF \
-D WITH_IPP=OFF \
-D WITH_1394=OFF \
-D BUILD_WITH_DEBUG_INFO=OFF \
-D BUILD_DOCS=OFF \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D WITH_QT=OFF \
-D WITH_GTK=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.2.0/modules \
-D WITH_V4L=ON  \
-D WITH_FFMPEG=ON \
-D WITH_XINE=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON ../
Enter fullscreen mode Exit fullscreen mode

If you see the following message, it is normal.

-- Configuring done

-- Generating done

-- Build files have been written to: -- Configuring done

-- Generating done

-- Build files have been written to: /home/xxxxx/opencv/opencv-4.2.0/build
Enter fullscreen mode Exit fullscreen mode

Start the compilation using the make(It can take a long time.)

make
Enter fullscreen mode Exit fullscreen mode

If the compilation is successful, you can view the following messages:

[100%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
[100%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o
[100%] Linking CXX shared module ../../lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so
[100%] Linking CXX shared module ../../lib/cv2.so
[100%] Built target opencv_python3
[100%] Built target opencv_python2
Enter fullscreen mode Exit fullscreen mode

You will now install the compilation output.

sudo make install
Enter fullscreen mode Exit fullscreen mode

Check Opencv Installation Results

  • Check Cpp
g++ -o Hellow Hellow.cpp $(pkg-config opencv4 --libs --cflags)
Enter fullscreen mode Exit fullscreen mode
  • Check python3
python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 

[GCC 8.3.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import cv2

>>> cv2.__version__

'4.2.0'

>>> 
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (1)

Collapse
 
neilyoung profile image
neilyoung

Full of errors... Review required. Thanks nevertheless

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More