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

运行结果

相关推荐
Acrelhuang2 分钟前
独立监测 + 集团管控 安科瑞连锁餐饮能源方案全链路提效-安科瑞黄安南
人工智能
laplace012312 分钟前
Clawdbot 部署到飞书(飞连)使用教程(完整版)
人工智能·笔记·agent·rag·clawdbot
是小蟹呀^13 分钟前
卷积神经网络(CNN):卷积操作
人工智能·神经网络·cnn
DN202025 分钟前
AI销售机器人:节日祝福转化率提升30倍
人工智能·python·深度学习·机器学习·机器人·节日
爱喝可乐的老王43 分钟前
PyTorch简介与安装
人工智能·pytorch·python
deephub1 小时前
用 PyTorch 实现 LLM-JEPA:不预测 token,预测嵌入
人工智能·pytorch·python·深度学习·大语言模型
量子-Alex1 小时前
【多模态大模型】Qwen2-VL项目代码初步解析
人工智能
飞鹰511 小时前
深度学习算子CUDA优化实战:从GEMM到Transformer—Week4学习总结
c++·人工智能·深度学习·学习·transformer
工程师老罗1 小时前
Pytorch如何验证模型?
人工智能·pytorch·深度学习