结合OpenCV和CUDA扩展自定义函数接口之导向滤波算法实现

本文介绍如何利用CUDA和OpenCV扩展自定义函数接口,以实现导向滤波算法。首先分析OpenCV模块结构,然后在cudaimgproc模块中定义接口和实现源文件。接着讲解如何重新编译生成库,并演示Python调用接口。文章提供了CUDA和OpenCV结合使用的实例,以及在实现过程中可能遇到的问题和解决方案。
摘要由CSDN通过智能技术生成

声明:本文内容原创,首发于CSDN博客。未经许可禁止转载。需要更多帮助请私信或邮件联系。

前言

CUDA(Compute Unified Device Architecture,统一计算架构)是由NVIDIA所推出的一种集成技术,是其对于GPGPU(A General-Purpose Graphics Processing Unit)的正式名称。通过该技术,开发者可以利用NVIDIA的GeForce 8以后的GPU进行计算。极大加速计算型应用的效率。通常用于游戏开发、视频编解码、图像处理等领域。

OpenCV从3.0版后集成了关于CUDA相关操作的高级封装,其中GpuMat数据类型可以看做Mat的GPU版本,有极好的数据属性封装,且能够内部隐式转化成可以直接作为核函数参数的PtrStepSz、PtrStep。
在这里插入图片描述

我们可以很方便使用OpenCV搭建好的框架来扩展实现需要的算法,本文将以导向滤波算法为例,记录使用CUDA使能的OpenCV来实现的GuidedFilter

分析

在使用OpenCV扩展实现算法前,需要了解OpenCV源码的结构。

在OpenCV Doc官网,可以看到OpenCV的模块包含Main modulesExtra modules两大部分,分别放在opencvopencv_contrib中:
在这里插入图片描述
在这里插入图片描述
对应到GitHub源码,可以很清晰的看出modules的结构:

Main modules部分源码:
在这里插入图片描述
Extra modules部分源码:
在这里插入图片描述
本文主要考虑CUDA加速的导向滤波算法实现,因此以Extra modules部分源码为基准进行扩展。

例如对于cudaimgproc模块,在opencv_contrib/modules/cudaimgproc/路径下的结构如下:
在这里插入图片描述
包括了头文件、实现源文件、测试代码以及CMakeLists.txt,其中CMakeLists.txt定义了该模块下需要编译的模块内容选项,其内容如下:

if(IOS OR WINRT OR (NOT HAVE_CUDA AND NOT BUILD_CUDA_STUBS))
  ocv_module_disable(cudaimgproc)
endif()

set(the_description "CUDA-accelerated Image Processing")

ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127 /wd4100 /wd4324 /wd4512 /wd4515 -Wundef -Wmissing-declarations -Wshadow -Wunused-parameter)

ocv_define_module(cudaimgproc opencv_imgproc OPTIONAL opencv_cudev opencv_cudaarithm opencv_cudafilters WRAP python)

而头文件的文件夹"include/opencv2"中只包含了cudaimgproc.hpp一个头文件,其中定义了编译完成后可供外部调用的函数接口,例如CUDA版双边滤波算法接口定义如下:
在这里插入图片描述
关于可供Python调用接口生成部分的内容可以看之前的博客《使用OpenCV自带gen2.py等工具生成C++的Python binding示例》内容。

再看实现源文件的文件夹src,可以看到如下结构:
在这里插入图片描述
还是以双边滤波为例,打开bilateral_filter.cpp文件,可以看到具体的实现内容:
在这里插入图片描述
需要注意的是其调用了cuda文件夹下的bilateral_filter.cu CUDA核函数实现源码。

另外被包含的precomp.hpp头文件中定义了OpenCV在编译这个modules时需要的一些依赖头文件:

#ifndef __OPENCV_PRECOMP_H__
#define __OPENCV_PRECOMP_H__

#include "opencv2/cudaimgproc.hpp"

#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include "opencv2/core/private.cuda.hpp"

#include "opencv2/opencv_modules.hpp"

#ifdef HAVE_OPENCV_CUDAARITHM
#  include "opencv2/cudaarithm.hpp"
#endif

#ifdef HAVE_OPENCV_CUDAFILTERS
#  include "opencv2/cudafilters.hpp"
#endif

#include <limits>
#include <algorithm>

#endif /* __OPENCV_PRECOMP_H__ */

关于CUDA版双边滤波的实现源码只在这些文件中出现,在OpenCV编译生成后则可以正常的使用相应的接口“cv::cuda::bilateralFilter()”了。

类似于双边滤波,导向滤波其实也可以类似实现。

实现

要实现基于OpenCV的CUDA版导向滤波算法,需要定义接口、编写头文件以及对类和方法进行实现。

一、接口定义

考虑到导向滤波和双边滤波的相似性,可以选择在cudaimgproc模块中实现相应源码。

先在cudaimgproc模块的头文件"include/opencv2/cudaimgproc.hpp"中定义导向滤波的接口:


// Guided Filter ///
class CV_EXPORTS_W  GuidedFilter  : public Algorithm{
   
 public:
    CV_WRAP virtual void update(InputArray guide, int dDepth = -1, Stream &stream = Stream::Null()) = 0;
    CV_WRAP virtual void filter(InputArray src, OutputArray dst, int dDepth = -1, Stream &stream = Stream::Null()) = 0;
};

CV_EXPORTS_W Ptr<GuidedFilter> createGuidedFilter(Size size, int radius, double eps, int dDepth, int type, int channels = 3);

CV_EXPORTS_W void guidedFilter(InputArray guide, InputArray src, OutputArray dst, int radius, double eps, int dDepth = -1, Stream& stream = Stream::Null());

通过定义接口,我们可以使用cv::cuda::guidedFilter(C++)或者createGuidedFilter & update & filter的方式来调用CUDA导向滤波。

在这里插入图片描述

二、头文件实现

在src源码中新建guided_filter.hpp文件,定义相应的导向滤波类和方法:

/*M///
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TracelessLe

❀点个赞加个关注再走吧❀

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值