OpenCV CUDA模块设备层-----在 GPU上计算反双曲正切函数atanh()

  • 操作系统: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
相关推荐
冬奇Lab1 天前
Agent 系列(23):Web Agent——让 Agent 真正浏览网页
人工智能·llm·agent
冬奇Lab1 天前
每日一个开源项目(第135篇):codebase-memory-mcp - 给 AI Agent 一张代码库的知识图谱
人工智能·开源·llm
IT_陈寒1 天前
JavaScript的闭包把我坑惨了,说好的内存会自动回收呢?
前端·人工智能·后端
jooloo1 天前
Codex 间歇性 400 之谜:一条对话里,它为什么有时候用 chat/completions,有时候切到 responses?
人工智能
用户5191495848451 天前
OpenSSL PKCS#12 PBMAC1 堆栈缓冲区溢出漏洞 (CVE-2025-11187) 分析与验证
人工智能·aigc
小小杨树1 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
用户5191495848451 天前
HP Sound Research SECOMNService 权限提升漏洞利用工具
人工智能·aigc
用户018349301691 天前
给 AI 智能体能力包一层 BFF,前端只调一个接口
人工智能