OpenCV CUDA模块设备层----- 正切(tangent)运算函数tan()
- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
OpenCV的CUDA模块(cudev)中的一个设备函数(device function),用于在 GPU 上对uchar3类型的向量(如 RGB 像素)进行正切(tangent)运算,并返回一个 float3 类型的结果。
函数原型
__device__ __forceinline__ float3 cv::cudev::tan(const uchar3 &a)
参数
- const uchar3 &a 输入参数为一个 3 通道的无符号字符向量(如 RGB 像素)
代码
#include <opencv2/opencv.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/cudev.hpp>
#include <iostream>__global__ void tanKernel(const uchar3* input, float3* output, int numPixels) {int idx = blockIdx.x * blockDim.x + threadIdx.x;// if (idx < numPixels) {output[idx] = cv::cudev::tan(input[idx]);// }
}int main() {// 读取图像cv::Mat bgr = cv::imread("/media/dingxin/data/study/OpenCV/sources/images/img0.jpg");if (bgr.empty()) {std::cerr << "Failed to load image!" << std::endl;return -1;}// 转换为 RGB 格式(uchar3)cv::Mat src;cv::cvtColor(bgr, src, cv::COLOR_BGR2RGB);int width = src.cols;int height = src.rows;int numPixels = width * height;// 分配 GPU 内存uchar3* d_input;float3* d_output;cudaMalloc(&d_input, numPixels * sizeof(uchar3));cudaMalloc(&d_output, numPixels * sizeof(float3));cudaMemcpy(d_input, src.ptr<uchar3>(), numPixels * sizeof(uchar3), cudaMemcpyHostToDevice);// 启动 kernelint blockSize = 256;int numBlocks = (numPixels + blockSize - 1) / blockSize;tanKernel<<<numBlocks, blockSize>>>(d_input, d_output, numPixels);// 下载结果cv::Mat result(height, width, CV_32FC3);cudaMemcpy(result.ptr<float3>(), d_output, numPixels * sizeof(float3), cudaMemcpyDeviceToHost);// 显示结果(注意:可能有非常大的值)cv::Mat display;cv::normalize(result, display, 0, 1, cv::NORM_MINMAX, CV_32F);cv::imshow("Tan Result", display);cv::waitKey(0);// 清理资源cudaFree(d_input);cudaFree(d_output);return 0;
}