如何使用OpenCV在Python中为图像添加噪声(高斯盐和胡椒粉等)

如何使用OpenCV在Python中为图像添加噪声(高斯盐和胡椒粉等),第1张

如何使用OpenCV在Python中为图像添加噪声(高斯/盐和胡椒粉等)

该功能会在图像中添加高斯,椒盐,泊松和斑点噪声

Parameters----------image : ndarray    Input image data. Will be converted to float.mode : str    One of the following strings, selecting the type of noise to add:    'gauss'     Gaussian-distributed additive noise.    'poisson'   Poisson-distributed noise generated from the data.    's&p'       Replaces random pixels with 0 or 1.    'speckle'   Multiplicative noise using out = image + n*image,where     n is uniform noise with specified mean & variance.import numpy as npimport osimport cv2def noisy(noise_typ,image):   if noise_typ == "gauss":      row,col,ch= image.shape      mean = 0      var = 0.1      sigma = var**0.5      gauss = np.random.normal(mean,sigma,(row,col,ch))      gauss = gauss.reshape(row,col,ch)      noisy = image + gauss      return noisy   elif noise_typ == "s&p":      row,col,ch = image.shape      s_vs_p = 0.5      amount = 0.004      out = np.copy(image)      # Salt mode      num_salt = np.ceil(amount * image.size * s_vs_p)      coords = [np.random.randint(0, i - 1, int(num_salt))   for i in image.shape]      out[coords] = 1      # Pepper mode      num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))      coords = [np.random.randint(0, i - 1, int(num_pepper))   for i in image.shape]      out[coords] = 0      return out  elif noise_typ == "poisson":      vals = len(np.unique(image))      vals = 2 ** np.ceil(np.log2(vals))      noisy = np.random.poisson(image * vals) / float(vals)      return noisy  elif noise_typ =="speckle":      row,col,ch = image.shape      gauss = np.random.randn(row,col,ch)      gauss = gauss.reshape(row,col,ch)   noisy = image + image * gauss      return noisy


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5507873.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存