Reputation: 229
My question is simple: I have an image and I want to add pixel wise independent noise to the image. The noise can be derived from from any distribution such as Gaussian. What are the available modules in numpy/scikit-learn to do the same?
I do not have any code but I am learning about modules such as numpy.random.normal, etc. and I needed more clarification. None of the modules explicitly say that if I draw samples from a distribution multiple times, the draws will be independent.
Thank you for suggestions.
Upvotes: 0
Views: 857
Reputation: 330
You have several options. If you want to take random samples with replacement, just use one of the numpy's builtin random modules (i.e., numpy.random.random). You could also use numpy.random.pareto for more dramatic/bursty noise. These methods generate independent samples.
If you have a distribution in the form of a set or array that you want to pull samples from without repetition (for instance you have an array [0.1, 0.3, 0.9] and want to generate noise with ONLY these values), you use python's builtin random.random.choice([0.1, 0.3, 0.9]) to draw independent samples from your custom distribution. You can also specify replace=False
.
Upvotes: 1
Reputation: 1988
Yes, random means Independent. You can use numpy/scipy to generate noise and add it to the image. Perhaps it's good for you to study this tutorial
Here the code:
import numpy as np
import matplotlib.pylab as plt
#--- data -----
a = 1
xi, yi = np.linspace(-a,a,nx), np.linspace(-a,a,ny)
x, y = np.meshgrid(xi,yi) # 2-dimensional grid
U = np.exp(-x*x - y*y) # picture/signal
V = np.random.randn(nx, ny) # random noise
#--- grafics -----
fig = plt.figure(figsize=(22,11))
ax1 = fig.add_subplot(131)
ax1.imshow(U)
ax2 = fig.add_subplot(132)
ax2.imshow(V)
ax3 = fig.add_subplot(133)
ax3.imshow(U+0.2*V)
plt.show()
fig.savefig('signal_noise.png', transparency=True)
Upvotes: 0