- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
对输入的 uchar1 像素值(范围 [0, 255]),先归一化到 [0.0, 1.0] 浮点区间,然后计算其 反双曲正切函数 atanh(x),最终返回一个 float1 类型的结果。
函数原型
cpp
__device__ __forceinline__ float1 cv::cudev::atanh ( const uchar1 & a )
参数
- uchar1 CUDA 向量类型,表示一个单通道 8 位无符号整型(等价于 unsigned char 或 uint8_t)。
代码示例
cpp
#include <opencv2/cudev/common.hpp>
#include <cstdio>
#include <cmath> // for atanhf
// 手动实现 atan2f 并做边界保护
__device__ __forceinline__ float safe_atanh(float x) {
if (x <= -1.0f) return -1.0e+37f; // -inf
if (x >= 1.0f) return +1.0e+37f; // +inf
return atanhf(x);
}
__global__ void test_atanh() {
uchar1 a = make_uchar1(128); // 输入一个像素值
float normalized = a.x / 255.0f;
// 安全处理
if (normalized >= 1.0f) normalized = 0.999999f;
if (normalized <= 0.0f) normalized = 0.000001f;
float result = safe_atanh(normalized);
printf("atanh(%f) = %f\n", normalized, result);
}
int main() {
test_atanh<<<1, 1>>>();
cudaDeviceReset();
return 0;
}
运行结果
bash
atanh(0.501961) = 0.551924