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

OpenCV CUDA模块设备层-----反向二值化阈值处理函数thresh_binary_inv_func()

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

算法描述

OpenCV CUDA 模块(cudev) 中的一个仿函数(functor)生成器,用于创建一个反向二值化阈值处理函数对象。
这个函数返回一个 仿函数对象(functor),用于在 GPU 上执行反向二值化阈值处理(Threshold Binary Inverted),即:
如果像素值小于等于 thresh,则设为 maxVal;否则设为 0。

函数原型


template<typename T >
__host__ __device__ ThreshBinaryInvFunc<T> cv::cudev::thresh_binary_inv_func 	( 	T  	thresh,T  	maxVal ) 		

参数

  • T thresh 阈值,如果像素值小于等于该值则保留最大值
  • T maxVal 像素满足条件时设置的最大值

代码


#include <opencv2/cudev.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>// CUDA kernel 使用 functor 对图像进行反向二值化
template <typename T>
__global__ void thresholdInvKernel(const T* input, T* output, int numPixels,cv::cudev::ThreshBinaryInvFunc<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: 创建反向二值化函数对象auto func = cv::cudev::thresh_binary_inv_func<uchar>(128, 255);// Step 4: 启动 kernelint blockSize = 256;int numBlocks = (numPixels + blockSize - 1) / blockSize;thresholdInvKernel<<<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("Binary Inv Threshold Result", result);cv::waitKey(0);cv::imwrite("binary_inv_result.jpg", result);// Step 7: 清理资源cudaFree(d_input);cudaFree(d_output);return 0;
}

运行结果

在这里插入图片描述

http://www.lryc.cn/news/578586.html

相关文章:

  • Python学习Day48
  • golang generic 2022-04-13
  • 技术学习_人工智能_1_神经网络是如何实现的?
  • IDE全家桶专用快捷键----------个人独家分享!!
  • 02.SpringBoot常用Utils工具类详解
  • pytorch学习—7.处理多维特征的输入
  • 通达信【极弱强势指标与股道波段交易系统】幅图
  • 【学习笔记】Python中主函数调用的方式
  • 修改Spatial-MLLM项目,使其专注于无人机航拍视频的空间理解
  • Electron 应用中的内容安全策略 (CSP) 全面指南
  • AbMole| H₂DCFDA(M9096;活性氧(ROS)探针)
  • 医学编码:临床试验数据标准化的关键
  • Next.js 安装使用教程
  • 多容器应用与编排——AI教你学Docker
  • Web性能测试常用指标(转自百度AI)
  • 开关电源和线性电源Multisim电路仿真实验汇总——硬件工程师笔记
  • 暖通锅炉的智能管控:物联网实现节能又舒适​
  • grom使用mysql快速上手
  • [论文阅读] 人工智能 + 软件工程 | 从软件工程视角看大语言模型:挑战与未来之路
  • 使用 icinga2 写入 TDengine
  • 基于ApachePOI实现百度POI分类快速导入PostgreSQL数据库实战
  • SpringBoot计时一次请求耗时
  • 基于netmiko模块实现支持SSH or Telnet的多线程多厂商网络设备自动化巡检脚本
  • 浏览器F12开发者工具的使用
  • [Python] -基础篇7-新手常见Python语法错误及解决方案
  • Qt时间显示按钮功能详解
  • openlayers根据图层名称判断图层是否在视口内
  • js代码09
  • Maven安装使用教程
  • java web2(黑马)