http://www.python36.com/how-to-install-opencv340-on-ubuntu1604/
Step #1:stepUpdate and Upgrade and Cleanup (Required for error-free installation)
$ sudo apt-get update$ sudo apt-get upgrade
Step #2:Install Dependencies
$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install libgtk-3-dev
$ sudo apt-get install libatlas-base-dev gfortran pylint
(*)have new
$ sudo apt-get install python2.7-dev python3.5-dev
Step #2:Download and Build OpenCV
$ wget https://github.com/opencv/opencv/archive/3.4.0.zip -O opencv-3.4.0.zip
$ wget https://github.com/opencv/opencv_contrib/archive/3.4.0.zip -O opencv_contrib-3.4.0.zip
$ sudo apt-get install unzip
$ unzip opencv-3.4.0.zip
$ unzip opencv_contrib-3.4.0.zip
$ cd opencv-3.4.0
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -DOPENCV_ENABLE_NONFREE=True ..
$ make -j4 (where 4 is number of cores)
$ sudo make install
$ sudo ldconfig
Check if the OpenCV file exists
python2
“/usr/local/lib/python2.7/dist-packages/cv2.so”
python3.5
“/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so”
python3.6
“/usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so”
To use OpenCV 3.4.0 in virtual environment like virtualenv, conda or any other, copy related .so file in your virtual environment python directory
“lib/site-packages/”
Finally, check OpenCV version. Go to the terminal and typepython
On the python terminal, type
import cv2
cv2.__version__
If you ran into any problem while installing OpenCV or have any query related to OpenCV then feel free to comment below. I will try my best to solve it.