☰
【opencv】OpenCV: blur函数的边界到底是怎么处理的?
2026/10/23 4:03:00 网站建设 项目流程

blur函数的边界到底是怎么处理的?

LZ倒是一直使用这个blur函数,而且一般都是用作中值滤波,所以在python中使用起来相当方便.结果后面需要考虑边界的时候,一个问题把我问住,这个blur在边界是怎么处理的呢?

  • 例如下面这个用法:
winSize=(3,3)blur_res=cv2.blur(test,winSize)

我们先来看下python中的函数调用

defblur(src,ksize,dst=None,anchor=None,borderType=None):# real signature unknown; restored from __doc__""" blur(src, ksize[, dst[, anchor[, borderType]]]) -> dst . @brief Blurs an image using the normalized box filter. . . The function smooths an image using the kernel: . . \f[\texttt{K} = \frac{1}{\texttt{ksize.width*ksize.height}} \begin{bmatrix} 1 & 1 & 1 & \cdots & 1 & 1 \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \hdotsfor{6} \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \end{bmatrix}\f] . . The call `blur(src, dst, ksize, anchor, borderType)` is equivalent to `boxFilter(src, dst, src.type(), . anchor, true, borderType)`. . . @param src input image; it can have any number of channels, which are processed independently, but . the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F. . @param dst output image of the same size and type as src. . @param ksize blurring kernel size. . @param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel . center. . @param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes . @sa boxFilter, bilateralFilter, GaussianBlur, medianBlur """pass
  • param src输入图像;它可以具有任意数量的通道,这些通道是独立处理的,但是深度应为CV_8U CV_16U, CV_16S, CV_32F或CV_64F。

  • param dst输出图像的大小和类型与src相同。

  • param ksize模糊核大小。

  • param锚点;默认值Point(-1,-1)表示锚点位于内核中央。

  • param borderType用于推断图像外部像素的边界模式,请参阅#BorderTypes

这其中BorderType有四种

  • BORDER_DEFAULT - (边界默认)openCV中默认的处理方法,自动填充图像边界(效果像是映像一样)

  • BORDER_CONSTANT – (边界常数)填充边缘用指定像素值,使用常数填充边界。

  • BORDER_REPLICATE – (边界复制)填充边缘像素用已知的边缘像素值,复制原图中最临近的行或者列。

  • BORDER_WRAP – (边界包装)用另外一边的像素来补偿填充。

写个测试代码

test=np.random.randint(1,5,[4,4,3])*10print(test.transpose(2,0,1))print("************************************")winSize=(3,3)blur_res=cv2.blur(test,winSize)print(blur_res.transpose(2,0,1))print("************************************")

运算结果

[[[40104010][10403010][30401030][30401040]][[30303010][10402040][10102020][20203010]][[10402030][40201030][20204010][10104030]]]************************************[[[27282426][29282423][34272820][37272818]][[30263026][23222423][20202323][13171821]][[28232319][26242423][19232328][17242432]]]

我们以最左上角为例,原始的值为40,而在blur后得到的是27,这个27是怎么得到的呢?

我们以40为中心,那么有效的值只有这4个值

40101040

但是我们的卷积核是3*3的卷积核,还有五个值就要补齐

401040104010401040

最后(40+10+40+10+40+10+40+10+40)/9=>27, 以此类推得到所有的值

不是很难的理论,但是经常容易忽略!

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询