如何在Python中向图像添加瑞利噪声?

huangapple go评论56阅读模式
英文:

How can I add Rayleigh noise to an image in python?

问题

以下是翻译好的部分:

"我正在使用cv2进行以下图像处理:"

import numpy as np
import cv2

image = cv2.imread('./tomatoes.png', cv2.IMREAD_GRAYSCALE)

noise_std = 0.1
noise = np.random.rayleigh(noise_std, image.shape)
noisy_image = image + noise

cv2.imwrite('noisy_image.jpg', noisy_image)
cv2.imshow('Noisy Image', noisy_image)
cv2.waitKey(0)

问题是:当将噪音添加到图像时,我只收到一个白色窗口对话框。

英文:

I am doing the following image processing using cv2:

import numpy as np
import cv2

image = cv2.imread('./tomatoes.png',cv2.IMREAD_GRAYSCALE)

noise_std = 0.1
noise = np.random.rayleigh(noise_std, image.shape)
noisy_image = image + noise

cv2.imwrite('noisy_image.jpg', noisy_image)
cv2.imshow('Noisy Image', noisy_image)
cv2.waitKey(0)

The problem is: I only receive a white window dialog when the noise is added to the image.

答案1

得分: 3

以下是如何在Python/OpenCV中添加瑞利噪声的方法。您有一些问题。首先,将您的图像转换为浮点数,以匹配噪声生成的结果。其次,使用addWeighted来组合噪声和图像。由于瑞利噪声的振幅非常小,所以需要一个很大的权重。 (注意:我故意选择了一个非常大的权重,以使噪声非常明显)

import numpy as np
import cv2

img = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
image = img.astype(np.float64)

noise_std = 0.2
noise = np.random.rayleigh(noise_std, img.shape)

noisy_image = cv2.addWeighted(image, 1, noise, 70, 0.0).astype(np.uint8)

cv2.imwrite('lena_rayleigh_noise.png', noisy_image)

cv2.imshow('Image', img)
cv2.imshow('Noise', noise)
cv2.imshow('Noisy Image', noisy_image)
cv2.waitKey(0)

结果:

额外信息

以下是使用Imagemagick脚本"statsfilt"中的统计滤波器对上述带有瑞利噪声的图像进行滤波的结果,该脚本可在http://www.fmwconcepts.com/imagemagick/statsfilt/index.php找到。有关更多信息,请参见维基百科,例如,Lp均值https://en.wikipedia.org/wiki/Lp_space 和Contraharmonic均值https://en.wikipedia.org/wiki/Contraharmonic_mean。这两种方法似乎是其他方法中最好的,包括几何均值和调和均值。我建议将指数(p)设置为2或更高。

Lp均值,p=2:

Contraharmonic均值,p=2:

Contraharmonic均值,p=3:

Imagemagick还具有一个名为"enhance"的噪声降低函数。我进行了五次迭代:

convert lena_rayleigh_noise.png -enhance -enhance -enhance -enhance -enhance lena_rayleigh_noise_enhance5.png

它对白噪声做得很好,但留下了黑色噪声。

英文:

Here is how to add Rayleigh noise in Python/OpenCV. You have a couple of issues. First, convert your image to float to match the result from the noise generation. Second use addWeighted to combine noise and image. Since the Rayleigh noise amplitude is very small, it needs a large weight. (Note: I have purposely chosen a very large weight to make the noise very visible)

Input:

如何在Python中向图像添加瑞利噪声?

import numpy as np
import cv2

img = cv2.imread('lena.png',cv2.IMREAD_GRAYSCALE)
image = img.astype(np.float64)

noise_std = 0.2
noise = np.random.rayleigh(noise_std, img.shape)

noisy_image = cv2.addWeighted(image, 1, noise, 70, 0.0).astype(np.uint8)

cv2.imwrite('lena_rayleigh_noise.png', noisy_image)

cv2.imshow('Image', img)
cv2.imshow('Noise', noise)
cv2.imshow('Noisy Image', noisy_image)
cv2.waitKey(0)

Result:

如何在Python中向图像添加瑞利噪声?

ADDITION

Here are the results of filtering the above image with Rayleigh noise using statistical filters in Imagemagick script, statsfilt, at http://www.fmwconcepts.com/imagemagick/statsfilt/index.php. See Wikipedia, for example, Lp mean at https://en.wikipedia.org/wiki/Lp_space and Contraharmonic Mean at https://en.wikipedia.org/wiki/Contraharmonic_mean. These two seem to be the best from others including the geometric mean and harmonic mean. I recommend using the exponent (p) as positive 2 or higher.

Lp Mean with p=2:

如何在Python中向图像添加瑞利噪声?

Contraharmonic Mean with p=2:

如何在Python中向图像添加瑞利噪声?

Contraharmonic Mean with p=3:

如何在Python中向图像添加瑞利噪声?

Imagemagick also has a noise reduction function called enhance. I did five iterations:

convert lena_rayleigh_noise.png -enhance -enhance -enhance -enhance -enhance lena_rayleigh_noise_enhance5.png

如何在Python中向图像添加瑞利噪声?

It does a nice job for the white noise, but leaves the black noise.

huangapple
  • 本文由 发表于 2023年2月9日 02:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定