How can I add Rayleigh noise to an image in python?
Question:
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.
Answers:
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:
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:
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:
Contraharmonic Mean with p=2:
Contraharmonic Mean with p=3:
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
It does a nice job for the white noise, but leaves the black noise.
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.
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:
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:
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:
Contraharmonic Mean with p=2:
Contraharmonic Mean with p=3:
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
It does a nice job for the white noise, but leaves the black noise.