Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Install OpenCV 4 on Ubuntu 18.04 (C++ and Python)

Learn how to build and install OpenCV from source. Bash script provided within.
Install OpenCV 4 on Ubuntu

In this post, we will provide abash script for installingOpenCV-4.0 (C++ and Python 3.6) on Ubuntu 18.04. We will also briefly study the script to understand what’s going in it. Note that this script will install OpenCV in a local directory and not on the entire system.

Looking for installation script forUbuntu 16.04? Have a look atthis blog.

What’s New in OpenCV 4.x?

OpenCV released OpenCV-3.4.4 and OpenCV-4.0.0 on 20th November. There have been a lot of bug fixes and other changes in these versions. The release highlights are as follows:

  • OpenCV is now C++11 library and requires C++11-compliant compiler. Minimum required CMake version has been raised to 3.5.1.
  • A lot of C API from OpenCV 1.x has been removed.
  • Persistence (storing and loading structured data to/from XML, YAML or JSON) in the core module has been completely reimplemented in C++ and lost the C API as well.
  • New module G-API has been added, it acts as an engine for very efficient graph-based image procesing pipelines.
  • dnn module now includes experimental Vulkan backend and supports networks in ONNX format.
  • The popular Kinect Fusion algorithm has been implemented and optimized for CPU and GPU (OpenCL)
    QR code detector and decoder have been added to the objdetect module.
  • Very efficient and yet high-quality DIS dense optical flow algorithm has been moved from opencv_contrib to the video module.

Install OpenCV on Ubuntu for CPP

Step 0: Select OpenCV version to install

echo "OpenCV installation by learnOpenCV.com"# Define OpenCV Version to installcvVersion="master"

We are also going to cleanbuild directories and createinstallation directory.

Clean build directoriesrm -rf opencv/buildrm -rf opencv_contrib/build
# Create directory for installationmkdir installationmkdir installation/OpenCV-"$cvVersion"

Finally, we will be storing the current working directory incwd variable. We are also going to refer to this directory asOpenCV_Home_Dirthroughout this blog.

 Save current working directorycwd=$(pwd)

Step 1: Update Packages

sudo apt -y updatesudo apt -y upgrade

If you are still not able to install OpenCV on your system, but want to get started with it, we suggest using our docker images with pre-installed OpenCV, Dlib, miniconda and jupyter notebooks along with other dependencies as described inthis blog.

Step 2: Install OS Libraries

sudo apt -y remove x264 libx264-dev## Install dependenciessudo apt -y install build-essential checkinstall cmake pkg-config yasmsudo apt -y install git gfortransudo apt -y install libjpeg8-dev libpng-devsudo apt -y install software-properties-commonsudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"sudo apt -y updatesudo apt -y install libjasper1sudo apt -y install libtiff-devsudo apt -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-devsudo apt -y install libxine2-dev libv4l-devcd /usr/include/linuxsudo ln -s -f ../libv4l1-videodev.h videodev.hcd "$cwd"sudo apt -y install libgstreamer1.0-dev libgstreamer-plugins-base1.0-devsudo apt -y install libgtk2.0-dev libtbb-dev qt5-defaultsudo apt -y install libatlas-base-devsudo apt -y install libfaac-dev libmp3lame-dev libtheora-devsudo apt -y install libvorbis-dev libxvidcore-devsudo apt -y install libopencore-amrnb-dev libopencore-amrwb-devsudo apt -y install libavresample-devsudo apt -y install x264 v4l-utils# Optional dependenciessudo apt -y install libprotobuf-dev protobuf-compilersudo apt -y install libgoogle-glog-dev libgflags-devsudo apt -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

Looking for installation script forOpenCV 3.4.4 on Ubuntu 18.04? Have a look atthis blog.

Step 3: Install Python Libraries

sudo apt -y install python3-dev python3-pipsudo -H pip3 install -U pip numpysudo apt -y install python3-testresources

We are also going to installvirtualenv andvirtualenvwrapper modules to create Python virtual environments.

cd $cwd############ For Python 3 ############# create virtual environmentpython3 -m venv OpenCV-"$cvVersion"-py3echo "# Virtual Environment Wrapper" >> ~/.bashrcecho "alias workoncv-$cvVersion="source $cwd/OpenCV-$cvVersion-py3/bin/activate"" >> ~/.bashrcsource "$cwd"/OpenCV-"$cvVersion"-py3/bin/activate# now install python libraries within this virtual environmentpip install wheel numpy scipy matplotlib scikit-image scikit-learn ipython dlib# quit virtual environmentdeactivate

To easily follow along this tutorial, please download installation script by clicking on the button below. It's FREE!

Download CodeTo easily follow along this tutorial, please download code by clicking on the button below. It's FREE!

Step 4: Download opencv and opencv_contrib

git clone https://github.com/opencv/opencv.git cd opencv git checkout $cvVersion cd .. git clone https://github.com/opencv/opencv_contrib.git cd opencv_contrib git checkout $cvVersion cd ..

Step 5: Compile and install OpenCV with contrib modules

First we navigate to the build directory.

cd opencvmkdir buildcd build

Next, we start the compilation and installation process.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-"$cvVersion" -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D WITH_TBB=ON -D WITH_V4L=ON -D OPENCV_PYTHON3_INSTALL_PATH=$cwd/OpenCV-$cvVersion-py3/lib/python3.5/site-packages -D WITH_QT=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D BUILD_EXAMPLES=ON ..

For system wide installation of OpenCV, changeCMAKE_INSTALL_PREFIX toCMAKE_INSTALL_PREFIX=/usr/local.

make -j4make install

How to use OpenCV in C++

Using CMakeLists.txt
The basic structure of yourCMakeLists.txt will be as follows.

cmake_minimum_required(VERSION 3.1)# Enable C++11set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

<predata-previewers=””>

The basic stavructure of yourCMakeLists.txt will be as follows:

SET(OpenCV_DIR /installation/OpenCV-master/lib/cmake/opencv4

Make sure that you replaceOpenCV_Home_Dir with correct path. For example, in my case:

SET(OpenCV_DIR /home/hp/OpenCV_installation/installation/OpenCV-master/lib/cmake/opencv4)

Once you have made your CMakeLists.txt, follow the steps given below.

mkdir build && cd buildcmake ..cmake --build . --config Release

This will generate your executable file inbuild directory.

How to use OpenCV in Python

To use the OpenCV version installed using Python script, first we activate the correct Python Virtual Environment.For OpenCV-4 : Python 3

workon OpenCV-master-py3

Once you have activated the virtual environment, you can enter Python shell and test OpenCV version.

import cv2print(cv2.__version__)

Hope this script proves to be useful for you :). Stay tuned for more interesting stuff. In case of any queries, feel free to comment below and we will get back to you as soon as possible.

Was This Article Helpful?

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used in this post, pleaseclick here. Alternately, sign up to receive a freeComputer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.



SAM-3: What’s New, How It Works, and Why It Matters

Yet another SOTA model from META, meet SAM-3. Learn about what’s new and how to

Image-GS: Adaptive Image Reconstruction using 2D Gaussians

Discover Image-GS, an image representation framework based on adaptive 2D Gaussians, outperforming neural and classical

vLLM: Deploying LLMs at Scale Like OpenAI

vLLM Paper Explained. Understand how pagedAttention, and continuous batching works along with other optimizations by

Table of Contents

Was This Article Helpful?

Read Next

ShubhamOctober 7, 2025

VideoRAG: Redefining Long-Context Video Comprehension

Discover VideoRAG, a framework that fuses graph-based reasoning and multi-modal retrieval to enhance LLMs' ability to understand multi-hour videos efficiently.

KukilSeptember 30, 2025

AI Agent in Action: Automating Desktop Tasks with VLMs

Learn how to build AI agent from scratch using Moondream3 and Gemini. It is a generic task based agent free from…

Bhomik SharmaSeptember 23, 2025

The Ultimate Guide To VLM Evaluation Metrics, Datasets, And Benchmarks

Get a comprehensive overview of VLM Evaluation Metrics, Benchmarks and various datasets for tasks like VQA, OCR and Image Captioning.

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.

Subscribe to receive the download link, receive updates, and be notified of bug fixes

Which email should I send you the download link?

  • We hate SPAM and promise to keep your email address safe.
 

Get Started with OpenCV

  • FREE OpenCV Crash Course
  • Getting Started Guides
  • Installation Packages
  • C++ And Python Examples
  • Newsletter Insights
  • We hate SPAM and promise to keep your email address safe.
Subscribe To Receive
  • FREE OpenCV Crash Course
  • Getting Started Guides
  • Installation Packages
  • C++ And Python Examples
  • Newsletter Insights

We hate SPAM and promise to keep your email address safe.​

  • We hate SPAM and promise to keep your email address safe.

[8]ページ先頭

©2009-2025 Movatter.jp