[SciPy-User] Antwort: Re: scipy.interpolate.interp2d too many data values..

Zachary Pincus zachary.pincus at yale.edu
Fri Sep 4 07:06:23 EDT 2009


> >For interpolation of images that are specified on a regular grid,  
> look at
> >scipy.ndimage, especially map_coordinates.
>
> Yes, that's what I was looking for. How do you actaully use it for a  
> RGB image?
> I used the example from http://www.scipy.org/Cookbook/Interpolation  
> but I don't understand how it works for 3-D coordinates.
> I want to apply the same remapping stored in y_new, x_new arrays  
> with 2-D shape for the 3 channels. Until now I just understand like
>
> >>> coords = array([y_new, x_new])
> >>> r = map_coordinates(img_org[:,:,0], coords )
> >>> g = map_coordinates(img_org[:,:,1], coords )
> >>> b = map_coordinates(img_org[:,:,2], coords )
> >>> img = dstack((r,g,b))
>
> But it seems that it can be done shorter...

Assuming the image is large-ish, the overhead from the extra function  
calls is probably low enough that this is not much slower than an all- 
in-one approach. You could do better, memory-wise, by pre-allocating  
the output array and passing views on it (e.g. the appropriate slices)  
to map_coordinates as output arrays, but this would only really matter  
if the images are huge or you are doing this in a tight loop.

Finally, you could also devise a 3D coordinate transform that is just  
the 2D transform you want, plus an identity transform in the third  
dimension (e.g. color channel) so you don't mix the colors. Basically,  
you just want your coords array, but with an additional dimension that  
contains 0s, 1s, and 2s to map red to red, green to green, and blue to  
blue, etc. If this is sufficiently unclear, I can try to gin up an  
example. Like I said, though, I'm not sure this will be much faster,  
and the code might not be any more clear.

Zach




More information about the SciPy-User mailing list