Building opencv with Nvidia CUDA 8.0 extensions on Ubuntu 16.04

cuda

Note: Building opencv without CUDA you just need to following the following blog:

https://aichamp.wordpress.com/2016/11/11/compiling-opencv-in-ubuntu-16-04-with-gcc-4-9/

Here is the opencv libs without CUDA/gpu:

$ pkg-config –libs opencv

-L/usr/local/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core

Get NVidia CUDA installed

Getting Source:

Get opencv-master and opencv_contrib from git and keep them in the same path:

Build Process (Step 1) – Config Build:

  • $ mkdir build;cd build
  • cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/mnt/avkash/opencv_contrib/modules -D BUILD_EXAMPLES=ON -DCMAKE_C_COMPILER=gcc-4.9 -DCMAKE_CXX_COMPILER=g++-4.9 -DCUDA_CUDA_LIBRARY=/usr/local/cuda -DWITH_CUDA=ON -DENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_OPENGL=ON  ..

Note: In above step if CUDA configuration is correct you will see the following

  • CUDA detected: 8.0
— CUDA NVCC target flags: -gencode;arch=compute_20,code=sm_20;-gencode;arch=compute_30,code=sm_30;-gencode;arch=compute_35,code=sm_35;-gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_52,code=sm_52;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-D_FORCE_INLINES

Error if CUDA Path are not set correctly:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_CUDA_LIBRARY (ADVANCED)

Verify CUDA path (-DCUDA_CUDA_LIBRARY=/usr/local/cuda)

Build process Step 2 – Making code:

  • Make sure you are in opencv-master/build folder and step-1 was successful.
  • $ make -j8

Note: It will take about 1-2 hours and about 10GB space so make sure you have enough patience and space 🙂

Possible Errors:

[One] If you build fail due to “opencv_cudaimgproc.dir” missing error

make[2]: *** [modules/cudaimgproc/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_gftt.cu.o] Error 1
make[2]: *** Waiting for unfinished jobs….
Scanning dependencies of target opencv_cudafeatures2d
[ 72%] Building CXX object modules/cudafeatures2d/CMakeFiles/opencv_cudafeatures2d.dir/src/orb.cpp.o
[ 72%] Building CXX object modules/cudafeatures2d/CMakeFiles/opencv_cudafeatures2d.dir/src/feature2d_async.cpp.o
[ 72%] Building CXX object modules/cudafeatures2d/CMakeFiles/opencv_cudafeatures2d.dir/src/brute_force_matcher.cpp.o
[ 72%] Building CXX object modules/cudafeatures2d/CMakeFiles/opencv_cudafeatures2d.dir/src/fast.cpp.o
[ 72%] Linking CXX shared library ../../lib/libopencv_cudafeatures2d.so
[ 72%] Built target opencv_cudafeatures2d
Scanning dependencies of target opencv_test_cudafeatures2d
Scanning dependencies of target opencv_perf_cudafeatures2d
[ 72%] Building CXX object modules/cudafeatures2d/CMakeFiles/opencv_test_cudafeatures2d.dir/test/test_features2d.cpp.o
[ 72%] Building CXX object modules/cudafeatures2d/CMakeFiles/opencv_test_cudafeatures2d.dir/test/test_main.cpp.o
[ 72%] Building CXX object modules/cudafeatures2d/CMakeFiles/opencv_perf_cudafeatures2d.dir/perf/perf_features2d.cpp.o
[ 72%] Building CXX object modules/cudafeatures2d/CMakeFiles/opencv_perf_cudafeatures2d.dir/perf/perf_main.cpp.o
[ 72%] Linking CXX executable ../../bin/opencv_perf_cudafeatures2d
CMakeFiles/Makefile2:3590: recipe for target ‘modules/cudaimgproc/CMakeFiles/opencv_cudaimgproc.dir/all’ failed
make[1]: *** [modules/cudaimgproc/CMakeFiles/opencv_cudaimgproc.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs….
[ 72%] Built target opencv_perf_cudafeatures2d
[ 72%] Linking CXX executable ../../bin/opencv_test_cudafeatures2d
[ 72%] Built target opencv_test_cudafeatures2d
Makefile:160: recipe for target ‘all’ failed
make: *** [all] Error 2

Solution:

  • Visit:https://github.com/opencv/opencv/issues/6632
  • Steps:
    • $ git clone https://github.com/thrust/thrust.git
    • $ cp -r thrust/thrust /usr/local/cuda/include
    • We are update above thrust files with files included into cuda/include folder
  • Rebuild code again
    • $ make -j8

[Two] fatal error: stdlib.h: No such file or directory

you may get the error with cmake step as below:

/usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: No such file or directory
#include_next <stdlib.h>
^
compilation terminated.

Solution:  Add the following parameter with cmake builder:

-DENABLE_PRECOMPILED_HEADERS=OFF

Updated cmake command as below:

  • cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/mnt/avkash/opencv_contrib/modules -D BUILD_EXAMPLES=ON -DCMAKE_C_COMPILER=gcc-4.9 -DCMAKE_CXX_COMPILER=g++-4.9 -DCUDA_CUDA_LIBRARY=/usr/local/cuda -DWITH_CUDA=ON -DENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_OPENGL=ON  -DENABLE_PRECOMPILED_HEADERS=OFF ..

Success Story:

[100%] Built target tutorial_imageSegmentation
[100%] Linking CXX executable ../../bin/cpp-tutorial-pnp_registration
[100%] Built target cpp-tutorial-pnp_registration
[100%] Linking CXX executable ../../bin/cpp-example-stitching_detailed
[100%] Built target example_stitching_detailed
[100%] Linking CXX shared module ../../lib/cv2.so
[100%] Built target opencv_python2

Build installer:

opencv-master/build$ sudo make install

Test OpenCV with CUDA in Python:

  • >>> import cv2
  • >>> print(cv2.__version__)
  • >>>print(cv2.cuda)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s