# Drawing gaussian spots into a texture

53 views

### paxcalpt

Dec 9, 2011, 8:31:12 AM12/9/11
to glumpy-users
Hi Nicolas,

I was wondering if you could give me an advice.
I have a np array with X and Y coordinates of spots that I would like
to draw as gaussians into a texture. Do you have an advice of how to
do this in glumpy?

Also, if we now make the spots 3D, is there to make them a gaussian
whose diameter is larger in the Z-axis compared to the X or Y?

Thanks...
Best,
-R

### Nicolas Rougier

Dec 9, 2011, 8:34:46 AM12/9/11
to glumpy-users

I'm not sure of what you mean by "drawi as Gaussians". Do you mean
that each x,y couple is the center of Gaussian ?

Nicolas

### Ricardo Henriques

Dec 9, 2011, 8:52:19 AM12/9/11
Hi,
Yes, that's it. Each X,Y corresponds to a gaussian centered on those
coordinates with a given (predefined) radius.

--
Ricardo Henriques, PhD
Institut Pasteur (Paris, France).

### Nicolas Rougier

Dec 9, 2011, 9:15:33 AM12/9/11
to glumpy-users

Does this work ?

import numpy as np
import matplotlib.pyplot as plt

def gaussian(d, sigma=1.0):
return 1.0/(sigma**2*np.pi)*np.exp(-d**2/(sigma**2))

p = 10
n = 256
xmin, xmax = -1.0, +1.0
ymin, ymax = -1.0, +1.0
dx = (xmax-xmin)/n
dy = (ymax-ymin)/n
X,Y = np.meshgrid(np.arange(xmin,xmax,dx),
np.arange(ymin,ymax,dy))
Xg = np.random.uniform(xmin,xmax,p)
Yg = np.random.uniform(xmin,xmax,p)
Z = np.zeros((n,n), dtype=np.float32)

for i in range(p):
D = np.sqrt((X-Xg[i])**2+(Y-Yg[i])**2)

# Z += gaussian(D, sigma=0.1)

# ... or take maximum
Z = np.maximum(Z, Z+gaussian(D, sigma=0.1))

plt.imshow(Z)
plt.show()

### Ricardo Henriques

Dec 9, 2011, 9:29:09 AM12/9/11
The problem was that I was trying to run away from the matplotlib and
do this in glumpy opengl.

Here's my options as I see them:
- Draw a gaussian into a numpy matrix, convert into texture, then
apply the texture to each spot in space.
or
- Use a glPoint for each spot, enlarge them via glPointSize to have
the gaussian size, but then apply some kind of intensity decay coming
from the center of the spot (don't know how to do this final part).

Currently I'm drawing million of gaussian-shaped spots into numpy
arrays then plotting them through glumpy, I do this in Cython - being
already reasonably fast, but it would even be faster if I directly
used GL to render the spots.
Here's how I'm doing it:

for p in range(nspots):
... # grab x0, x1, y0, y1 as the vicinities of the spots
... # grab xc, yc as the centers of the spot
for i in range(x0, x1):
for j in range(y0, y1):
# here comes the 2d gaussian equation
v = 10*math.exp(-((i-xc)**2+(j-yc)**2)/(2*sigma_xy**2))
#math.exp is a direct cython-c call
rec[i, j] += v
# rec is the numpy array where I draw the spot

Do you think glPoints or textures would be a good replacement?
Otherwise to draw it into a numpy array means that I have to iterate
through a large number of pixels, then render the final array in
glumpy.

### Nicolas Rougier

Dec 9, 2011, 10:16:29 AM12/9/11
to glumpy-users

Ok, I understand better your problem.

The best option (for 2d display) would be to draw a simple rectangle
for each gaussian center and texture it with a gaussian texture.
This also depends on the information you want to draw for each
gaussian. If each gaussian is the same, this would work. If you need a
different texture for each gaussian, that might be a bit more tricky.

For 3D display, you can do the same using billboarding (rectangle is
always facing the camera) but you would have to z-sort rectangle
because of transparency.

Another posible option is to use instanced objects and this would
require a recent video card.

Last option is to use shaders.

This depends on the level of performance you need to achieve (but a
million object is quite a lot anyway).

Nicolas

P.S.: Can you subscribe to the group ? (because currently I have to

### Ricardo Henriques

Dec 9, 2011, 10:30:45 AM12/9/11
to glumpy-users
Perfect, thanks for the help.
If you're curious, you can check out what I'm trying to do in the
first video of http://code.google.com/p/quickpalm/ - I'm basically
rewriting the rendering engine for the particles from java to cython
using glumpy as the interface.

I thought I was already registered to the group :/.

### Nicolas Rougier

Dec 9, 2011, 10:36:09 AM12/9/11
to glumpy-users

I did not find the "first video". Could you post the link.
I will then look if I can write the equivalent using glumpy.

Nicolas

On Dec 9, 4:30 pm, Ricardo Henriques <paxca...@gmail.com> wrote:
> Perfect, thanks for the help.
> If you're curious, you can check out what I'm trying to do in the

> first video ofhttp://code.google.com/p/quickpalm/- I'm basically