一、 均值模糊
1. 基于平均值
# -*- coding:utf-8 -*-
# Linda Li 2019/8/19 9:08 cv_22_均值模糊 PyCharm
import cv2 as cv
import numpy as np
def blur_demo(image):
"""均值模糊——用于随机去噪"""
# 定义一个一行三列的卷积盒(1,3)代表x和y方向,y方向上15个像素的长度的模糊
dst = cv.blur(image, (1, 15))
cv.imshow("blur_y", dst)
# x方向上15个像素的模糊
dst1 = cv.blur(image, (15, 1))
cv.imshow("blur_x", dst1)
# 经常用到的是5*5=f*f的卷积盒
dst2 = cv.blur(image, (5, 5))
cv.imshow("blur_5", dst2)
print("------hello python-------")
src = cv.imread("../cv_02/cv_23.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
blur_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
2、高斯模糊Gaussion_去高斯噪声
横着的
周围的红色的因为边缘的原因不计算
( 151+ 202 + 251 ) / 4 = 20
( 201+ 252 + 251 ) / 4 = 95 / 4 = 23.75
( 251+ 252 + 15*1 ) / 4 = 90 / 4 = 22.5
竖着的
( 151+ 202 + 201 ) / 4 = 75 / 4 = 18,75
( 201+ 202 + 201 ) / 4 = / 4 = 20
( 201+ 202 + 15*1 ) / 4 = 75 / 4 = 18.75
== 2. 基于高斯的权重的均值模糊**==
高斯模糊的去噪能力更好
拆开是为了提速,
# -*- coding:utf-8 -*-
# Linda Li 2019/8/19 9:08 cv_22_均值模糊 PyCharm
import cv2 as cv
import numpy as np
def clamp(pv):
"""防止超出255"""
if pv > 255:
return 255
elif pv < 0:
return 0
else:
return pv
def gaussian_noise(image):
"""给图像加高斯噪声"""
h, w, c = image.shape
# 其实是for row in range(0, h, 1)从0到h步幅是1
for row in range(h):
for col in range(w):
s = np.random.normal(0, 20, 3)
# blue green red
b = image[row, col, 0]
g = image[row, col, 1]
r = image[row, col, 2]
# 加上噪音
image[row, col, 0] = clamp(b + s[0])
image[row, col, 1] = clamp(g + s[1])
image[row, col, 2] = clamp(r + s[2])
cv.imshow("noise image", image)
print("------hello python-------")
src = cv.imread("../cv_02/cv_193.jpeg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
# 调用高斯噪音的函数,并且计算加噪音用了多长的时间
t1 = cv.getTickCount()
# gaussian_noise(src)
t2 = cv.getTickCount()
# 除以单位值,返回的是毫秒
time = (t2 - t1)/cv.getTickFrequency()
print("time consume: %s" % (time * 1000))
# 调用高斯模糊的函数
dst = cv.GaussianBlur(src, (0, 0), 15)
cv.imshow("Gussian Blur", dst)
cv.waitKey(0)
cv.destroyAllWindows()
二、中值模糊——用于去椒盐噪声
# -*- coding:utf-8 -*-
# Linda Li 2019/8/19 9:08 cv_22_均值模糊 PyCharm
import cv2 as cv
import numpy as np
def median_blur_demo(image):
"""中值模糊——用于去椒盐噪声"""
dst = cv.medianBlur(image, 5)
cv.imshow("median_blur", dst)
print("------hello python-------")
src = cv.imread("../cv_02/cv_23.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
median_blur_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
三、自定义模糊
# -*- coding:utf-8 -*-
# Linda Li 2019/8/19 9:08 cv_22_均值模糊 PyCharm
import cv2 as cv
import numpy as np
def custom_blur_demo(image):
"""自定义,轻微模糊"""
# f=5,5*5 保证不会溢出,最大是255
kernel = np.ones([5, 5], np.float32)/25
dst = cv.filter2D(image, -1, kernel=kernel)
cv.imshow("custom_blur_5", dst)
# kernel1 = np.ones([3, 3], np.float32) / 9
kernel1 = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], np.float32) / 9
dst = cv.filter2D(image, -1, kernel=kernel1)
cv.imshow("custom_blur_3", dst)
# 锐化
kernel2 = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
dst = cv.filter2D(image, -1, kernel=kernel2)
cv.imshow("custom_blur_rui", dst)
print("------hello python-------")
src = cv.imread("../cv_02/cv_193.jpeg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
custom_blur_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
上图是锐化的图