ubuntu16.04下opencv3.3 GPU(CUDA)加速

前端之家收集整理的这篇文章主要介绍了ubuntu16.04下opencv3.3 GPU(CUDA)加速前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

虽然网上已经有一部分在ubuntu下配置opencv gpu加速的教程,但是随着opencv迭代到3.3.0,发现之前的教程或多或少都已经不管用了,也存在许多坑,这次配置的时基于最新的opencv3.3.0,写下这篇博文记录下配置的经过。

1.准备

1.安装好ubuntu16.04
2.安装配置好opencv3.3.0 配置教程
3.安装好cuda8.0
4.安装QT creator(可选)

2.编译

opencv的GPU加速需要用到cuda,故需要在此之前的基础上重新编译一次

  1. cd opencv-3.3.0
  2. mkdir my_build_dir_gpu
  3. cd my_build_dir_gpu
  1. 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 WITH_OPENGL=ON -D WITH_CUDA=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D WITH_CUBLAS=1 \..

当出现以上现象时表示已经cmake成功

接下来就是漫长的编译过程(我编译花了3个小时 - -),视cpu性能(我i7-6700hq - -)

  1. make
  2. sudo make install

3.配置.bashrc

  1. echo '/usr/local/lib' | sudo tee -a /etc/ld.so.conf.d/opencv.conf
  2. sudo ldconfig
  3. printf '# OpenCV\nPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig\nexport PKG_CONFIG_PATH\n' >> ~/.bashrc
  4. source ~/.bashrc

4.代码测试

通用CMakeLists.txt:

  1. # cmake needs this line
  2. cmake_minimum_required(VERSION 2.8)
  3.  
  4. # Define project name
  5. project(test)
  6. # Find OpenCV,you may need to set OpenCV_DIR variable
  7. # to the absolute path to the directory containing OpenCVConfig.cmake file
  8. # via the command line or GUI
  9. find_package(OpenCV 3.3.0 required)
  10.  
  11. # If the package has been found,several variables will
  12. # be set,you can find the full list with descriptions
  13. # in the OpenCVConfig.cmake file.
  14. # Print some message showing some of them
  15. message(STATUS "OpenCV library status:")
  16. message(STATUS " version: ${OpenCV_VERSION}")
  17. message(STATUS " libraries: ${OpenCV_LIBS}")
  18. message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
  19.  
  20. if(CMAKE_VERSION VERSION_LESS "2.8.11")
  21. # Add OpenCV headers location to your include paths
  22. include_directories(${OpenCV_INCLUDE_DIRS})
  23. endif()
  24.  
  25. # Declare the executable target built from your sources
  26. add_executable(opencv_example main.cpp)
  27.  
  28. # Link your application with OpenCV libraries
  29. target_link_libraries(opencv_example ${OpenCV_LIBS})

代码1

  1. using namespace std;
  2. #include "opencv2/opencv.hpp"
  3. #include "opencv2/gpu/gpu.hpp"
  4. using namespace cv;
  5.  
  6. int main()
  7. {
  8. int num_devices = cv::cuda::getCudaEnabledDeviceCount();
  9.  
  10. cout<<num_devices<<endl;
  11. }

如果安装成功:
则会显示出1来,若为0表示安装不成功


代码2

  1. using namespace std;
  2. #include "opencv2/opencv.hpp"
  3. #include "opencv2/gpu/gpu.hpp"
  4. using namespace cv;
  5.  
  6. int main()
  7. {
  8. int num_devices = cv::cuda::getCudaEnabledDeviceCount();
  9.  
  10. if(num_devices <= 0)
  11. {
  12. std::cerr<<"There is no device."<<std::endl;
  13. return -1;
  14. }
  15. int enable_device_id = -1;
  16. for(int i=0;i<num_devices;i++)
  17. {
  18. cv::cuda::DeviceInfo dev_info(i);
  19. if(dev_info.isCompatible())
  20. {
  21. enable_device_id=i;
  22. }
  23. }
  24. if(enable_device_id < 0)
  25. {
  26. std::cerr<<"GPU module isn't built for GPU"<<std::endl;
  27. return -1;
  28. }
  29. cv::cuda::setDevice(enable_device_id);
  30.  
  31. std::cout<<"GPU is ready,device ID is "<<num_devices<<"\n";
  32.  
  33. VideoCapture cap(0);
  34. Mat frame;
  35. Mat dst_image;
  36. while(1)
  37. {
  38. cap>>frame;
  39. cuda::GpuMat d_src_img(frame);
  40. cuda::GpuMat d_dst_img;
  41. cuda::cvtColor(d_src_img,d_dst_img,CV_BGR2GRAY);
  42. d_dst_img.download(dst_image);
  43. imshow("test",dst_image);
  44. waitKey(1);
  45. }
  46. return 0;
  47. }

代码效果
显示出灰度的摄像头图像

这个时候我们可以调用

  1. nvidia-smi

可以看到该程序使用了GPU资源

5.可能出现的问题

1.报错

  1. /usr/include/opencv2/gpu/gpu.hpp:432: error: 'vector' does not name a type
  2. CV_EXPORTS void merge(const vector<GpuMat>& src,GpuMat& dst,Stream& stream = Stream::Null());

解决办法:把 using namespace std放到最最上面即可。


2.提示未定义gpu::getCudaEnabledDeviceCount()

  1. /home/xukeqin/code/cpp/gpu_test/main.cpp:-1: error: undefined reference to `cv::gpu::getCudaEnabledDeviceCount()'

解决办法:opencv3.2.0中的gpu模块有比较大的变化,原来的gpu换成了现在的cuda,故改为cuda::getCudaEnabledDeviceCount()即可!

猜你在找的Ubuntu相关文章