opencv-python- study notes eleven (image smoothing and filtering)

Two-dimensional convolution (image filtering)

Like the one-dimensional signal, the image may be in a variety of low-pass filters (the LPF), a high pass filter (HPF) for filtering the like. LPF help remove noise, blurred images. HPF filters help find edges in the image. OpenCV provides a function cv.filter2D () to the kernel and convolving the image. For example, we will try to average image filtering. A 5x5 average filter kernel as follows:

As follows: the core remains above a pixel, the core under all 25 pixels are added and averaged, and then replaced with a new average pixel center. Continue to continue this operation for all the pixels in the image.

function

dst=cv.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]])

parameter:

src The input image
dst The same size as src, the same number of channels of the output image.
ddepth The required depth of the target image. -1 the depth of the original image. See  Combinations  
kernel Convolution kernel, a single-channel floating-point matrix; If you want to use different channels for different convolution kernels, using a split the image into separate color plane, and process them separately.                                                                          
anchor Core anchor showing the relative position of the point of the filtered kernel; anchor should be in the kernel; the default value (-1, -1) represents the center of the core i.e. the anchor.
delta Before storing the target image is added to an optional value of the pixel, the default value is 0
borderType Border types, See  BorderTypes

 

For example:

Import numpy AS NP
 Import CV2 AS CV
 from matplotlib Import pyplot AS PLT 
IMG = cv.imread ( ' 2.jpg ' )
 # convolution kernel, a single-channel floating-point matrices 
kernel = np.ones ((5, 5 ), np .float32) / 25 
DST = cv.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 ( ' the Averaging ' ) 
plt.xticks ([]), plt.yticks ([]) 
plt.show ()

 

 

Image blur (image smoothing)

 Image blur is accomplished by low pass filtering the image with a convolution nuclei. It is useful to eliminate noise. It effectively deletes high frequency content in the image (such as: noise, edge). So in this operation, the edge a little fuzzy. OpenCV provides four main types of fuzzy technology.

Average blur

 Implemented by the normalized convolution box. He just cover an area average of the pixel block to replace with a convolutional the central element, may be a function cv.blur or () cv.boxFilter completed. We need to set the width and height convolution box. The following is a normalized convolution block, also known as average filter kernel:

 

For example, a 3 * 3 convolution block is:

If you do not want to use the normalized convolution box, you should use cv2.boxFilter (), this time to pass parameters normalize = False.

function:

dst=cv.blur(src, ksize[, dst[, anchor[, borderType]]])

parameter:

src Input image, it can have any number of channels, are processed independently. But the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
dst Output image, and src from the same size and type
ksiz to The average size of the convolution kernel, the tuple (x, y) which represents the x * y matrix                                                                                                           
anchor Anchor, the reference point of the core; the default value of the point (- 1,1), the center of the anchor in the kernel.
borderType Border types, See  BorderTypes

transfer blur(src, dst, ksize, anchor, borderType)函数相当于调用 boxFilter(src, dst, src.type(), anchor, true, borderType).

Gaussian blur

在这种情况下,用高斯核函数代替了盒形滤波器。这是通过函数cv.GaussianBlur()完成的。我们应该指定内核的宽度和高度,它们应该是正的奇数。我们还应该分别指定X和Y方向的标准差,sigmaX和sigmaY。如果只指定sigmaX,则sigmaY与sigmaX相同。如果两者都是零,那么函数会根据核函数的大小自己计算。高斯滤波可以有效的从 图像中去除高斯噪音。

函数:

dst=cv.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]])使用高斯滤波器模糊图像

参数:

 

src 输入图像;图像可以有任意数量的通道,这些通道是独立处理的,但是深度应该是CV_8U、CV_16U、CV_16S、CV_32F或CV_64F。
dst 与src有相同大小和类型的输出图像
ksize 高斯卷积核尺寸,元祖(x,y). ksize.width 和 ksize.height 可以是不同的 但必须是正的奇数. 或者, 它们可以为0然后由计算得到.
sigmaX 高斯核在x方向的标准差
sigmaY 高斯核在y方向的标准差(sigmaY=0时,其值自动由sigmaX确定(sigmaY=sigmaX);sigmaY=sigmaX=0时,它们的值将由ksize.width和ksize.height自动确定)                                                    
borderType 像素边界类型, see BorderTypes

 

中值模糊

就是用与卷积框对应像素的中值来替代中心像素的值。这个滤波器经常用来去除椒盐噪声。前面的滤波器都是用计算得到的一个新值来取代中 心像素的值,而中值滤波是用中心像素周围(也可以使他本身)的值来取代他。kernel size的大小也应该是一个正奇数。

函数:

dst=cv.medianBlur(src, ksize[, dst])使用中值滤波器模糊图像。

该函数使用ksize×ksize孔径的中值滤波器平滑图像。多通道图像的每个通道都是独立处理的。支持就地操作。中值过滤器在内部使用border_replication来处理边界像素,请参见BorderTypes

参数:

src 输入1-,3-,4-通道的图像; 当ksize 是 3 或 5时,图像的深度应该时 CV_8U, CV_16U, or CV_32F, 如果孔径更大, 它只可能是CV_8U.
dst 与src大小和类型相同的输出图像                                                                                                                    
ksize 孔径大小,必须是奇数且大于1, for example: 3, 5, 7 ...

 

双边滤波

cv.bilateralFilter在保持边缘清晰的同时,对噪声去除非常有效。但与其他过滤器相比,该操作速度较慢。们已经知道高斯滤波器是求 中心点邻近区域像素的高斯加权平均值。这种高斯滤波器只考虑像素之间的空间关系,而不会考虑像素值之间的关系(像素的相似度,所以这种方法不会考 虑一个像素是否位于边界。因此边界也会别模糊掉,而这并不是我们想要的。

 

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

函数:

dst=cv.bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]])

参数:

src 8-bit or floating-point, 1-channel or 3-channel image.
dst 与原图像具有相同大小和类型的输出图像
d 用于滤波的每个像素邻域的直径. 如果这个值是负数,则根据 sigmaSpace自动计算
sigmaColor 色空间过滤器的sigma值. 值越大 表明该像素邻域内(see sigmaSpace)更多的颜色被混合到一起,产生较大的半相等颜色区域
sigmaSpace 坐标空间中滤波器的sigma值,参数值越大,表示距离越远的像素只要颜色足够接近就会相互影响,从而使更大的区域中足够相似的颜色获取相同的颜色。当d>0时,d指定了邻域大小且与sigmaSpace无关,否则d正比于sigmaSpace.                           
borderType 边界类型, see BorderTypes

 

综合案例

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

img = cv2.imread('4.jpg')  # 注意图片尺寸必须是卷积核的整倍,否则出错

# 2D卷积滤波
kernel = np.ones((3, 3), np.float32)/9  # 创建一个9*9的平均滤波器核
dst = cv2.filter2D(img, -1, kernel)

# 平均模糊
blur = cv2.blur(img, (5, 5))

# 高斯滤波
gauss = cv2.GaussianBlur(img, (5, 5), 0)

# 中值滤波
median = cv2.medianBlur(img, 5)

# 双边滤波
bila = cv2.bilateralFilter(img, 10, 200, 200)

plt.subplot(231), plt.imshow(img), plt.title('Original')
plt.subplot(232), plt.imshow(dst), plt.title('Filter2D')
plt.subplot(233), plt.imshow(blur), plt.title('Averaging')
plt.subplot(234), plt.imshow(gauss), plt.title('GuassianBlur')
plt.subplot(235), plt.imshow(median), plt.title('MedianBlur')
plt.subplot(236), plt.imshow(bila), plt.title('BilateralFilter')

plt.show()

 

Guess you like

Origin www.cnblogs.com/blog-xyy/p/11260486.html