OpenCV CUDA模块设备层-----双曲余弦函数cosh()

发布于:2025-06-27 ⋅ 阅读:(17) ⋅ 点赞:(0)
  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

该函数用于计算四维浮点向量(float4类型)的双曲余弦值,作用于CUDA设备端。双曲余弦函数定义为cosh(x) = (eˣ + e⁻ˣ)/2,具有偶函数性质(cosh(-x) = cosh(x))

函数原型

__device__ __forceinline__ float4 cv::cudev::cosh 	( 	const float4 &  	a	) 	

参数

参数 类型 描述
a const float4& 输入向量,每个分量独立计算双曲余弦

返回值

返回float4类型向量,其每个分量为输入向量对应分量的双曲余弦值,值域为[1, +∞)

应用场景‌

适用于GPU加速的数学计算、图像处理(如非线性滤波)和物理模拟等领域。

代码示例


#include <opencv2/opencv.hpp>
#include <opencv2/cudev/common.hpp>
#include <opencv2/cudev/util/vec_math.hpp>

__global__ void kernel_cosh(const float4* input, float4* output, int size) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < size) {
        output[idx] = cv::cudev::cosh(input[idx]);
    }
}

int main() {
    const int N = 4;
    float4 h_input[N] = {
        {0.0f, 1.0f, 2.0f, 3.0f},
        {-1.0f, -2.0f, -3.0f, -4.0f},
        {0.5f, 1.5f, 2.5f, 3.5f},
        {-0.5f, -1.5f, -2.5f, -3.5f}
    };
    float4 h_output[N];

    // 分配设备内存
    float4* d_input;
    float4* d_output;
    cudaMalloc(&d_input, N * sizeof(float4));
    cudaMalloc(&d_output, N * sizeof(float4));

    // 拷贝数据到设备
    cudaMemcpy(d_input, h_input, N * sizeof(float4), cudaMemcpyHostToDevice);

    // 调用核函数
    dim3 block(4);
    dim3 grid(1);
    kernel_cosh<<<grid, block>>>(d_input, d_output, N);

    // 拷贝结果回主机
    cudaMemcpy(h_output, d_output, N * sizeof(float4), cudaMemcpyDeviceToHost);

    // 打印结果
    for (int i = 0; i < N; ++i) {
        printf("cosh(%.1f, %.1f, %.1f, %.1f) = (%.6f, %.6f, %.6f, %.6f)\n",
               h_input[i].x, h_input[i].y, h_input[i].z, h_input[i].w,
               h_output[i].x, h_output[i].y, h_output[i].z, h_output[i].w);
    }

    // 释放内存
    cudaFree(d_input);
    cudaFree(d_output);

    return 0;
}

运行结果

cosh(0.0, 1.0, 2.0, 3.0) = (1.000000, 1.543081, 3.762196, 10.067662)
cosh(-1.0, -2.0, -3.0, -4.0) = (1.543081, 3.762196, 10.067662, 27.308231)
cosh(0.5, 1.5, 2.5, 3.5) = (1.127626, 2.352410, 6.132289, 16.572824)
cosh(-0.5, -1.5, -2.5, -3.5) = (1.127626, 2.352410, 6.132289, 16.572824)

网站公告

今日签到

点亮在社区的每一天
去签到