- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
OpenCV 的 CUDA 设备端数学函数 中的一个内联函数,用于在 GPU 上对 uchar1 类型(单通道图像像素)执行指数运算 exp(…)。
函数原型
__device__ __forceinline__ float1 cv::cudev::exp ( const uchar1 & a )
参数
- a const uchar1& 输入一个 uchar1 类型的像素值(取值范围:0 ~ 255)
返回值
返回一个 float1 类型(CUDA 向量结构体),包含 .x 成员,表示计算结果。
示例代码
#include <opencv2/opencv.hpp>
#include <opencv2/cudev/common.hpp>
#include <opencv2/cudev/util/vec_math.hpp>
__global__ void kernel_exp(const uchar1* input, float1* output, int size) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < size) {
output[idx] = cv::cudev::exp(input[idx]);
}
}
int main() {
const int N = 256;
uchar1 h_input[N];
float1 h_output[N];
// 初始化输入数据
for (int i = 0; i < N; ++i) {
h_input[i].x = i; // 0-255
}
// 分配设备内存
uchar1* d_input;
float1* d_output;
cudaMalloc(&d_input, N * sizeof(uchar1));
cudaMalloc(&d_output, N * sizeof(float1));
// 拷贝数据到设备
cudaMemcpy(d_input, h_input, N * sizeof(uchar1), cudaMemcpyHostToDevice);
// 调用核函数
dim3 block(256);
dim3 grid((N + block.x - 1) / block.x);
kernel_exp<<<grid, block>>>(d_input, d_output, N);
// 拷贝结果回主机
cudaMemcpy(h_output, d_output, N * sizeof(float1), cudaMemcpyDeviceToHost);
// 打印部分结果
for (int i = 0; i < 10; ++i) {
printf("exp(%d) = %f\n", h_input[i].x, h_output[i].x);
}
// 释放内存
cudaFree(d_input);
cudaFree(d_output);
return 0;
}
运行结果
exp(0) = 1.000000
exp(1) = 2.718282
exp(2) = 7.389056
exp(3) = 20.085537
exp(4) = 54.598148
exp(5) = 148.413162
exp(6) = 403.428802
exp(7) = 1096.633179
exp(8) = 2980.958008
exp(9) = 8103.083984