---
# System prepended metadata

title: Install OPENCV3 with CUDA
tags: [system]

---

# Install OPENCV3 with CUDA
1. `touch install_opencv.sh`
2. `nano install_opencv.sh`
3. select version of opencv from [opencv github](https://github.com/opencv/opencv/releases) 
4. select cuda by typing `nvcc --version`

copy all following code

```
OPENCV_VERSION='3.4.10'
CUDA_VERSION='10.2'
sudo apt-get -y update
sudo apt-get install -y python-numpy python3-numpy
sudo apt-get install -y build-essential cmake git pkg-config gfortran libatlas-base-dev
sudo apt-get install -y libjpeg-dev libpng-dev libtiff-dev libjasper-dev libgtk2.0-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev v4l-utils
sudo apt-get install -y unzip wget git
wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip -O opencv-${OPENCV_VERSION}.zip
unzip opencv-${OPENCV_VERSION}.zip
rm opencv-${OPENCV_VERSION}.zip
wget https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip -O opencv_contrib-${OPENCV_VERSION}.zip
unzip opencv_contrib-${OPENCV_VERSION}.zip
rm opencv_contrib-${OPENCV_VERSION}.zip
mkdir ~/opencv-${OPENCV_VERSION}/build
cd ~/opencv-${OPENCV_VERSION}/build
cmake -D WITH_TBB=ON \
      -D WITH_OPENMP=ON \
      -D WITH_IPP=ON \
      -D CMAKE_BUILD_TYPE=RELEASE \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-${OPENCV_VERSION}/modules \
      -D BUILD_EXAMPLES=OFF \
      -D WITH_NVCUVID=ON \
      -D WITH_CUDA=ON \
      -D WITH_CUBLAS=ON \
      -D BUILD_DOCS=OFF \
      -D BUILD_opencv_cudacodec=OFF \
      -D BUILD_PERF_TESTS=OFF \
      -D BUILD_TESTS=OFF \
      -D WITH_CSTRIPES=ON \
      -D BUILD_opencv_python2=ON \
      -D BUILD_opencv_python3=ON \
      -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
      -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-${CUDA_VERSION} \
      -D WITH_V4L=ON \
      -D WITH_OPENCL=ON \
      CMAKE_INSTALL_PREFIX=/usr/local/ .. 
make -j12
sudo make install
sudo ldconfig
```
3. `chmod +x install_opencv.sh`
4. `./install_opencv.sh`


---
### After installation finished
1. `cd /opt/ros/kinetic/lib/python2.7/dist-packages`
2. `sudo rm cv2.so`
3. `sudo ldconfig`
4. pkg-config --cflags opencv
5. pkg-config --libs opencv


---
### Uninstall opencv cleanly
```
1. cd ~/opencv-4.1.1/build
2. sudo make uninstall
3. sudo rm /usr/local/{bin,lib}/*opencv*     
4. sudo find / -name "*opencv*" -exec rm -rf {} \;
```


---
### References

1. http://amritamaz.net/blog/opencv-config
`usefull cmake opencv`