OpenCV CUDA 模块图像过滤------创建一个线性滤波器(Linear Filter)函数createLinearFilter()

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

算法描述

该函数用于创建一个线性滤波器(Linear Filter),可以对图像执行任意用户定义的卷积核(kernel)操作。适用于模糊、锐化、边缘检测等图像处理任务。

由于是 CUDA 版本,该函数在 GPU 上运行,适合大规模图像处理应用,具有较高的性能。

函数原型

cpp 复制代码
Ptr<Filter> cv::cuda::createLinearFilter 	
(
 	int  	srcType,
	int  	dstType,
	InputArray  	kernel,
	Point  	anchor = Point(-1, -1),
	int  	borderMode = BORDER_DEFAULT,
	Scalar  	borderVal = Scalar::all(0) 
) 		

参数

参数名 描述
srcType 输入图像类型。支持 CV_8UCV_16UCV_32F 的单通道和四通道图像。
dstType 输出图像类型。目前仅支持与输入图像相同的类型。
kernel 滤波器系数的二维数组(即卷积核)。
anchor 锚点。默认值 Point(-1, -1) 表示锚点位于卷积核的中心。
borderMode 像素外推方法。详细信息请参见 borderInterpolate 函数。
borderVal 默认的边界填充值。

代码示例

cpp 复制代码
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/cudafilters.hpp>

int main()
{
    // 读取图像并上传到 GPU
    cv::Mat h_input = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/Lenna.png", cv::IMREAD_GRAYSCALE );
    cv::cuda::GpuMat d_input, d_output;
    d_input.upload( h_input );

    // 定义一个简单的 3x3 平均模糊核
    cv::Mat kernel = ( cv::Mat_< float >( 3, 3 ) << 1. / 9, 1. / 9, 1. / 9, 1. / 9, 1. / 9, 1. / 9, 1. / 9, 1. / 9, 1. / 9 );

    // 创建线性滤波器
    cv::Ptr< cv::cuda::Filter > filter = cv::cuda::createLinearFilter( d_input.type(), d_input.type(), kernel );

    // 应用滤波
    filter->apply( d_input, d_output );

    // 下载结果并显示
    cv::Mat h_output;
    d_output.download( h_output );
    cv::imshow( "Filtered Image", h_output );
    cv::waitKey();
    return 0;
}

运行结果

相关推荐
珂朵莉MM3 分钟前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第3赛季优化题--束搜索
人工智能·算法
智慧物业老杨3 分钟前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
科技AI训练师3 分钟前
2026高压清洗泵厂家选择指南:判断标准与选购要点
大数据·人工智能
Front思5 分钟前
AI前端工程师需要具备能力+
前端·人工智能·ai
Percent_bigdata6 分钟前
“模数共振”开启产业AI新阶段,重新定义数据治理
大数据·人工智能
AI品信智慧数智人9 分钟前
告别传统导游!伴游小助手,AI智能解锁全新旅行体验✨
人工智能·旅游
乐维_lwops17 分钟前
从 “救火运维” 到 “自动驾驶”:运维智能体到底解决了什么?
运维·人工智能·运维智能体
TheRouter40 分钟前
AI Agent 记忆体系建设实战:短期、长期与工作记忆的工程实现
数据库·人工智能·oracle
weixin_4684668543 分钟前
MoneyPrinterTurbo 短视频自动化生产实战指南
运维·人工智能·自动化·大模型·音视频·moneyprinter
Omics Pro1 小时前
首个!外源天然产物综合性代谢图谱
数据库·人工智能·算法·机器学习·r语言