1.操作
b站教学视频。
安装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配合的操作。后面会继续出。