Compiling a basic OpenCV + Cuda program on linux

Asked
Active3 hr before
Viewed126 times

4 Answers

opencvcompilingbasic
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Stack Overflow en español, Meta Stack Overflow , Stack Overflow help chat

The sample code from OpenCV_GPU was successfully compiled on my machine through:

g++threshold.cpp - o threshold `pkg-config --cflags --libs opencv` - lopencv_gpu
88%

From there, we add in a few more optimizations, mainly around using cuBLAS, an implementation of the BLAS (Basic Linear Algebra Subprograms) library in the CUDA runtime.,Truth be told, I’ve already covered installing OpenCV on Ubuntu in many previous blog posts, but I’ll explain the process here as well. Overall, the instructions are near identical, but with a few important changes inside the cmake command, allowing us to compile OpenCV with CUDA support.,Have you tried installing OpenCV on Google compute engine IaaS platform? I was initially leaning on that route to leverage using TensorFlow.,I have not tried compiling OpenCV on any of Google’s services yet. I’ll consider that in the future.

Before we can compile OpenCV with CUDA support, we first need to install some prerequisites:

$ sudo apt - get install libjpeg8 - dev libtiff5 - dev libjasper - dev libpng12 - dev
$ sudo apt - get install libgtk2 .0 - dev
$ sudo apt - get install libavcodec - dev libavformat - dev libswscale - dev libv4l - dev
$ sudo apt - get install libatlas - base - dev gfortran
$ sudo apt - get install libhdf5 - serial - dev
$ sudo apt - get install python2 .7 - dev

If you’re a follower of the PyImageSearch blog, then you’ll also know that I’m a big fan of using pip , virtualenv , and virtualenvwrapper to create sequestered, independent Python virtual environments for each of our projects. You can install the virtual environment packages using the commands listed below (or you can skip this step if you already have Python virtual environments setup on your machine):

$ wget https: //bootstrap.pypa.io/get-pip.py
   $ sudo python get - pip.py
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm - rf get - pip.py~/.cache/pip

Next, let’s use update our ~/.bashrc file. Open this file using your favorite command line text editor (such as nano , vi , or emacs ):

$ nano~/.bashrc

Then, scroll down to the bottom of the file, append the following lines, and save + exit the editor:

# virtualenv and virtualenvwrapper
export WORKON_HOME = $HOME / .virtualenvs
source / usr / local / bin / virtualenvwrapper.sh

At this point, we can create our cv virtual environment:

$ source~/.bashrc
$ mkvirtualenv cv
$ pip install numpy

If you are indeed on an Amazon EC2 instance, be sure to change directory to /mnt and create a directory specifically for your OpenCV compile prior to downloading the source:

$ cd / mnt
$ sudo mkdir opencv_compile
$ sudo chown - R ubuntu opencv_compile
$ cd opencv_compile

For this tutorial, I’ll be using OpenCV 3.1. But you could also use OpenCV 2.4.X or OpenCV 3.0. Use the following commands to download the source:

$ wget - O opencv.zip https: //github.com/Itseez/opencv/archive/3.1.0.zip
   $ wget - O opencv_contrib.zip https: //github.com/Itseez/opencv_contrib/archive/3.1.0.zip
   $ unzip opencv.zip
$ unzip opencv_contrib.zip

We are now ready to use cmake to configure our build. Take special care when running this command, as I’m introducing some configuration variables you may not be familiar with:

$ cd opencv - 3.1 .0
$ mkdir build
$ cd build
$ cmake - D CMAKE_BUILD_TYPE = RELEASE\ -
   D CMAKE_INSTALL_PREFIX = /usr/local\ -
   D WITH_CUDA = ON\ -
   D ENABLE_FAST_MATH = 1\ -
   D CUDA_FAST_MATH = 1\ -
   D WITH_CUBLAS = 1\ -
   D INSTALL_PYTHON_EXAMPLES = ON\ -
   D OPENCV_EXTRA_MODULES_PATH = .. / .. / opencv_contrib - 3.1 .0 / modules\ -
   D BUILD_EXAMPLES = ON..

Provided that your own CMake command exited without error, you can now compile and install OpenCV:

$ make - j8
$ sudo make install
$ sudo ldconfig

Again, assuming your compile finished without error, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages . You can verify this using the ls command:

$ ls - l / usr / local / lib / python2 .7 / site - packages
total 2092
   -
   rw - r--r--1 root staff 2138812 Jun 2 14: 11 cv2.so

The last step is to sym-link the cv2.so file (our Python bindings) into the cv virtual environment:

$ cd~/.virtualenvs/cv / lib / python2 .7 / site - packages /
   $ ln - s / usr / local / lib / python2 .7 / site - packages / cv2.so cv2.so

To verify our installation, open up a new terminal, access the cv virtual environment using the workon command, fire up a Python shell, and then import OpenCV:

$ cd~
   $ workon cv
$ python
   >>>
   import cv2 >>>
   cv2.__version__ '3.1.0' >>>

Finally, now that OpenCV is installed, let’s perform a bit of cleanup and remove the source files used for installation:

$ cd / mnt
$ sudo rm - rf opencv_compile
load more v
72%

Let's consider a short test program:,The sample code from OpenCV_GPU was successfully compiled on my machine through:,Follow this link for further details and doubts: http://cgal-discuss.949826.n4.nabble.com/Compiling-a-basic-CGAL-program-in-C-on-Mac-OS-X-10-7-2-td4166413.html,In contrast, the driver API is harder to program but provided more control over how CUDA is used. The programmer has to directly deal with initialization, module loading, etc.

I've worked with opencv on linux in the past, but not with cuda. I've struggled with the following compilation error for months. And after trying many solutions i gave up and worked with windows. However, i really want to work on linux. This is the command i'm using to compile the threshold example given on the opencv_gpu website.

nvcc `pkg-config --libs opencv` - L. - L / usr / local / cuda / lib - lcuda - lcudart `pkg-config --cflags opencv` - I. - I / usr / local / cuda / include threshold.cpp - o threshold

here is the error:

/tmp/tmpxft_0000171b_00000000 - 1_ threshold.o: In
function `main':
threshold.cpp:(.text+0x124): undefined reference to `
cv::gpu::Stream::Null()
'
threshold.cpp: (.text + 0x156): undefined reference to `cv::gpu::threshold(cv::gpu::GpuMat const&, cv::gpu::GpuMat&, double, double, int, cv::gpu::Stream&)'
threshold.cpp:(.text+0x16d): undefined reference to `
cv::gpu::GpuMat::download(cv::Mat & ) const ' /
   tmp / tmpxft_0000171b_00000000 - 1_ threshold.o: In
function `cv::gpu::GpuMat::GpuMat(cv::Mat const&)':
threshold.cpp:(.text._ZN2cv3gpu6GpuMatC1ERKNS_3MatE[cv::gpu::GpuMat::GpuMat(cv::Mat const&)]+0x63): undefined reference to `
cv::gpu::GpuMat::upload(cv::Mat
   const & )
' /
tmp / tmpxft_0000171b_00000000 - 1_ threshold.o: In
function `cv::gpu::GpuMat::~GpuMat()':
threshold.cpp:(.text._ZN2cv3gpu6GpuMatD1Ev[cv::gpu::GpuMat::~GpuMat()]+0xd): undefined reference to `
cv::gpu::GpuMat::release()
'                                        
collect2: ld returned 1 exit status
make: ** * [all] Error 1
load more v
65%

To run the code with CUDA, we will do a simple addition to the C++ and Python code:,That’s it! Now you can implement the code using OpenCV library with DNN GPU Support.,To download it to the $HOME folder, simply run the following commands in the terminal:,I took this course because of the experts that were ahead of it and the availability to see the code implementations in both languages, C++ and Python.

sudo apt - get update
sudo apt - get upgrade
sudo apt - get install build - essential cmake unzip pkg - config
sudo apt - get install libjpeg - dev libpng - dev libtiff - dev
sudo apt - get install libavcodec - dev libavformat - dev libswscale - dev
sudo apt - get install libv4l - dev libxvidcore - dev libx264 - dev
sudo apt - get install libgtk - 3 - dev
sudo apt - get install libblas - dev liblapack - dev gfortran
sudo apt - get install python3 - dev
load more v

Other "opencv-compiling" queries related to "Compiling a basic OpenCV + Cuda program on linux"