Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"painting" one array onto another using python / numpy

I'm writing a library to process gaze tracking in Python, and I'm rather new to the whole numpy / scipy world. Essentially, I'm looking to take an array of (x,y) values in time and "paint" some shape onto a canvas at those coordinates. For example, the shape might be a blurred circle.

The operation I have in mind is more or less identical to using the paintbrush tool in Photoshop.

I've got an interative algorithm that trims my "paintbrush" to be within the bounds of my image and adds each point to an accumulator image, but it's slow(!), and it seems like there's probably a fundamentally easier way to do this.

Any pointers as to where to start looking?

like image 617
Nate Avatar asked Dec 22 '09 21:12

Nate


1 Answers

In your question you describe a Gaussian filter, for which scipy has support via a package. For example:

from scipy import * # rand
from pylab import * # figure, imshow
from scipy.ndimage import gaussian_filter

# random "image"
I = rand(100, 100)
figure(1)
imshow(I)

# gaussian filter
J = gaussian_filter(I, sigma=10)
figure(2)
imshow(J)

Of course, you can apply this on the whole image, or just on a patch, using slicing:

J = array(I) # copy image
J[30:70, 30:70] = gaussian_filter(I[30:70, 30:70], sigma=1) # apply filter to subregion
figure(2)
imshow(2)

For basic image manipulation, the Python Image library (PIL) is probably what you want.

NOTE: for "painting" with a "brush", I think you could just create a boolean mask array with your brush. For instance:

# 7x7 boolean mask with the "brush" (example: a _crude_ circle)
mask = array([[0, 0, 1, 1, 1, 0, 0],
              [0, 1, 1, 1, 1, 1, 0],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [1, 1, 1, 1, 1, 1, 1],
              [0, 1, 1, 1, 1, 1, 0],
              [0, 0, 1, 1, 1, 0, 0]], dtype=bool)

# random image
I = rand(100, 100)
# apply filter only on mask
# compute the gauss. filter only on the 7x7 subregion, not the whole image
I[40:47, 40:47][mask] = gaussian_filter(I[40:47, 40:47][mask], sigma=1)
like image 136
catchmeifyoutry Avatar answered Oct 03 '22 15:10

catchmeifyoutry