OpenCV每日函数 图像过滤模块 (3) boxFilter函数

一、概述

        使用箱形滤镜模糊图像,该函数使用内核平滑图像:

其中

         非归一化箱形滤波器可用于计算每个像素邻域上的各种积分特征,例如图像导数的协方差矩阵(用于密集光流算法等)。 如果您需要计算可变大小窗口上的像素和,请使用积分图

二、boxFilter函数

1、函数原型

cv::boxFilter (InputArray src, OutputArray dst, int ddepth, Size ksize, Point anchor=Point(-1,-1), bool normalize=true, int borderType=BORDER_DEFAULT)

2、参数详解 

src 输入图像。
dst 输出与 src 大小和类型相同的图像。
ddepth 输出图像深度(-1 使用 src.depth())。
ksize 模糊内核大小。
anchor 锚点; 默认值 Point(-1,-1) 表示锚点位于内核中心。
normalize 标志,指定内核是否按其区域进行规范化。
borderType 用于推断图像外部像素的边框模式,请参阅 BorderTypes。 不支持 BORDER_WRAP。

三、OpenCV源码 

1、源码路径

opencv\modules\imgproc\src\box_filter.dispatch.cpp

2、源码代码

void boxFilter(InputArray _src, OutputArray _dst, int ddepth,
               Size ksize, Point anchor,
               bool normalize, int borderType)
{
    CV_INSTRUMENT_REGION();

    CV_Assert(!_src.empty());

    CV_OCL_RUN(_dst.isUMat() &&
               (borderType == BORDER_REPLICATE || borderType == BORDER_CONSTANT ||
                borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101),
               ocl_boxFilter3x3_8UC1(_src, _dst, ddepth, ksize, anchor, borderType, normalize))

    CV_OCL_RUN(_dst.isUMat(), ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize))

    Mat src = _src.getMat();
    int stype = src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
    if( ddepth < 0 )
        ddepth = sdepth;
    _dst.create( src.size(), CV_MAKETYPE(ddepth, cn) );
    Mat dst = _dst.getMat();
    if( borderType != BORDER_CONSTANT && normalize && (borderType & BORDER_ISOLATED) != 0 )
    {
        if( src.rows == 1 )
            ksize.height = 1;
        if( src.cols == 1 )
            ksize.width = 1;
    }

    Point ofs;
    Size wsz(src.cols, src.rows);
    if(!(borderType&BORDER_ISOLATED))
        src.locateROI( wsz, ofs );

    CALL_HAL(boxFilter, cv_hal_boxFilter, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, ddepth, cn,
             ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
             anchor.x, anchor.y, normalize, borderType&~BORDER_ISOLATED);

    CV_OVX_RUN(true,
               openvx_boxfilter(src, dst, ddepth, ksize, anchor, normalize, borderType))

    //CV_IPP_RUN_FAST(ipp_boxfilter(src, dst, ksize, anchor, normalize, borderType));

    borderType = (borderType&~BORDER_ISOLATED);

    Ptr<FilterEngine> f = createBoxFilter( src.type(), dst.type(),
                        ksize, anchor, normalize, borderType );

    f->apply( src, dst, wsz, ofs );
}

四、效果图像示例

原图
ksize = 3;normalize = true
ksize = 3;normalize = false

猜你喜欢

转载自blog.csdn.net/bashendixie5/article/details/125235507
今日推荐