How to install OpenCV version 3.1 on Ubuntu 16.04 LTS to work with Python 3.5.1?
I tried this on a VM but obviously that is useful to do if you want to work with Python 2.
I have OpenCV version 3.1 on Ubuntu 16.04 LTS to work with Python 3.5.1. At the latest version it's fixed, try:
But I didn't download dev python and OpenCV, I took directly from github:
Here are the instructions I tested successfully and mentioned in the link above:
sudo apt-get update
Download the required packages to compile OpenCV:
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python3.5-dev
python3.5-config --includesThe output should be similar to:
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m. The first part of the output is the expected location & the second part shows the current location of the config file. To solve this problem, we’ll copy the file from the current location to the expected location:
sudo cp /usr/include/x86_64-linux-gnu/python3.5m/pyconfig.h /usr/include/python3.5m/
git clone https://github.com/Itseez/opencv.git. This will download OpenCV 3. Then rename the folder
build directory, move to it and run:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ../opencv-3
sudo make install.
I found this.
OpenCV on wheels.
Unofficial OpenCV packages for Python.
Installation was painless for Ubuntu 16.04:
pip3 install opencv-python
Check the installation:
python3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.2.0'
python3-opencv can be installed from the Universe repository in Ubuntu 17.10 and later. Open the terminal and type:
sudo apt install python3-opencv
Upgrade your OS to 18.04 if you are using Ubuntu 14.04 or Ubuntu 16.04. If Ubuntu 16.04 is installed in WSL
sudo do-release-upgrade -d will upgrade it to 18.04.
The most simple and elegant way I found online is to install the library by running an installation script. Download the installation script
install-opencv.sh, open your terminal and execute:
Type your sudo password and you will have installed OpenCV. This operation may take a long time due to the packages to be installed and the compilation process.
The reference: https://milq.github.io/install-opencv-ubuntu-debian/
I am currently working with Google VM (ubuntu 14.04). Installing opencv on python3.4 version has been quite a task. I wanted opencv to be installed for python 3.4 but every time it was getting installed on 2.7 version.
I will share the steps I followed so as to help others on it.
Step 1 Follow all the steps as mentioned on openCv installation part till cmake. Link is given below:
Note: Install all the 3 packages mentioned at start. That optional one too..!! And don't forget to change the python version for which you are installing.
sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
Follow step 2 for
Step 2 For installing opencv in specific version of python (ubuntu), you have to set the default (PYTHON_DEFAULT_EXECUTABLE) with the path to where your python is installed. You can find that out by using command whereis python3.4 (or, your version). Mine was in
Instead of cmake mentioned on the page, use this,
cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=ON -D HAVE_opencv_python3=ON -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.4 ..
Note: Don't forget to change your python version and path in PYTHON_DEFAULT_EXECUTABLE.
Step 3 Follow the remaining steps as mentioned in the link till sudo make install
Hope it helps.
The following, tested on Ubuntu 18.04, installs OpenCV 4.1.0-dev and does not use python virtual environment. I haven't figured out how they work yet. I put the answer here so it has wider visibility.
[compiler] sudo apt-get install build-essential [required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev [optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
How to install jasper: https://github.com/opencv/opencv/issues/8622#issuecomment-353761271
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main" sudo apt update sudo apt install libjasper1 libjasper-dev
The installation will include contrib modules.
cd ~/<my_working_directory> git clone https://github.com/opencv/opencv.git git clone https://github.com/opencv/opencv_contrib.git
(Git branch to 3.4 to install that version.)
If you mess up, just delete build folder and start over from here.
mkdir build cd build
I use the following cmake flags:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
-DINSTALL_C_EXAMPLES=ON -DINSTALL_PYTHON_EXAMPLES=ON -DWITH_TBB=ON -DWITH_V4L=ON -DWITH_QT=ON -DWITH_OPENGL=ON -DWITH_NVCUVID=ON -DWITH_CUDA=ON -DWITH_OPENMP=ON -DWITH_IPP=ON -DWITH_OPENCL=ON
Ensure cmake finds your python version by checking its output for the following:
-- Python 2: -- Interpreter: /usr/bin/python2.7 (ver 2.7.15) -- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.15+) -- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3) -- install path: lib/python2.7/dist-packages/cv2/python-2.7 -- -- Python 3: -- Interpreter: /usr/bin/python3 (ver 3.6.8) -- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.8) -- numpy: /home/jx/.local/lib/python3.6/site-packages/numpy/core/include (ver 1.15.4) -- install path: lib/python3.6/dist-packages/cv2/python-3.6
Use make with the desired number of threads (ex. number of processors) then install.
make -j8 sudo make install
Things should now install to
If all went well then you should be able to import cv2 in python.
import cv2 cv2.__version__
OpenCV can be installed using pip/pip3 and homebrew. I've tried installing it before using pip3 (Python 3.6.0), but i had problems with VideoCapture, which requires the ffmpeg package. cv2.VideoCapture('videofilename'). This is why I used homebrew for installing OpenCV. Open a Terminal with Ctrl + Alt + T and type the following commands:
brew install ffmpeg brew install opencv3 --with-ffmpeg -v (Python 2.7) brew install opencv3 --with-python3 --with-ffmpeg -v (Python 3.6)