How to estimate standard deviation in images with speckle noise?


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[0], img.shape[1], img.shape[2]).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:

enter image description here

according to my knowledge, the variance can be calculated in gamma distribution as follows:

enter image description here

so standard deviation or sigma is equivalent to:

enter image description here

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:

def add_speckle(sigma,img):

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[0],img.shape[1],img.shape[2]).astype('uint8')
  noise = img + img * gauss 
  return noise
  img = cv2.imread('/content/Hand.jpeg')

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 (1,1) or (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)

Leave a Reply

Your email address will not be published.