在VS2022中配置opencv_cuda版本,并运行,与cuda runtime结合

1.操作

b站教学视频。

https://www.bilibili.com/video/BV1AE411k7Fm/?spm_id_from=333.337.search-card.all.click&vd_source=87968590149c2e0671873770a67d702e

安装cuda工具,opencv编译创建环境啥的。

我是cuda11.6,opencv4.6,没啥问题。

就是编译会花很长很长时间。

完了之后,因为是vs2022,所以在空项目里跑.cu文件时修改不到CUDA文件类型,所以会出问题。

这里我是在cuda runtime项目里配置的opencv cuda环境。

就是这个。

然后测试运行了这本书里的代码。

其中有些gpu文件时opencv的老文件了,在新版已经舍弃了。


#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include<cuda.h>
#include<cuda_device_runtime_api.h>
#include <opencv2/core/cuda.hpp>
#include<opencv2\opencv.hpp>
#include<opencv.hpp>
#include <stdio.h>
#include<iostream>


using namespace std;
using namespace cv;
using namespace cuda;

template<int nthreads>
__global__ void compute_kernel(int height, int width, const PtrStepb img, PtrStepb dst) {

	const int x = blockIdx.x * blockDim.x + threadIdx.x;
	const int y = blockIdx.y * blockDim.y + threadIdx.y;

	const uchar* src_y = (const uchar*)(img + y * img.step);
	uchar* dst_y = (uchar*)(dst + y * dst.step);
	if (x < width && y < height) {
		dst_y[3 * x] = src_y[3 * x];
		dst_y[3 * x + 1] = src_y[3 * x + 1];
		dst_y[3 * x + 2] = src_y[3 * x + 2];   //三通道
	}


}


int main() {

	Mat a = imread("C:/Users/1/Pictures/Saved Pictures/1.jpg");
	GpuMat d_a(a);
	GpuMat d_dst(d_a.size(), CV_8UC3);

	int width = a.size().width;
	int height = a.size().height;
	const int nthreads = 256;
	dim3 bdim(nthreads, 1);
	dim3 gdim(divUp(width, bdim.x), divUp(height, bdim.y));

	compute_kernel<nthreads> << <gdim, bdim >> > (height, width, d_a, d_dst);

	Mat dst(d_dst);
	imshow("原始图像",a);
	imshow("处理后图像",dst);

	waitKey();

	return 0;
}

代码,即运行结果。

刚开始学习opencv与cuda配合的操作。后面会继续出。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值