OpenCV-Python 中文教程11——图像平滑

OpenCV-Python 中文教程11——图像平滑

目标

学习使用不同的低通滤波器对图像进行模糊

使用自定义的滤波器对图像进行卷积(2D 卷积)

1、2D 卷积

       与以为信号一样, 可以对 2D 图像实施低通滤波(LPF),高通滤波(HPF)等。 LPF 帮助我们去除噪音,模糊图像。 HPF 帮助我们找到图像的边缘。OpenCV 提供的函数 cv.filter2D() 可以对一幅图像进行卷积操作。下面将对一幅图像使用平均滤波器,如一个 5x5 的平均滤波器核:

       操作如下:将核放在图像的一个像素 A 上,求与核对应的图像上 255x5)个像素的和,在取平均数,用这个平均数替代像素 A 的值。重复以上操作直到将图像的每一个像素值都更新一边。 
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('table.jpg')
kernel = np.ones((5,5),np.float32)/25
#cv.Filter2D(src, dst, kernel, anchor=(-1, -1))
#ddepth –desired depth of the destination image;
#if it is negative, it will be the same as src.depth();
#the following combinations of src.depth() and ddepth are supported:
#src.depth() = CV_8U, ddepth = -1/CV_16S/CV_32F/CV_64F
#src.depth() = CV_16U/CV_16S, ddepth = -1/CV_32F/CV_64F
#src.depth() = CV_32F, ddepth = -1/CV_32F/CV_64F
#src.depth() = CV_64F, ddepth = -1/CV_64F
#when ddepth=-1, the output image will have the same depth as the source.
dst = cv2.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:

2、图像模糊(图像平滑)

        使用低通滤波器可以达到图像模糊的目的,这对与去除噪音很有帮助。其实就是去除图像中的高频成分(比如:噪音,边界)。所以边界也会被模糊一点。(当然,也有一些模糊技术不会模糊掉边界)。 OpenCV 提供了四种模糊技术。

2.1 平均

        这是由一个归一化卷积框完成的,用卷积框覆盖区域所有像素的平均值来代替中心元素。可以使用函数 cv2.blur() cv2.boxFilter() 来完成。可通过查看文档了解更多卷积框的细节,调用时只需要设定卷积框的宽和高。下面是一个 3x3 的归一化卷积框:(如果不想使用归一化卷积框,可使用 cv2.boxFilter(),这时要传入参数 normalize=False


代码:

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('table.jpg')
blur = cv2.blur(img,(5,5))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

运行结果:


2.2 高斯模糊

       现在把卷积核换成高斯核(简单来说,方框不变,将原来每个方框的值是相等的,现在里面的值是符合高斯分布的,方框中心的值最大,其余方框根据距离中心元素的距离递减,构成一个高斯小山包。原来的求平均数现在变成求加权平均数,全就是方框里的值)。实现的函数是 cv2.GaussianBlur()。需要指定高斯核的宽和高(必须是奇数)。以及高斯函数沿 XY 方向的标准差。如果只指定了 X 方向的的标准差, Y 方向也会取相同值。如果两个标准差都是 0,那么函数会根据核函数的大小自己计算。高斯滤波可以有效的从图像中去除高斯噪音。

       当然也可以使用函数 cv2.getGaussianKernel() 自己构建一个高斯核。如果要使用高斯模糊的话,上边的代码应该写成:

#0 是指根据窗口大小(5,5)来计算高斯函数标准差
blur = cv2.GaussianBlur(img,(5,5),0)

2.3 中值模糊

       用与卷积框对应像素的中值来替代中心像素的值。这个滤波器经常用来去除椒盐噪声。前面的滤波器都是用计算得到的一个新值来取代中心像素的值,而中值滤波是用中心像素周围的值来取代它。卷积核的大小也应该是一个奇数。在这个例子中,如果给原始图像加上 50% 的噪声然后再使用中值模糊。

      在OpenCV中并没有像MATLAB中提供现成可以使用的噪声函数,比如在MATLAB中我们可以使用salt & pepper(),gaussian(),等函数方便的添加椒盐噪声和高斯噪声,因此需要自己编写一个尽量贴近于MATLAB风格的椒盐噪声函数。参考博客:https://blog.csdn.net/KimLK/article/details/78261809

       完整代码为:

import cv2
import numpy as np
from matplotlib import pyplot as plt

def saltpepper(img,n):
    m=int((img.shape[0]*img.shape[1])*n)
    for a in range(m):
        i=int(np.random.random()*img.shape[1])
        j=int(np.random.random()*img.shape[0])
        if img.ndim==2:
            img[j,i]=255
        elif img.ndim==3:
            img[j,i,0]=255
            img[j,i,1]=255
            img[j,i,2]=255
    for b in range(m):
        i=int(np.random.random()*img.shape[1])
        j=int(np.random.random()*img.shape[0])
        if img.ndim==2:
            img[j,i]=0
        elif img.ndim==3:
            img[j,i,0]=0
            img[j,i,1]=0
            img[j,i,2]=0
    return img


img = cv2.imread('table.jpg')
saltImage=saltpepper(img,0.02)
median = cv2.medianBlur(saltImage,5)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(median),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()
运行结果:

2.4 双边滤波

      函数 cv2.bilateralFilter() 能在保持边界清晰的情况下有效的去除噪音。但是这种操作与其他滤波器相比会比较慢。由于高斯滤波器是求中心点邻近区域像素的高斯加权平均值,高斯滤波器只考虑像素之间的空间关系,而不考虑像素值之间的关系(像素的相似度),所以这种方法不会考虑一个像素是否位于边界,以至于边界也会被模糊掉,而这并不是我们想要的结果。

       双边滤波在同时使用空间高斯权重和灰度值相似性高斯权重。空间高斯函数确保只有邻近区域的像素对中心点有影响,灰度值相似性高斯函数确保只有与中心像素灰度值相近的才会被用来做模糊运算。所以这种方法会确保边界不会被模糊掉,因为边界处的灰度值变化比较大。代码如下:

#cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace)
#d – Diameter of each pixel neighborhood that is used during filtering.
# If it is non-positive, it is computed from sigmaSpace
#9 邻域直径,两个 75 分别是空间高斯函数标准差,灰度值相似性高斯函数标准差
blur = cv2.bilateralFilter(img,9,75,75)

运行结果:


更多资源——双边滤波:http://people.csail.mit.edu/sparis/bf_course/

猜你喜欢

转载自blog.csdn.net/zichen_ziqi/article/details/80846813