在opencv的cuda加速版时候会将cuda的一个libcudart.so.8.0加入到opencv的lib中,所以如果你以后升级你的cuda,那么opencv需要卸载后重新编译才能使用,毕竟cuda变了,原来的libcudart.so.8.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.代码测试
首先新建文件夹test,然后在test中新建文件CMakeLists.txt和main.cpp(文件内的内容看下面的代码),然后cd到test目录下,假设文件夹test的目录是/home/test,那么就使用命令
cd /home/test
接着使用命令
cmake .
如果没有cmake,请自行百度安装。这是会出现好几个文件,当中会有一个makefile,接着使用命令
make
就会有一个名为"opencv_example"的可执行文件,最后使用命令
./opencv_example
如果opencv安装成功,代码编译成功:
则会显示出1来(如果2块显卡就显示2),若为0表示安装不成功
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})
main.cpp代码为:
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;
}
另外一段测试代码,用来调用摄像头,然后查看是否占用gpu的,如果上面成功了,这段代码就没必要测试。
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://blog.csdn.net/u013066730/article/details/53782015