--- title: OpenCV installation on Ubuntu 18.04 tags: OpenCV description: View the slide with "Slide Mode". # type: slide # dual mode to preview slide on the right. --- # OpenCV installation on Ubuntu 18.04 <!-- Put the link to this slide here so people can follow --> <!-- slide: https://hackmd.io/@BennyHito/cv-on-ubuntu1804#/ --> <!-- --- --> *Install the OpenCV with versions of Python 2/3 and C++ on Ubuntu 18.04* --- ## Preparations ```shell # Update the package lists sudo apt update -y # Install required packages sudo apt install -y pkg-config python-dev python-numpy python-scipy python-setuptools python-pip ``` ## Direct Installation ### With Python 2 #### Input command: ```shell sudo apt install python-opencv -y ``` #### Confirm a correct installation of OpenCV with Python 2: ```shell $ python Python 2.7.17 (default, Mar 18 2022, 13:21:42) [GCC 7.5.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '4.5.0' ``` ### With Python 3 #### Input command: ```shell sudo apt install python3-opencv -y ``` #### Confirm a correct installation of OpenCV with Python 3: ```shell $ python3 Python 3.6.9 (default, Mar 15 2022, 13:55:28) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '4.6.0' ``` ### With C++ #### Input command: ```shell sudo apt install libopencv-dev -y ``` #### Confirm a correct installation of OpenCV with C++: ```shell $ pkg-config --modversion opencv 3.2.0 ``` --- ## Sources Building ### Install the required packages to build the official sources ```shell sudo apt-get install -y cmake git libgtk2.0-dev pkg-config mlocate \ libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev \ libpng-dev libtiff-dev libjasper1 libjasper-dev libdc1394-22-dev build-essential ``` ### Download sources from OpenCV website ```shell # Go to $HOME folder cd ~ # 4.6.0 was released on June 12, 2022 wget -O opencv.zip https://github.com/opencv/opencv/archive/4.6.0.zip wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.6.0.zip # Unzip the folder of sources unzip opencv.zip unzip opencv_contrib.zip # Do operations for working folders mv opencv-4.5.0 opencv mv opencv_contrib-4.5.0 opencv_contrib mv opencv_contrib opencv cd ~/opencv && mkdir build && cd build ``` ### Build all :::info It will take long time to build all, but you can speed it up by {-jN}: N is as twice as the number of kernels of your CPU. ::: ```shell cmake .. sudo make -j8 sudo make install cd ~ rm opencv.zip opencv_contrib.zip ``` ### Config the path of OpenCV libraries ```shell sudo touch /etc/ld.so.conf.d/opencv.conf sudo echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf sudo ldconfig sudo echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig" >> /etc/bash.bashrc sudo echo "export PKG_CONFIG_PATH" >> /etc/bash.bashrc source /etc/bash.bashrc sudo updatedb -y ``` ### Restart the computer if required ```shell sudo reboot ``` --- ## Additional Environment Setup (If Required) ### Reading multimedia data from OpenCV samples ```shell echo "export OPENCV_SAMPLES_DATA_PATH=~/opencv/samples/data" >> ~/.bashrc source ~/.bashrc ``` ### Allow Ubuntu 18.04 to decode mpeg-4 formats ```shell sudo apt-get install ubuntu-restricted-extras sudo apt-get install libavcodec54 libav-tools ffmpeg ```