OpenCV 3.4.0 Installation

物体検出

OpenCV installation byitself is actually not difficult, but installing it together with CUDA is quite hard for people who are new to Ubuntu or OpenCV installation.

Installation

If you just want openCV without CUDA support, open terminal

sudo apt-get install python-opencv

If you want it to work with CUDA and for Object Detection, well here we go

Resources

CUDA 9.0 Toolkit already installed

Linux operating system (any should work)

Compiling OpenCV from Source

I will be installing OpenCV 3.4.0 for it to work with CUDA. However, installation of other versions of openCV will be the same too, the only difference is what files that you download

First, install the necessary reprequisite for installing OpenCv

Run the following one by one

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavdiv class="code"c-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
sudo apt-get install python2.7-dev python3.5-dev
sudo apt-get install python-dev python-numpy
sudo apt-get install gcc gcc-c++
sudo apt-get install gtk2.0
sudo apt-get install libv4l-0

Next, download the desired OpenCV version and its contrib repository

OpenCV 3.4.0

cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.4.0.zip
unzip opencv.zip

OpenCV 3.4.0 contrib

wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.4.0.zip
unzip opencv_contrib.zip

Latest OpenCV

cd ~
wget -O opencv.zip https://github.com/opencv/opencv
unzip opencv.zip

Lates OpenCV Contrib

wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib
unzip opencv_contrib.zip

After unzipping it, go into opencv folder and make a new folder called "build"

cd opencv
mkdir build
cd build

Then build the config file! Choose accordingly and run it in the build folder

Compile with OpenCV_contrib

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON \
..

Compile with OpenCV_contrib and CUDA

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
..
NOTE: Change the "OPENCV_EXTRA_MODULES_PATH" to the appropriate path to OpenCV_contrib

Basically, these are the parameter you can use to compile OpenCV to your liking. If you want to compile it with CUDA(which is what you need for ObjDectection) then choose the appropriate parameter above. Regardless of how you edit the parameter, add a ".." at the end of the command, it is to point to the root OpenCV folder.

NOTE: ".." means go back the directory path by one

After the config file is done (Could take quite a while), run the following

#change "4" with the number of cores you want to compile it with
make -j4

When it is done compiling, install it

make install

And you are done!

Testing

For testing to see if installation is complete run the following in python

import cv2 as cv
print(cv.__version__)

It should return the version of OpenCV that you installed