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

运行结果

相关推荐
u0103055275 分钟前
AI Agent驱动智能应用生成
人工智能
一次旅行7 分钟前
DeepSeek‑V4‑Flash‑Vision‑Exp 小白入门实战|3种传图方式、完整可跑代码、避坑排障
java·前端·人工智能
pjj198548 分钟前
机器学习-NumPy2
人工智能·python·机器学习
天涯明月199313 分钟前
AI Agent应用深度研究报告
人工智能·大模型·agent
鹿鸣天涯17 分钟前
AI办公场景的数据安全和Token自主方案
人工智能
doudoudalao19 分钟前
2026年AI生成电商带货视频工具有哪些?TikTok带货素材生产方案与对比
大数据·人工智能
网络研究院25 分钟前
不装了!Claude背后的AI巨头也开始造芯片了,英伟达的“铁饭碗”要被砸?
人工智能·科技·ai·芯片·供应链·底层·产业链
laboratory agent开发28 分钟前
企业AI应用定制中的转人工交接:上下文为何接不上
人工智能
其实防守也摸鱼30 分钟前
Codex破局:前端组件秒级生成的技术文章大纲
开发语言·前端·人工智能·学习·安全·web安全
audyxiao00135 分钟前
重磅发布|《上海市教育发展“十五五”规划》及其解读
人工智能·十五五·教育发展