【问题标题】:How to add noise using skimage如何使用 skimage 添加噪声
【发布时间】:2020-05-09 08:59:09
【问题描述】:

我写了一个简单的代码来给图像添加噪声:

import cv2
from skimage.util import *
img = cv2.imread("./A/0030050944.jpg")

img = random_noise(img, mode='poisson', seed=42, clip=False)

cv2.imwrite("test.jpg", img)

但这只会给出一个空白的黑色图像。

【问题讨论】:

标签: python scikit-image


【解决方案1】:

检查此代码。它在图像中添加高斯、椒盐、泊松和散斑噪声。

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 np
import os
import cv2
def 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

来自How to add noise (Gaussian/salt and pepper etc) to image in Python with OpenCV

位置噪音 从 PIL 导入图像 将 numpy 导入为 np 从 skimage.util 导入 random_noise

im = Image.open("test.jpg")
# convert PIL Image to ndarray
im_arr = np.asarray(im)

# random_noise() method will convert image in [0, 255] to [0, 1.0],
# inherently it use np.random.normal() to create normal distribution
# and adds the generated noised back to image
noise_img = random_noise(im_arr, mode='possion', var=0.05**2)
noise_img = (255*noise_img).astype(np.uint8)

img = Image.fromarray(noise_img)
img.show()

【讨论】:

  • 你能解释一下斑点背后的直觉吗?
  • 散斑是一种颗粒干扰,固有地存在于有源雷达、合成孔径雷达 (SAR)、医学超声和光学相干断层扫描图像中,会降低其质量。
  • 如何更改泊松函数。我试过了,它返回几乎相同的图像
  • 21 # 本质上它使用 np.random.normal() 创建正态分布 22 # 并将生成的噪声添加回图像 ---> 23 noise_img = random_noise(im_arr, mode='poisson' , var=0.05**2) 24 noise_img = (255*noise_img).astype(np.uint8) 25 123 if key not in allowedkwargs[allowedtypes[mode]]: 124 raise ValueError('%s keyword not in allowed keywords % s' % --> 125 (key, allowedkwargs[allowedtypes[mode]])) 126 127 # 设置 kwarg 默认值 ValueError: var keyword not in allowed keywords []
猜你喜欢
  • 1970-01-01
  • 2011-08-16
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多