Install OpenCV in Ubuntu 18.04

황고은·2022년 11월 22일
0

1. Check OpenCV installed

$ pkg-config --modversion opencv

# after OpenCV4
$ pkg-config --modversion opencv4

If OpenCV is not installed,

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

Delete OpenCV package

$ sudo apt-get purge libopencv* python-opencv
$ sudo apt-get autoremove

Delete OpenCV library

$  sudo find /usr/local/ -name "*opencv*" -exec rm -i {} \;

2. Environment Setting

$ sudo apt-get update
$ sudo apt-get upgrade

Package installation for build

$ sudo apt-get install build-essential cmake pkg-config 
$ sudo apt-get install libjpeg-dev libtiff5-dev libpng-dev ffmpeg libavcodec-dev libavformat-dev libswscale-dev libxvidcore-dev libx264-dev libxine2-dev libv4l-dev v4l-utils libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk-3-dev mesa-utils libgl1-mesa-dri libgtkgl2.0-dev libgtkglext1-dev libatlas-base-dev gfortran libeigen3-dev python2.7-dev python3-dev python-numpy python3-numpy

3. OpenCV build environment setting

$ mkdir opencv
$ cd opencv
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip
$ unzip opencv.zip
$ unzip opencv_contrib.zip

$ cd opencv-4.2.0
$ mkdir build
$ cd build

OpenCV build setting

$ 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 the setting is corrected, --Configuration done, --Generating done message are printed.

4. OpenCV build

Check number of CPU core to use build

$ cat /proc/cpuinfo | grep processor | wc -l

Then number of CPU core is printed, after enter the number with the assignment

$ time make -j{CPU core number}

OpenCV install

$ sudo make install

If /usr/local/lib is not exist, the assignment is need.

$ cat /etc/ld.so.conf.d/*
/usr/local/cuda-10.0/targets/x86_64-linux/lib
/usr/lib/x86_64-linux-gnu/libfakeroot
# Multiarch support
/usr/local/lib/i386-linux-gnu
/lib/i386-linux-gnu
/usr/lib/i386-linux-gnu
/usr/local/lib/i686-linux-gnu
/lib/i686-linux-gnu
/usr/lib/i686-linux-gnu
# libc default configuration
/usr/local/lib
# Multiarch support
/usr/local/lib/x86_64-linux-gnu
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
# Legacy biarch compatibility support
/lib32
/usr/lib32
profile
Snow Roller : 영차영차 눈을 굴려보아요 ⛄🚎

0개의 댓글