0

I have a images, each with a single value of 1 (delta) within it and previously known sigma. Reproduction of a single example:

img = np.zeros((40,40))
idx1 = np.random.randint(0, img.shape[0])
idx2 = np.random.randint(0, img.shape[1])
img [idx1, idx2] = 1

I wish to convolve each image with it's respected sigma value, such as in:

out_image = scipy.ndimage.filters.gaussian_filter(img, sigma, mode='constant')

The thing is since it is only a single delta, the output will just be to substitute the gaussian's values into the image, centered around the location of the delta. Will it be faster to implement this? If so, how do I generate the sigma filter? Maybe there is a faster sparse representation in skimage or cv2 which can make a faster job for me?

What will be the most efficient way (in terms of execution time), to repeatedly calculate such a case, given that the location of the delta and sigma size changes each time?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
havakok
  • 1,185
  • 2
  • 13
  • 45
  • If you know where your set pixel is, then obviously it's cheaper to only modify the few pixels in the vicinity of that one pixel. Computing the values of the Gaussian is very simple, just look up the equation. – Cris Luengo May 01 '22 at 08:59
  • numpy, slicing, just insert the gaussian in the right position. – Christoph Rackwitz May 01 '22 at 14:44

1 Answers1

0

why not construct a Gaussian...?

idx1 = np.random.randint(0, img.shape[0])
idx2 = np.random.randint(0, img.shape[1])

yg,xg = np.mgrid[range(0,img.shape[0]),range(0,img.shape[1])]
simga=5
img =np.exp(-0.5/sigma**2*((xg-idx2)**2+(yg-idx1)**2))
img=img/np.sum(img)

UPDATE

Since your Gaussian is out of bounds, it does not always sum to 1 (due to the tails), and your new image does not sums to 1. If you would like to consider boundary conditions, you would need to calculate the double integral sum on your Gaussian, which is not straightforward:

https://www.wolframalpha.com/input?i=integrate+%28integrate+e%5E%28-0.5%2Fs%5E2*%28%28x-x_0%29%5E2%2B%28y-y_0%29%5E2%29%29+dx+from+x%3D0+to+w+%29+dy+from+y%3D0+to+h

Mercury
  • 1,886
  • 5
  • 25
  • 44