Comments (5)
I was about to create an issue asking for support for masking any pixels outside the boundary as np.nan
. Is there any nice way to do that with interpolation.py
or shall I write my own script for this?
from interpolation.py.
Currently, there is no support for that.
Closest you can do is eval_linear( ..., extrap_mode='constant')
. It will put zeros outside of the grid.
This is an easy feature, though. And it should be easy to add it. The question is how. I see two easy options.
eval_linear(..., extrap_mode='constant(nan)')
oreval_linear(..., extrap_mode='constant(0)')
: very easy to implementeval_linear(..., extrap_mode='constant', constant_value=0)
where 0 would need to be treated as a literal. Not clear what would be the literal corresponding to nan.
from interpolation.py.
It was easier than expected - I ended up doing it like this:
mask = np.any((points >= image_shape) | (points < 0.), axis=1)
interpolated_points[mask] = np.nan
So the full function for warping an image became:
def interpolate_image_to_new_position(img: "(Y, X)", points: "(N, 2) or (Y, X, 2)", fill_value=np.nan):
"""Warp an image to new positions given by a list of coordinates that has the same length
as the image has pixels
Parameters
----------
img: Image of shape (Y, X)
points: Array of points of shape (N, 2), where the last two indices are in traditional (X,Y) convention
fill_value: Value of points interpolated outside the grid of the image.
False or float or np.nan
False follows interpolate.py behaviour.
"""
# Grid probably becomes a linspace'd array:
grid = ((0, img.shape[0]-1, img.shape[0]), (0, img.shape[1]-1, img.shape[1]))
points = points.reshape((-1, 2))
points = points[:,::-1] # Swap coordinates to (Y, X) convention
interpolated_values = eval_linear(grid, img, points)
if fill_value is not False and fill_value is not None:
mask = np.any((points >= img.shape) | (points < 0.), axis=1)
interpolated_values[mask] = fill_value
return interpolated_values.reshape(img.shape)
from interpolation.py.
Yes, that would work. It's a workaround with a bit of memory allocation that wouldn't happen if the functionality is included in interpolation.py. So let's keep this issue open.
from interpolation.py.
I think I suffered some sort of brain-fart during turning my code into the previous comment. While the idea was right, the code I pasted didn't run. I've updated it.
from interpolation.py.
Related Issues (20)
- Is the dependency on tempita necessary? HOT 7
- `eval_splines` gives error `TypeError: 'module' object is not callable` HOT 2
- "interp" not working with python 3.11 HOT 2
- Errors with multidimensional outputs HOT 4
- eval_spline() doesn't recognize argument `k=` HOT 5
- literal type error when eval_spline() called without some keyword arguments HOT 4
- New version HOT 5
- Numba warnings HOT 8
- eval_cubic fails when array to interpolate on is non-writable
- After 2.2.0 eval_linear needs C-order and dtype FLOAT64 HOT 3
- guvectorized wrapping of eval_linear() appears broken HOT 2
- DOC: reorganize
- 1D interpolation with eval_linear HOT 5
- Documentation link broken HOT 1
- Import jitclass error HOT 1
- interpolating wind data on the satellilte track (in same lon lat time)
- Documentation for derivatives HOT 9
- Error when running example code in the documentation HOT 3
- `curses` requirement? HOT 6
- Python 3.10 support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interpolation.py.