I am using this function that I found on the web, to add speckle noise to images for research purposes:
def add_speckle(k,theta,img): gauss = np.random.gamma(k, theta, img.size) gauss = gauss.reshape(img.shape, img.shape, img.shape).astype('uint8') noise = img + img * gauss return noise
My issue is that I want to estimate/define the speckle noise I add as a standard deviation(sigma) parameter, and this function that I found depends on the gamma distribution or random.gamma() which it depends on the k,theta(shape,scale) parameters, which you can see in the gamma pdf equation down below:
according to my knowledge, the variance can be calculated in gamma distribution as follows:
so standard deviation or sigma is equivalent to:
I want to add speckle noise as sigma dependent, so am saying there should be a way to estimate that sigma from k,theta(shape,scale) that we make the input with, so the speckle_adding() function would look like something like this:
edited : for the answer in the comments :
def add_speckle(sigma,mean,img): theta = sigma ** 2 / mean k = mean / theta gauss = np.random.gamma(k,theta,img.size) gauss = gauss.reshape(img.shape,img.shape,img.shape).astype('uint8') noise = img + img * gauss print("k=",k) print("theta=",theta) return noise img = cv2.imread('/content/Hand.jpeg') cv2.imwrite('speckle12.jpg',add_speckle(8,3,img))
thanks sir for your help, but i really understand why k,theta values changes each time i change values of mean while sigma is constant, i think it must not changes??
As you have noticed that
sigma = k ** 0.5 * theta, there are infinite possibilities for parameters in the gamma distribution if only sigma is given (eg. if sigma is 1,
(k, theta) can be
(4, 0.5) and so on).
If you really want to generate the speckle with statistical inferences as input, I suggest you to add
mean as the second input so that the required
(k, theta) can be calculated.
The first moment (ie. mean) of a gamma distribution is simply
k * theta.
def add_speckle(mean, sigma, img): # find theta theta = sigma ** 2 / mean k = mean / theta # your code proceeds...
Answered By – tyson.wu
Answer Checked By – Katrina (AngularFixing Volunteer)