失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 210_Python+OpenCV_04_模糊(均值 中值 高斯)

210_Python+OpenCV_04_模糊(均值 中值 高斯)

时间:2022-09-20 02:28:50

相关推荐

210_Python+OpenCV_04_模糊(均值 中值 高斯)

一、 均值模糊

1. 基于平均值

# -*- coding:utf-8 -*-# Linda Li /8/19 9:08 cv_22_均值模糊 PyCharmimport cv2 as cvimport numpy as npdef 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 /8/19 9:08 cv_22_均值模糊 PyCharmimport cv2 as cvimport numpy as npdef clamp(pv):"""防止超出255"""if pv > 255:return 255elif pv < 0:return 0else:return pvdef gaussian_noise(image):"""给图像加高斯噪声"""h, w, c = image.shape# 其实是for row in range(0, h, 1)从0到h步幅是1for row in range(h):for col in range(w):s = np.random.normal(0, 20, 3)# blue green redb = 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 /8/19 9:08 cv_22_均值模糊 PyCharmimport cv2 as cvimport numpy as npdef 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 /8/19 9:08 cv_22_均值模糊 PyCharmimport cv2 as cvimport numpy as npdef custom_blur_demo(image):"""自定义,轻微模糊"""# f=5,5*5 保证不会溢出,最大是255kernel = np.ones([5, 5], np.float32)/25dst = cv.filter2D(image, -1, kernel=kernel)cv.imshow("custom_blur_5", dst)# kernel1 = np.ones([3, 3], np.float32) / 9kernel1 = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], np.float32) / 9dst = 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()

上图是锐化的图

如果觉得《210_Python+OpenCV_04_模糊(均值 中值 高斯)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。