opencv3.3 CUDA 初学实例

Posted 罗比达

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv3.3 CUDA 初学实例相关的知识,希望对你有一定的参考价值。

#include "opencv2/core.hpp"  
#include "opencv2/cudaarithm.hpp"  
#include "opencv2/cudafilters.hpp"  
#include "opencv2/cudaimgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>


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

	if (num_devices <= 0)
	{
		std::cout << "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::cout << "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";

	cv::Mat src_image = cv::imread("1118.png", 1);
	cv::Mat dst_image;
	cv::cuda::GpuMat d_src_img(src_image);
	cv::cuda::GpuMat d_dst_img;
	cv::cuda::cvtColor(d_src_img, d_dst_img, CV_BGR2GRAY);
	d_dst_img.download(dst_image);
	cv::namedWindow("test", 0);
	cv::imshow("test", dst_image);
	cv::waitKey(0);
	return 0;
}

  

以上是关于opencv3.3 CUDA 初学实例的主要内容,如果未能解决你的问题,请参考以下文章

RuntimeError: CUDA out of memory. Tried to allocate 600.00 MiB (GPU 0; 23.69 GiB total capacity)(代码片

ubuntu16.04下安装opencv3.3

CMake Error NOTFOUND CUDA_nppi_LIBRARY (ADVANCED)

Professional CUDA C Programming的代码实例1.1

Visual Studio 是不是使用 nvcc 编译 cuda 代码?

SIFT图像特征提取 python3.6 + opencv3.3代码