# OpenCV-Install-tutorial **Repository Path**: mhguo/open-cv-install-tutorial ## Basic Information - **Project Name**: OpenCV-Install-tutorial - **Description**: How to install opencv4 in Ubuntu20.04 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-05-17 - **Last Updated**: 2023-03-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Tutorial: How to install OpenCV-4.7.0 in Ubuntu22.0 - date: 2021-5-17 - update: 2023-3-25 - by: mhguo - environment: - OpenCV-4.7.0, - Ubuntu 22.04.2 LTS on VMware 17 Pro - reference: [How to install OpoenCV 4 on Ubuntu](https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu/) --- ## Step0: Change sources and update ## Step1: Install OpenCV4 dependencies on Ubuntu ```bash # update system sudo apt-get update sudo apt-get upgrade # install developer tools: sudo apt-get install build-essential cmake unzip pkg-config # install a handful of image and vide I/O libraries. sudo apt-get install libjpeg-dev libpng-dev libtiff-dev sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo apt-get install install libxvidcore-dev libx264-dev # install GTK for our GUI backed: sudo apt-get install libgtk-3-dev # install two packages which contain mathematical optimizations for OpenCV: sudo apt-get install libatlas-base-dev gfortran # install Python3 development headers: sudo apt-get install python3-dev ``` ## Step2: Download, Unzip, MV OpenCV files. - copy zip files to ~. ```bash cd ~ #wget -O opencv.zip https://github.com/opencv/opencv/archive/4.7.0.zip #wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.7.0.zip unzip opencv.zip unzip opencv_contrib.zip # Rename mv opencv-4.7.0 opencv mv opencv_contrib-4.7.0 opencv_contrib ``` > `opencv.zip` and `opencv_contrib.zip` has already been downloaded in this repository. ## Step3: Configure python3 virtual environment - copy `get-pip.py` to ~. ```bash cd ~ #wget https://bootstrap.pypa.io/get-pip.py sudo python3 get-pip.py sudo pip install virtualenv virtualenvwrapper sudo rm -rf ~/get-pip.py ~/.cache/pip ``` Using `nano` to open `~/.bashrc` file: ```bash sudo nano ~/.bashrc ``` Add 3 lines below to the end of `~/.bashrc` file. ```bash export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /usr/local/bin/virtualenvwrapper.sh ``` Update source file like this: ````bash source ~/.bashrc ```` Create virtual environment (py3cv4) using below cmd: ```bash mkvirtualenv py3cv4 -p python3 ``` When virtual environment is created, using `workon` to active such env: ```bash workon py3cv4 ``` Install numpy ```bash pip install numpy ``` ## Step4: Compile OpenCV and link it to Virtual Python - cmake and make OpenCV4 **(Warning: running in virtual environment `py3cv4` )** ```bash cd ~/opencv mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ #using nonfree modules -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D PYTHON_EXECUTABLE=~/.virtualenvs/py3cv4/bin/python \ # Warning: vir name must be the same -D BUILD_EXAMPLES=ON .. make -j4 #according to the number of CPU sudo make install sudo ldconfig ``` - NOTE: check results of `cmake` - **Important**: link OpenCV4 to virtual environment - Step1: Check `cv2.so`: ```bash ls /usr/local/lib/python3.8/site-packages/cv2/python-3.8/ ``` it will return: `cv2.cpython-38-x86_64-linux-gnu.so` - Step2: Rename to simple `cv2.so`: ```bash cd /usr/local/lib/python3.8/site-packages/cv2/python-3.8/ sudo mv cv2.cpython-38-x86_64-linux-gnu.so cv2.so ``` - Step3: Make link ```bash cd ~/.virtualenvs/py3cv4/lib/python3.8/site-packages ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.so cv2.so ``` --- # OpenCV Test: - check the version of OpenCV in python terminal: ```bash workon py3cv4 python import cv2 print(cv2.__version__) ``` - In folder `OpenCV_Basic_Demos`, a demo `.py` file is created for the following tests: - test1: read and show a jpg picture. - test2: read and show a mp4 video. - test3: read and show a live camera. - If the camera is connected but not work, check if USB 3.0 is used.