I have an Ubuntu 14.04 system, on which I want to install OpenCV and use it with Python 2.x.
I installed OpenCV using the instructions here: https://help.ubuntu.com/
Try conda install -c conda-forge opencv
if you are using anaconda, it works!
You can build for source following the official OpenCV tutorial. The crucial part is to set the PYTHON3_EXECUTABLE
, PYTHON_LIBRARY
, PYTHON3_PACKAGES_PATH
and PYTHON3_NUMPY_INCLUDE_DIRS
parameters for python3.6
. Here are all the steps:
Clone the repo
git clone https://github.com/opencv/opencv.git
Create build
directory
cd ~/opencv
mkdir build
cd build
Configure
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local .. \
-D PYTHON_INCLUDE_DIR=/usr/include/python3.6 \
-D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.6m \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.6 \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.6 \
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so \
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages .. \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/user/.local/lib/python3.6/site-packages/numpy/core/include/
Build
make -j8
Install libraries
sudo make install
Test
python3
import cv2
If you don't get the error "No module named cv2", then the installation was successful.
Note: If you don't know the path to numpy
for the PYTHON3_NUMPY_INCLUDE_DIRS
parameter, you can find it by executing import numpy
and then numpy.__file__
in a python3 shell.
I found a solution in the guide here:
http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/
I resorted to compiling and installing from source. The process was very smooth, had I known, I would have started with that instead of trying to find a more simple way to install. Hopefully this information is helpful to someone.
Try using:
from cv2 import cv
It works for me.
If you want as simple as possible, install from the repository:
sudo apt-get install python-opencv libopencv-dev python-numpy python-dev
My environment:
Since none of the previous answers worked for me, I downloaded OpenCV 3.0 from http://opencv.org/downloads.html and followed the installation manual. I used the following cmake
command:
$ ~/Programs/opencv-3.0.0$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_EXECUTABLE=/usr/bin/python3.5 -D PYTHON_INCLUDE_DIR=/usr/include/python3.5 -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.5m -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages ..
Each step of the tutorial is important. Particularly, don't forget to call sudo make install
.