|
| 1 | +Install OpenCV-Python in Ubuntu {#tutorial_py_setup_in_ubuntu} |
| 2 | +=============================== |
| 3 | + |
| 4 | +Goals |
| 5 | +----- |
| 6 | + |
| 7 | +In this tutorial We will learn to setup OpenCV-Python in Ubuntu System. |
| 8 | +Below steps are tested for Ubuntu 16.04 (64-bit) and Ubuntu 14.04 (32-bit). |
| 9 | + |
| 10 | +OpenCV-Python can be installed in Ubuntu in two ways: |
| 11 | +- Install from pre-built binaries available in Ubuntu repositories |
| 12 | +- Compile from the source. In this section, we will see both. |
| 13 | + |
| 14 | +Another important thing is the additional libraries required. |
| 15 | +OpenCV-Python requires only **Numpy** (in addition to other dependencies, which we will see later). |
| 16 | +But in this tutorials, we also use **Matplotlib** for some easy and nice plotting purposes (which I feel much better compared to OpenCV). |
| 17 | +Matplotlib is optional, but highly recommended. |
| 18 | +Similarly we will also see **IPython**, an Interactive Python Terminal, which is also highly recommended. |
| 19 | + |
| 20 | +Installing OpenCV-Python from Pre-built Binaries |
| 21 | +------------------------------------------------ |
| 22 | + |
| 23 | +This method serves best when using just for programming and developing OpenCV applications. |
| 24 | + |
| 25 | +Install package [python-opencv](https://packages.ubuntu.com/trusty/python-opencv) with following command in terminal (as root user). |
| 26 | + |
| 27 | +``` |
| 28 | +$ sudo apt-get install python-opencv |
| 29 | +``` |
| 30 | + |
| 31 | +Open Python IDLE (or IPython) and type following codes in Python terminal. |
| 32 | + |
| 33 | +``` |
| 34 | +import cv2 |
| 35 | +print(cv2.__version__) |
| 36 | +``` |
| 37 | + |
| 38 | +If the results are printed out without any errors, congratulations !!! |
| 39 | +You have installed OpenCV-Python successfully. |
| 40 | + |
| 41 | +It is quite easy. But there is a problem with this. |
| 42 | +Apt repositories may not contain the latest version of OpenCV always. |
| 43 | +For example, at the time of writing this tutorial, apt repository contains 2.4.8 while latest OpenCV version is 3.x. |
| 44 | +With respect to Python API, latest version will always contain much better support and latest bug fixes. |
| 45 | + |
| 46 | +So for getting latest source codes preference is next method, i.e. compiling from source. |
| 47 | +Also at some point in time, if you want to contribute to OpenCV, you will need this. |
| 48 | + |
| 49 | +Building OpenCV from source |
| 50 | +--------------------------- |
| 51 | + |
| 52 | +Compiling from source may seem a little complicated at first, but once you succeeded in it, there is nothing complicated. |
| 53 | + |
| 54 | +First we will install some dependencies. |
| 55 | +Some are required, some are optional. |
| 56 | +You can skip optional dependencies if you don't want. |
| 57 | + |
| 58 | +### Required build dependencies |
| 59 | + |
| 60 | +We need **CMake** to configure the installation, **GCC** for compilation, **Python-devel** and |
| 61 | +**Numpy** for building Python bindings etc. |
| 62 | + |
| 63 | +``` |
| 64 | +sudo apt-get install cmake |
| 65 | +sudo apt-get install python-devel numpy |
| 66 | +sudo apt-get install gcc gcc-c++ |
| 67 | +``` |
| 68 | + |
| 69 | +Next we need **GTK** support for GUI features, Camera support (libv4l), Media Support |
| 70 | +(ffmpeg, gstreamer) etc. |
| 71 | + |
| 72 | +``` |
| 73 | +sudo apt-get install gtk2-devel |
| 74 | +sudo apt-get install libv4l-devel |
| 75 | +sudo apt-get install ffmpeg-devel |
| 76 | +sudo apt-get install gstreamer-plugins-base-devel |
| 77 | +``` |
| 78 | + |
| 79 | +### Optional Dependencies |
| 80 | + |
| 81 | +Above dependencies are sufficient to install OpenCV in your Ubuntu machine. |
| 82 | +But depending upon your requirements, you may need some extra dependencies. |
| 83 | +A list of such optional dependencies are given below. You can either leave it or install it, your call :) |
| 84 | + |
| 85 | +OpenCV comes with supporting files for image formats like PNG, JPEG, JPEG2000, TIFF, WebP etc. |
| 86 | +But it may be a little old. |
| 87 | +If you want to get latest libraries, you can install development files for system libraries of these formats. |
| 88 | + |
| 89 | +``` |
| 90 | +sudo apt-get install libpng-devel |
| 91 | +sudo apt-get install libjpeg-turbo-devel |
| 92 | +sudo apt-get install jasper-devel |
| 93 | +sudo apt-get install openexr-devel |
| 94 | +sudo apt-get install libtiff-devel |
| 95 | +sudo apt-get install libwebp-devel |
| 96 | +``` |
| 97 | + |
| 98 | +### Downloading OpenCV |
| 99 | + |
| 100 | +To download the latest source from OpenCV's [GitHub Repository](https://github.com/opencv/opencv). |
| 101 | +(If you want to contribute to OpenCV choose this. For that, you need to install **Git** first) |
| 102 | + |
| 103 | +``` |
| 104 | +$ sudo apt-get install git |
| 105 | +$ git clone https://github.com/opencv/opencv.git |
| 106 | +``` |
| 107 | + |
| 108 | +It will create a folder "opencv" in current directory. |
| 109 | +The cloning may take some time depending upon your internet connection. |
| 110 | + |
| 111 | +Now open a terminal window and navigate to the downloaded "opencv" folder. |
| 112 | +Create a new "build" folder and navigate to it. |
| 113 | + |
| 114 | +``` |
| 115 | +$ mkdir build |
| 116 | +$ cd build |
| 117 | +``` |
| 118 | + |
| 119 | +### Configuring and Installing |
| 120 | + |
| 121 | +Now we have all the required dependencies, let's install OpenCV. |
| 122 | +Installation has to be configured with CMake. |
| 123 | +It specifies which modules are to be installed, installation path, which additional libraries to be used, whether documentation and examples to be compiled etc. |
| 124 | +Most of this work are done automatically with well configured default parameters. |
| 125 | + |
| 126 | +Below command is normally used for configuration of OpenCV library build (executed from build folder): |
| 127 | + |
| 128 | +``` |
| 129 | +$ cmake ../ |
| 130 | +``` |
| 131 | + |
| 132 | +OpenCV defaults assume "Release" build type and installation path is "/usr/local". |
| 133 | +For additional information about CMake options refer to OpenCV @ref tutorial_linux_install "C++ compilation guide": |
| 134 | + |
| 135 | +You should see these lines in your CMake output (they mean that Python is properly found): |
| 136 | + |
| 137 | +``` |
| 138 | +-- Python 2: |
| 139 | +-- Interpreter: /usr/bin/python2.7 (ver 2.7.6) |
| 140 | +-- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6) |
| 141 | +-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2) |
| 142 | +-- packages path: lib/python2.7/dist-packages |
| 143 | +-- |
| 144 | +-- Python 3: |
| 145 | +-- Interpreter: /usr/bin/python3.4 (ver 3.4.3) |
| 146 | +-- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3) |
| 147 | +-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2) |
| 148 | +-- packages path: lib/python3.4/dist-packages |
| 149 | +``` |
| 150 | + |
| 151 | +Now you build the files using "make" command and install it using "make install" command. |
| 152 | + |
| 153 | +``` |
| 154 | +$ make |
| 155 | +# sudo make install |
| 156 | +``` |
| 157 | + |
| 158 | +Installation is over. |
| 159 | +All files are installed in "/usr/local/" folder. |
| 160 | +Open a terminal and try import "cv2". |
| 161 | + |
| 162 | +``` |
| 163 | +import cv2 |
| 164 | +print(cv2.__version__) |
| 165 | +``` |
0 commit comments