26

I want to install OpenCV for python3 in ubuntu 16.04. Fist I tried running sudo apt-get install python3-opencv which is how I pretty much install all of my python software. This could not find a repository. The install does work however if I do sudo apt-get install python-opencv this issue with this is that by not adding the three to python it installs for python 2 which I do not use. I would really perfer not to have to build and install from source so is there a way I can get a repository? I also tried installing it with pip3 and it could not find it either.

chasep255
  • 11,745
  • 8
  • 58
  • 115
  • I don't think you can install opencv on python 3.x directly the way you would for python 2.x. You should follow this short [guide](http://www.pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu/) or [this](https://github.com/rainyear/lolita/issues/18) to help you get through an installation from git – Moses Koledoye May 12 '16 at 14:09
  • Ok just wanted to make sure. I am doing the source install right now. – chasep255 May 12 '16 at 14:10

9 Answers9

47

Well this will be a lengthy answer, so let's start :

Step 1: Install prerequisites : Upgrade any pre-installed packages:

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

Install developer tools used to compile OpenCV 3.0:

$ sudo apt-get install build-essential cmake git pkg-config

Install libraries and packages used to read various image and videos formats from disk:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

Install GTK so we can use OpenCV’s GUI features:

$ sudo apt-get install libgtk2.0-dev

Install packages that are used to optimize various functions inside OpenCV, such as matrix operations:

$ sudo apt-get install libatlas-base-dev gfortran

Step 2: Setup Python (Part 1)

Let’s download pip , a Python package manager, installed for Python 3:

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py

Let’s use our fresh pip3 install to setup virtualenv and virtualenvwrapper :

$ sudo pip3 install virtualenv virtualenvwrapper

Now we can update our ~/.bashrc file (place at the bottom of the file):

# virtualenv and virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
$ source ~/.bashrc
$ mkvirtualenv cv

Step 2: Setup Python (Part 2)

we’ll need to install the Python 3.4+ headers and development files:

$ sudo apt-get install python3.4-dev

OpenCV represents images as NumPy arrays, so we need to install NumPy into our cv virtual environment:

$ pip install numpy

Step 3: Build and install OpenCV 3.0 with Python 3.4+ bindings

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

Time to setup the build:

$ cd ~/opencv
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_C_EXAMPLES=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..

Let's start OpenCV compile process :

$ make -j4

Assuming OpenCV 3.0 compiled without error, you can now install it on your system:

$ sudo make install
$ sudo ldconfig

Step 4: Sym-link OpenCV 3.0

If you’ve reached this step, OpenCV 3.0 should now be installed in /usr/local/lib/python3.4/site-packages/.

Here, our OpenCV bindings are stored under the name cv2.cpython-34m.so

However, in order to use OpenCV 3.0 within our cv virtual environment, we first need to sym-link OpenCV into the site-packages directory of the cv environment, like this: (Be sure to take note of cv2.cpython-34m.so)

$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so cv2.so

Notice how I am changing the name from cv2.cpython-34m.so to cv2.so — this is so Python can import our OpenCV bindings using the name cv2 .

Step 5: Test out the OpenCV 3.0 and Python 3.4+ install

$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.0.0'

Hope that helps. Also, credit to Adrian Rosebrock on his post. It worked for me as a charm.

Seraf
  • 850
  • 1
  • 17
  • 34
Vtik
  • 3,073
  • 2
  • 23
  • 38
  • So I installed it and it seems to be working. The only issue I am having is that PyDev in eclipse does not seem to be able to find cv2. I can import cv2 and run it fine but I find it annoying that I can't use the auto-complete ctrl+Space feature in eclipse while coding. I looked into the `/usr/local/lib/python3.5/dist-packages` and noticed that there was only a .so file for cv2 rather than a folder containing python files like the other libraries in the package. Is there any way to get python files for cv2 so it will integrate nicely with eclipse? – chasep255 May 12 '16 at 15:51
  • it looks like solved [here](http://stackoverflow.com/questions/6070423/adding-python-modules-to-pydev-in-eclipse-results-in-import-error), also, please mark your question as answered if the answer did solve your original question. Thanks. – Vtik May 12 '16 at 15:54
  • Well I actually got it to work by adding cv2 to forced built-ins in pydev. – chasep255 May 12 '16 at 15:56
  • 2
    This installation is incompatible with cuda 8. Use latest cv2 repo if you have cuda 8. – Cartesian Theater Dec 24 '16 at 20:24
  • I did this for CUDA 8 and have this issue for Ubuntu 14.04 https://github.com/opencv/opencv/issues/8036 Please have a look – Mona Jalal Jan 19 '17 at 02:07
  • Thank you. I worked for me. But I had to replace _$ ln -s /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so cv2.so_ with _$ ln -s /usr/local/lib/python3.4/dist-packages/cv2.cpython-34m.so cv2.so_ to make it work. – Renaud May 23 '17 at 15:45
  • tried 'make -j1' instead of 'make -j4'. It shows the cores. j1 was working for me but not j4 – bibinwilson Feb 23 '19 at 07:52
43

I found this:

https://pypi.python.org/pypi/opencv-python

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'

Not sure why this wasn't mentioned. Perhaps it is newly available?

jrbedard
  • 3,662
  • 5
  • 30
  • 34
Trevor
  • 594
  • 5
  • 6
5

Assuming that you installed Python3.x, I resolved it using the following:

1: Install side packages required for OpenCV with Ubuntu (only validated for: Ubuntu 16.04):

apt-get update
apt-get install -y libglib2.0.0 libsm6
apt-get install libxext6
apt-get install -y libxrender-dev

2: Install OpenCV on python3.x:

pip3 install opencv-contrib-python
Robin
  • 171
  • 1
  • 6
3

Using conda inside a python3 environment:

First install conda in a python3 environment and activate it if you haven't yet:

conda create --name py3k python=3
source activate py3k

Now you can install opencv in the conda environment:

pip install pillow
conda install -c menpo opencv3=3.1.0

To import in Python:

import cv2
k26dr
  • 1,229
  • 18
  • 15
  • 1
    I have been trying different solutions for hours. I kept getting an error with cv2.imshow(). Now I've tried this and it finally works. Thank you! – elevendollar Mar 10 '17 at 16:04
3

Ubuntu 18.04 and later

In Ubuntu 18.04 and later Python 3 bindings for the OpenCV (Open Computer Vision) library can be installed from the default Ubuntu repositories with the following command:

sudo apt install python3-opencv  

The Open Computer Vision Library is a collection of algorithms and sample code for various computer vision problems. The library is compatible with IPL (Intel's Image Processing Library) and, if available, can use IPP (Intel's Integrated Performance Primitives) for better performance.

karel
  • 5,489
  • 46
  • 45
  • 50
2

The popular suggested solution worked miracles for me, where sudo apt install of opencv failed to install properly. Another note on step 4 (which may make life easier for others). It seems simple now but it took me quite a while to troubleshoot (because I am new to this).

Step 4a: Run the following code to find out where the cv2 file is installed. Your filename may also be slightly different. Just because you can't find it where the OP said it would be, that doesn't mean the installation did not work.

> $ find /usr/ -iname cv2.*
> /usr/lib/python3/dist-packages/cv2.cpython-38-x86_64-linux-gnu.so

Step 4b: SymLink OpenCV 3.0 as above

> $ cd ~/anaconda3/virtualenv/lib/python3.8/site-packages/
> $ ln -s /usr/lib/python3/dist-packages/cv2.cpython-38-x86_64-linux-gnu.so cv2.so
Fiza
  • 21
  • 2
1

This is because you have multiple installations of python in your machine.You should make python3 the default, because by default is the python2.7

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
makoulis
  • 33
  • 5
  • Well I don't think that is the issue. Why can't I just install it for python3 like I have done with every other library. – chasep255 May 12 '16 at 13:59
  • 1
    You just need to set the default python in cmake/OpenCVDetectPython.cmake: set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}") – Joe Chakra Sep 29 '17 at 02:27
1

After ubuntu 18.04, use

sudo apt install python3-opencv

Careful, autostart Anaconda enviroment may cover normal apt installation, which took me hours to find. put it down here in case some newbie like me find it not working.

tothedistance
  • 161
  • 2
  • 4
0
sudo pip3 install opencv-python opencv-contrib-python
Js541
  • 100
  • 1
  • 13