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
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 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
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.4.0.zip
unzip opencv.zip
OpenCV 3.4.0 contrib
unzip opencv_contrib.zip
Latest OpenCV
wget -O opencv.zip https://github.com/opencv/opencv
unzip opencv.zip
Lates OpenCV Contrib
After unzipping it, go into opencv folder and make a new folder called "build"
mkdir build
cd build
Then build the config file! Choose accordingly and run it in the build folder
Compile with OpenCV_contrib
-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
-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 \
..
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.
After the config file is done (Could take quite a while), run the following
make -j4
When it is done compiling, install it
And you are done!
Testing
For testing to see if installation is complete run the following in python
print(cv.__version__)
It should return the version of OpenCV that you installed