opencv学习(4)--image smoothing

        学习时看的时英文文档。有些专业名词有时候靠自己的能力真的很难翻译的准确。尽量写清楚,但也会交杂些英文表述。

② Image Blurring(Image Smoothing)图像模糊

1.Averaging 平均化

       通过同均值滤波器来卷积图像来实现。它简单地通过取得所有像素在kernel area的平均值,并且用该平均值来取代其central element。

opencv提供了cv2.blur()、cv2.boxFilter()方法。

       指定一个3x3均值滤波器内核如下:(不想用均值滤波器也可以用cv2.boxFilter()传normalize = False参数)

                                      

      以下用的cv2.blur()附上用5X5kernel的demo:

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

img = cv2.imread("a.jpg")
blur = cv2.blur(img,(5,5))//第二个参数表示kernel的大小。
plt.subplot(121),plt.imshow(img),plt.title("Orignal")
plt.subplot(122),plt.imshow(blur),plt.title("Blurred")
plt.show()

实现后效果图:

2.Gaussian Filtering

       其可以非常高效地从图像移除Gaussian noise(Gaussian noise is statistical noise having a probability density function (PDF) equal to that of the normal distribution, which is also known as the Gaussian distribution.[1][2] In other words, the values that the noise can take on are Gaussian-distributed. )

     数字图像中高斯噪声的主要来源是在采集过程中产生的,例如由于光照不足和/或高温造成的传感器噪声,以及/或传输,如电子电路噪声。在数字图像处理中,可以使用空间滤波器来减少高斯噪声,但在平滑图像时,不良的结果可能会导致细尺度图像边缘和细节的模糊,因为它们也对应于阻塞的高频。传统的消除噪声的空间滤波技术包括:平均(卷积)滤波、中值滤波和高斯平滑。

       此高斯滤波,在需要一个gaussian kernel。指定kernel长宽的值需要时正奇数;同时也需要指定在X、Y方向上的标准差(如果只指定x,则y与x值一致;如果都为0,则由kernel计算得出)

         也可以直接由opencv的方法cv2.getGaussianKernel()来获得Gaussian Kernel.滤波方法为cv2.GaussianBlur(src,ksize,xsigma)

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

img = cv2.imread("a.jpg")
blur = cv2.GaussianBlur(img,(5,5),0)
plt.subplot(121),plt.imshow(img),plt.title("Orignal")
plt.subplot(122),plt.imshow(blur),plt.title("Blurred")
plt.show()

    3.Mdian Filtering 中值滤波器

       opencv的方法cv2.medianBlur()替代图像中所有像素值的中值,并且在central pixel 中用中值去替代它。

中值对处理salt-and pepper noise非常有效。区别与Gaussian and box filters的一点是,中值滤波器中替代原图像中值可能是原图像中的像素值。kernel size必须为正奇整数。

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

img = cv2.imread("p.png")
blur = cv2.medianBlur(img,5)#注意到了,kernel并非矩阵,是一个数值
plt.subplot(121),plt.imshow(img),plt.title("Orignal")
plt.subplot(122),plt.imshow(blur),plt.title("Blurred")
plt.show()

实现后效果图:

4.Bilateral Filtering 双边滤波器

     之前三个滤波器都是会在过滤时,同时模糊边缘。双边滤波器(cv2.bilateralFilter())可以在过滤的同时保留图像的边缘,但其操作处理的速度会更慢一些。

     高斯滤波器会获取周边的像素,并得出它的高斯加权平均值。高斯滤波是space alone,即它附近的所有像素点都会进行过滤,而不考虑像素是否强度一样,也不考虑像素是否在边缘,因此它处理后会模糊了边缘。

      双边滤波器使用了高斯滤波器的同时,还多用了个高斯滤波器组件(是一个像素强度差异方法)。高斯滤波器保证是空间上相邻的像素会被进行滤波,而高斯滤波器组件保证只有那些强度和central pixel相似的才会被包括进去计算模糊强度值。(while the Gaussian component applied in the intensity domain (a Gaussian function of intensity differences) ensures that only those pixels with intensities similar to that of the central pixel (‘intensity neighbors’) are included to compute the blurred intensity value.)

        因此,这种方法保留了边缘,因为在边缘附近的像素点,相邻的像素放置在边缘的另一端,因此与中心像素相比,显示出较大的强度变化,不会被包含在模糊中。

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

img = cv2.imread("b.jpg")
blur = cv2.bilateralFilter(img,9,75,75)
plt.subplot(121),plt.imshow(img),plt.title("Orignal")
plt.subplot(122),plt.imshow(blur),plt.title("Blurred")
plt.show()

          

      实现效果如下:

猜你喜欢

转载自blog.csdn.net/kathrynlala/article/details/82882941