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;
}

运行结果

相关推荐
OceanBase数据库官方博客4 小时前
OceanBase DataPilot AIP:Ontology 承载AI能力面的另一条路
人工智能·oceanbase
笨鸟先飞,勤能补拙5 小时前
AI 赋能网络安全:技术全景、成熟度评估与实战案例
人工智能·python·安全·web安全·网络安全·sqlite·github
一次旅行5 小时前
AI 前沿日报 | 2026年07月31日
人工智能
2601_963749105 小时前
标题:越华环保集团|面向美丽河湖项目的数字化污水治理云边协同采集架构设计
人工智能
沐籽李5 小时前
从溶剂可及表面积SASA理解抗体结构与工程改造
人工智能·药物设计·aidd·sasa
智慧物业老杨5 小时前
物业如何做好预算管理?落地架构逻辑
人工智能·架构
微学AI5 小时前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
城管不管5 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
boppu6 小时前
布草特殊污渍去渍剂的种类及作用
大数据·人工智能
AIsoft_86886 小时前
会议录音转文字与AI纪要工具推荐:免费额度与核心功能对比指南
人工智能