how to install OpenCV 4.2.0  on Ubuntu 18.04
Tags: ubuntu, opencv
Published: 2021-04-01 08:23:01

# 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. 
```bash
sudo apt-get install build-essential cmake
```
Packages required to import or record image files in a particular format
```bash
sudo apt-get install libjpeg-dev libtiff5-dev libpng-dev
```
Packages that are required to read or record video files from a particular codec.
```bash
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libxvidcore-dev libx264-dev libxine2-dev
```
The Video4Linux package includes device drivers and APIs to support real-time video capture on Linux.
```bash
sudo apt-get install libv4l-dev v4l-utils
```
GStreamer is a library for streaming video
```bash
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev 
```
OpenCV can window itself using the highgui module to show images or videos.
```bash
sudo apt-get install libgtk2.0-dev
```
Library required to support OpenGL.
```bash
sudo apt-get install mesa-utils libgl1-mesa-dri libgtkgl2.0-dev libgtkglext1-dev  
```
Libraries used for OpenCV optimization.
```bash 
sudo apt-get install libatlas-base-dev gfortran libeigen3-dev
```
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.
```bash
sudo apt-get install python2.7-dev python3-dev python-numpy python3-numpy
```
# OpenCV Install (이제시작)
Download and extract the OpenCV 4.2.0 source code from the homepage.
```bash
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip

$ unzip opencv_contrib.zip
```
Download and extract the opencv_contrib (extramodules) source code
```bash
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip
$ unzip opencv_contrib.zip
```
```bash
cd opencv-4.0.1/
mkdir build
cd build
```
Set the opencv compilation using cmake.
/usr/local 에 opencv4/opencv2 install
```bash
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 ../
```
If you see the following message, it is normal.
```bash
-- 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
```
Start the compilation using the make(It can take a long time.)
```bash
make
```
If the compilation is successful, you can view the following messages:
```bash
[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
```
You will now install the compilation output.
```bash
sudo make install
```
# Check Opencv Installation Results 
- Check Cpp
```bash
g++ -o Hellow Hellow.cpp $(pkg-config opencv4 --libs --cflags)
```
- Check python3 
```bash
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'

>>> 
```