1.加高斯噪声
import cv2 as cv
import numpy as np
# 定义一个函数,把加上随机数后的像素值限制在0-255
def clamp(pv):
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
def gaussian_noise(image):
h, w, c = image.shape
# 每一行每一列加入随机数得到高斯噪声
for row in range(h):
for col in range(w):
s = np.random.normal(0, 20, 3) # 每次产生3个随机数,给三个通道用
b = image[row, col, 0] # blue
g = image[row, col, 1] # green
r = image[row, col, 2] # red
image[row, col, 0] = clamp(b + s[0]) # 把随机值加到原像素值上
image[row, col, 1] = clamp(b + s[1])
image[row, col, 2] = clamp(b + s[2])
cv.imshow("noise image", image)
src = cv.imread("C:/Users/ASUS/Desktop/11/2.jpg") # 从目录中读取图片
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.imshow("input image", src) # 在窗口中将图片显示出来,通过名字“src”找到图片
gaussian_noise(src)
cv.waitKey(0)
cv.destroyAllWindows()
输出:
2.对含有高斯噪点的图像进行高斯迷糊
import cv2 as cv
import numpy as np
# 定义一个函数,把加上随机数后的像素值限制在0-255
def clamp(pv):
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
def gaussian_noise(image):
h, w, c = image.shape
# 每一行每一列加入随机数得到高斯噪声
for row in range(h):
for col in range(w):
s = np.random.normal(0, 20, 3) # 每次产生3个随机数,给三个通道用
b = image[row, col, 0] # blue
g = image[row, col, 1] # green
r = image[row, col, 2] # red
image[row, col, 0] = clamp(b + s[0]) # 把随机值加到原像素值上
image[row, col, 1] = clamp(b + s[1])
image[row, col, 2] = clamp(b + s[2])
cv.imshow("noise image", image)
src = cv.imread("C:/Users/ASUS/Desktop/11/2.jpg") # 从目录中读取图片
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.imshow("input image", src) # 在窗口中将图片显示出来,通过名字“src”找到图片
gaussian_noise(src)
dst = cv.GaussianBlur(src, (0, 0), 15) # ksize=(0,0) 和为15
cv.imshow("Gaussian Blur", dst)
cv.waitKey(0)
cv.destroyAllWindows()
输出:
5*5高斯迷糊——对高斯噪声有很好的去除作用
import cv2 as cv
import numpy as np
# 定义一个函数,把加上随机数后的像素值限制在0-255
def clamp(pv):
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
def gaussian_noise(image):
h, w, c = image.shape
# 每一行每一列加入随机数得到高斯噪声
for row in range(h):
for col in range(w):
s = np.random.normal(0, 20, 3) # 每次产生3个随机数,给三个通道用
b = image[row, col, 0] # blue
g = image[row, col, 1] # green
r = image[row, col, 2] # red
image[row, col, 0] = clamp(b + s[0]) # 把随机值加到原像素值上
image[row, col, 1] = clamp(b + s[1])
image[row, col, 2] = clamp(b + s[2])
cv.imshow("noise image", image)
src = cv.imread("C:/Users/ASUS/Desktop/11/2.jpg") # 从目录中读取图片
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE) # 通过opencv的GUI将图片显示出来
cv.imshow("input image", src) # 在窗口中将图片显示出来,通过名字“src”找到图片
gaussian_noise(src)
dst = cv.GaussianBlur(src, (5, 5), 0) # ksize=(0,0) 和为15
cv.imshow("Gaussian Blur", dst)
cv.waitKey(0)
cv.destroyAllWindows()
输出:
文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890
Python Free
邮箱:417803890@qq.com
QQ:417803890
皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved
微信扫一扫关注公众号:
Python Free