当前位置: 首页 > news >正文

OpenCV CUDA模块设备层-----“大于阈值设为零” 的图像处理函数 thresh_to_zero_inv_func()

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

OpenCV 的 CUDA 模块(cudev) 中的一个仿函数生成器,用于创建一个 “大于阈值设为零” 的图像处理函数对象。
这个函数返回一个 仿函数对象(functor),用于在 GPU 上执行如下操作:
如果像素值大于 thresh,则设置为 0;否则保留原值不变。

函数原型

__host__ __device__ ThreshToZeroInvFunc<T> cv::cudev::thresh_to_zero_inv_func 	( 	T  	thresh	) 	

参数

  • T thresh 阈值,如果像素值大于该值,则设置为 0

代码


#include <opencv2/cudev.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>// CUDA kernel 使用 functor 对图像进行 "大于阈值设为 0" 处理
template <typename T>
__global__ void toZeroInvKernel(const T* input, T* output, int numPixels,cv::cudev::ThreshToZeroInvFunc<T> func) {int idx = blockIdx.x * blockDim.x + threadIdx.x;if (idx < numPixels) {output[idx] = func(input[idx]);}
}int main() {// Step 1: 读取图像并转为灰度图cv::Mat bgr = cv::imread("/media/dingxin/data/study/OpenCV/sources/images/Lenna.png", cv::IMREAD_COLOR);if (bgr.empty()) {std::cerr << "Failed to load image!" << std::endl;return -1;}cv::Mat src;cv::cvtColor(bgr, src, cv::COLOR_BGR2GRAY); // 灰度图int width = src.cols;int height = src.rows;int numPixels = width * height;// Step 2: 分配 GPU 内存uchar* d_input, *d_output;cudaMalloc(&d_input, numPixels * sizeof(uchar));cudaMalloc(&d_output, numPixels * sizeof(uchar));cudaMemcpy(d_input, src.data, numPixels * sizeof(uchar), cudaMemcpyHostToDevice);// Step 3: 创建 "大于阈值设为 0" 的函数对象auto func = cv::cudev::thresh_to_zero_inv_func<uchar>(128);// Step 4: 启动 kernelint blockSize = 256;int numBlocks = (numPixels + blockSize - 1) / blockSize;toZeroInvKernel<<<numBlocks, blockSize>>>(d_input, d_output, numPixels, func);// Step 5: 下载结果cv::Mat result(height, width, CV_8U);cudaMemcpy(result.data, d_output, numPixels * sizeof(uchar), cudaMemcpyDeviceToHost);// Step 6: 显示和保存结果cv::imshow("original image", bgr);cv::imshow("ToZero Inv Threshold Result", result);cv::waitKey(0);cv::imwrite("tozero_inv_result.jpg", result);// Step 7: 清理资源cudaFree(d_input);cudaFree(d_output);return 0;
}

运行结果

在这里插入图片描述

http://www.lqws.cn/news/597133.html

相关文章:

  • torch.nn
  • Postman - API 调试与开发工具 - 标准使用流程
  • Mac 部署 Dify小红书种草工作流
  • 新手向:从零开始MySQL超详细安装、配置与使用指南
  • stm32l4系列启用看门狗后,调用HAL_IWDG_Refreh()就复位
  • HakcMyVM-Arroutada
  • java生成word文档
  • 飞算JavaAI:重构软件开发范式的智能引擎
  • ABB驱动系列SCYC51213 63911607C驱动板
  • java微服务-linux单机CPU接近100%优化
  • Python应用指南:利用高德地图API获取公交+地铁可达圈(二)
  • 再见 RAG?Gemini 2.0 Flash 刚刚 “杀死” 了它!
  • 学习面向对象
  • 第TR3周:Pytorch复现Transformer
  • 快速手搓一个MCP服务指南(九): FastMCP 服务器组合技术:构建模块化AI应用的终极方案
  • 【仿muduo库实现并发服务器】Poller模块
  • 基于中国印尼会计准则差异,中国企业在印尼推广ERP(SAP、Oracle)系统需要注意的细节
  • Pycharm命令行能运行,但绿色三角报错?
  • mac重复文件清理,摄影师同款清理方案
  • nosql项目:基于 Redis 哨兵模式的鲜花预订配送系统
  • 设计模式之组合模式
  • 将实时流的 H.264(视频)与 G.711A(音频)封装成 MP4 文件
  • 关于量子计算的一份介绍
  • 12【进程间通信——管道】
  • Vue 响应式数据传递:ref、reactive 与 Provide/Inject 完全指南
  • 基于 Three.js 与 WebGL 的商场全景 VR 导航系统源码级解析
  • 遥感云大数据在灾害、水体与湿地领域案例及GPT应用
  • 第八章:LeRobot摄像头配置与应用指南
  • 使用GeoServer发布地图shapefi(.shp)数据
  • Spring Bean的生命周期与作用域详解