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,故需要在此之前的基础上重新编译一次

cd opencv-3.3.0   
mkdir my_build_dir_gpu   
cd my_build_dir_gpu
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 - -)

make
sudo make install

3.配置.bashrc

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

4.代码测试

通用CMakeLists.txt:

# cmake needs this line
cmake_minimum_required(VERSION 2.8)

# Define project name
project(test)
# Find OpenCV,you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV 3.3.0 required)

# If the package has been found,several variables will
# be set,you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")

if(CMAKE_VERSION VERSION_LESS "2.8.11")
  # Add OpenCV headers location to your include paths
  include_directories(${OpenCV_INCLUDE_DIRS})
endif()

# Declare the executable target built from your sources
add_executable(opencv_example main.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example ${OpenCV_LIBS})

代码1

using namespace std;
#include "opencv2/opencv.hpp"
#include "opencv2/gpu/gpu.hpp"
using namespace cv;

int main()
{
    int num_devices = cv::cuda::getCudaEnabledDeviceCount();

    cout<<num_devices<<endl;
}

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


代码2

using namespace std;
#include "opencv2/opencv.hpp"
#include "opencv2/gpu/gpu.hpp"
using namespace cv;

int main()
{
    int num_devices = cv::cuda::getCudaEnabledDeviceCount();

    if(num_devices <= 0)
    {
        std::cerr<<"There is no device."<<std::endl;
        return -1;
    }
    int enable_device_id = -1;
    for(int i=0;i<num_devices;i++)
    {
        cv::cuda::DeviceInfo dev_info(i);
        if(dev_info.isCompatible())
        {
            enable_device_id=i;
        }
    }
    if(enable_device_id < 0)
    {
        std::cerr<<"GPU module isn't built for GPU"<<std::endl;
        return -1;
    }
    cv::cuda::setDevice(enable_device_id);

    std::cout<<"GPU is ready,device ID is "<<num_devices<<"\n";

    VideoCapture cap(0);
    Mat frame;
    Mat dst_image;
    while(1)
    {
        cap>>frame;
        cuda::GpuMat d_src_img(frame);
        cuda::GpuMat d_dst_img;
        cuda::cvtColor(d_src_img,d_dst_img,CV_BGR2GRAY);
        d_dst_img.download(dst_image);
        imshow("test",dst_image);
        waitKey(1);
    }
    return 0;
}

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

这个时候我们可以调用

nvidia-smi

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

5.可能出现的问题

1.报错

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

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


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

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

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

原文链接:https://www.f2er.com/ubuntu/350757.html

猜你在找的Ubuntu相关文章