0

I believe code below is somewhat correct implementation of this exponential heatmap function:

enter image description here

def expfunc(image, landmark, sigma=6): #image = array of shape (512,512), landmark = array of shape (2,)
  a= np.sqrt(np.log(2)/2)/sigma # 
  for i in range(image.shape[0]):
    for j in range(image.shape[1]):
      prob = np.exp(-a*(np.abs(i-landmark[0])+np.abs(j-landmark[1])))
      if prob > 0.01:
        image[i][j] = prob
      else:
        image[i][j]= 0
  return image

My questions are:

  1. How could I vectorize this code?
  2. This probability function gives values to all pixels so how should proceed with very small values? Now I am using threshold of 0.01 for zeros?
Ode
  • 19
  • 1
  • 6

1 Answers1

0

Let me know if this works for you:

i = np.arange(image.shape[0])
j = np.arange(image.shape[1])
prob = np.exp(-a*(np.abs(i[:,None]-landmark[0])+np.abs(j-landmark[1])))
image = np.where(prob>0.01, prob, 0)

First compute the array prob for all of the indices i and j. Then prob has the same shape as image, and you can redefine image based on the values of prob using numpy.where.