OpenCV_GPU
Contents
The OpenCV GPU module is a set of classes and functions to utilize GPU computational capabilities. It is implemented using NVidia CUDA Runtime API , so only that vendor GPUs are supported. It includes utility functions, low level vision primitives as well as high level algorithms. I.e. the module is being developed as a powerful infrastructure for fast vision algorithms building on GPU with some high level state of the art functionality.
Request Feature
OpenCV GPU Feature Request form
Documentation
Documentation is hosted at Itseez site (regenerated each night). There are a lot of tutorials for beginners.
In PDF format (slightly rarely updated) is available in SVN: https://code.ros.org/svn/opencv/trunk/opencv/doc/.
Getting Started
Compiling OpenCV with CUDA Support
Prerequisites:
Sources from SVN or SourceForge
- NVIDIA Display Driver
CMake (minimum 2.8.3)
Cuda Toolkit (the latest release)
Build steps:
Checkout the latest OpenCV from SVN: https://code.ros.org/svn/opencv/trunk/opencv.
- Open CMake-Gui
- Set source and build folders
- Source folder is a folder with OpenCV sources. It contains root CMakeLists.txt file.
- Build folder is arbitrary folder on you disk, where you want to create makefiles/projects and build OpenCV.
- Set WITH_CUDA flag
(Tip) If you use Visual Studio Prof. or higher, you can set USE_SOLUTION_FOLDERS flag for more convenient use (projects will be organized in folders)
(Tip) Look at other parameters in Cake-Gui to familiarize yourself with it. You might want to tweak another settings.
- Configure for your favorite IDE or for make files.
- If you want to generate for NMake, please run CMake-Gui from Visual Studio Command Prompt.
If CMake can't find "FindCuda.cmake", please set environment variable CMAKE_MODULE_PATH = <path to location of FindCUDA.cmake>
Check that all CUDA paths are detected correctly. If not, specify CUDA_TOOLKIT_ROOT_DIR manually.
- Generate projects for your favorite IDE.
(Optional) You can reduce build time by setting target architecture variables (in order to build only for your GPU)
CUDA_ARCH_BIN = "X.X", where XX is your GPU Compute Capability
- CUDA_ARCH_PTX = "empty"
(Optional) To enable samples compilation, set BUILD_SAMPLES flag in Cmake-Gui.
- Set source and build folders
- Open and build projects generated, or run make.
Adding OpenCV to your project via Cmake
If you use Cmake for your project, adding opencv libraries to it is a very easy.
- Build OpenCV
- Modify your "CMakeLists.txt" file as in the sample below
- On "Configure" stage for you application, Cmake-Gui finds OpenCV automatically in last build place.
- If it can't find, specify OpenCV_Dir variable to OpenCV build directory (a place where OpenCVConfig.cmake is located)
cmake_minimum_required(VERSION 2.8)
set(the_target YourProjName}
set(src "main.cpp" "other.cpp")
project( ${the_target} )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIR} )
add_executable( ${the_target} ${src} )
target_link_libraries( ${the_target} ${OpenCV_LIBS} )
Warning!!! First Function Call Perfomance
While using OpenCV GPU the following specific should be taken into consideration:
- GPU module uses CUDA Runtime API which runs initialization code on the first function call.
- Just-in-time compilation of GPU code can be run on the first function call if the GPU module wasn't built for the current GPU.
Therefore first call of many functions may be quite slow. So for performance measuring, it is necessary to do dummy function call and only then perform time tests. If it is critical for an application to run GPU code only once, it is possible to use a compilation cache which is persistent over multiple runs. Please read nvcc documentation for details (CUDA_DEVCODE_CACHE environment variable).
Short Sample
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/gpu/gpu.hpp"
int main (int argc, char* argv[])
{
try
{
cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
cv::gpu::GpuMat dst, src;
src.upload(src_host);
cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
cv::Mat result_host = dst;
cv::imshow("Result", result_host);
cv::waitKey();
}
catch(const cv::Exception& ex)
{
std::cout << "Error: " << ex.what() << std::endl;
}
return 0;
}
Asking questions
Send your question to OpenCV Yahoo Group using recommendations below:
Place [GPU] or [CUDA] in question's topic (OpenCV GPU team can't read the group entirely).
- In case of some strange problems, specify your GPU, OS, Cuda Toolkit, NPP, message from caught cv::Exception (if it is), etc.
List of Ported Functions
Initialization & information (getCudaEnabledDeviceCount, setDevice, getDevice, isCompatibleWith, freeMemory, totalMemory, getComputeCapability).
Per-element operations with/without mask (add, subtract, multiply, divide, exp, log, absdiff, compare, bitwise_not, bitwise_or, bitwise_and, bitwise_xor, bitwise_xor, min, minS, max, maxS).
Color conversion (almost all color spaces, cvtColor).
Geometrical image transforms (rotate, resize, warpAffine, warpPerspective, remap, transpose)
Mean shift-based transforms (meanShiftFiltering, meanShiftSegmentation)
Corner detectors (cornerHarris, cornerMinEigenVal)
Reductions with/without mask ( meanStdDev, norm, sum, sqrSum, minMax, minMaxLoc, countNonZero, integral, sqrIntegral, columnSum, rectStdDev)
Template matching (matchTemplate)
Filter engine (low-level filter classes, middle-level filtering engines, and high level filtering functions: boxFilter, blur, erode, dilate, morphologyEx, filter2D, sepFilter2D, Sobel, Scharr, GaussianBlur, Laplacian)
Histograms (evenLevels, histEven, histRange)
Stereo correspondence (StereoBlockMatching, StereoBeliefPropagation, StereoConstantSpaceBP, DisparityBilateralFilter, drawColorDisp)
Pedestrian detection (Histograms of oriented gradients (HOG) descriptor)
Universal descriptor matcher (BruteForceMatcher)
Haar features detection, face detection (CascadeClassifier based on code contributed by Anton Obukhov, NVidia)
Feature detection for object recognition ( Speeded Up Robust Features, SURF )
Other image transforms (flip, LUT, merge, split, magnitude, magnitudeSqr, phase, cartToPolar, polarToCart, reprojectImageTo3D, graphcut, threshold, copyMakeBorder, dft, convolve, mulSpectrums, mulAndScaleSpectrums)
Frequently Asked Questions
FAQ list is available here: OpenCV GPU FAQ.