名词解释
图像噪声:图像在产生、传输等过程中被其他因素干扰或由于某些原因出现数据丢失,出现的某些像素点明显异于周围像素点的现象,称作图像噪声;
图像平滑:为了降低噪声对图像质量带来的影响,对图像进行区域增强的算法,“平滑”可以理解为降低噪点与周围正常点差异,抚平像素值的显著跃迁;
人工增噪
import cv2
import numpy as np
import matplotlib.pyplot as plt
def read_image(path, mode):
image = cv2.imread(path, mode)
return image
def add_noise(img, noise_point_cnt=10000):
height, width = img.shape[0], img.shape[1]
for i in range(noise_point_cnt):
x = np.random.randint(0, height)
y = np.random.randint(0, width)
img[x, y, :] = 255
return img
def show_source_blurred(img, blur_img):
images = [img, blur_img]
titles = ['Source', 'Blurred']
for i in range(2):
plt.subplot(1, 2, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
滤波算法
均值滤波
对于每个需要被增强的点,定义一个邻域K,如点A(10,20),定义邻域大小(3,3),那么该点的领域为以B1(9,19)为左上角,B2(11,21)为右下角的矩形合围区域;
(8,18) | (8,19) | (8,20) | (8,21) | (8,22) |
(9,18) | (9,19) | (9,20) | (9,21) | (9,22) |
(10,18) | (10,19) | (10,20) | (10,21) | (10,22) |
(11,18) | (11,19) | (11,20) | (11,21) | (11,22) |
(12,18) | (12,19) | (12,20) | (12,21) | (12,22) |
增强后的点A新值=邻域内所有点的均值;
def compare_simple_blur(img):
blur_img = cv2.blur(img, (3, 3))
show_source_blurred(img, blur_img)
方框滤波
划定邻域,将邻域内所有点求和,作为增强后的值;
Python-OpenCV中boxFilter方法中的normalize参数,0代表直接求和;1代表正规化,即对和平均,normalize=1效果等同于均值滤波;
def compare_box_filter(img):
blur_img = cv2.boxFilter(img, -1, (3, 3), normalize=0)
show_source_blurred(img, blur_img)
高斯滤波
高斯滤波相当于给邻域内所有点加了个权重,离A越近的点,对A的影响越大;
权重分布服从于高斯分布;
def compare_gaussian_blur(img):
blur_img = cv2.GaussianBlur(img)
show_source_blurred(img, blur_img)
中值滤波
首先邻域大小必须为奇数,邻域内所有点按值排序;
增强后的点A的新值=邻域内所有点的中值;
def compare_median_blur(img):
blur_img = cv2.medianBlur(img)
show_source_blurred(img, blur_img)