栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516695.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号