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

I am wondering if there exists some functions in Python with OpenCV or any other python image processing library that adds Gaussian or salt and pepper noise to an image? For example, in MATLAB there exists straight-forward functions that do the same job.

Or, how to add noise to an image using Python with OpenCV?

解决方案The Function adds gaussian , salt-pepper , poisson and speckle noise in an image

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值