Linear interpolation and gridding for 2D and 3D images in PyTorch
Project description
torch-image-interpolation
2D/3D image interpolation routines in PyTorch.
Why?
This package provides a simple, consistent API for
- sampling values from 2D/3D images (
sample_image_2d()/sample_image_3d()) - inserting values into 2D/3D images (
insert_into_image_2d()/insert_into_image_3d)
Operations are differentiable, multichannel data and complex valued images are supported.
torch.nn.functional.grid_sample
is used under the hood for sampling.
Installation
pip install torch-image-interpolation
Usage
Coordinate System
This library uses an array-like coordinate system where coordinate values span from 0
to dim_size - 1 for each dimension.
Fractional coordinates are supported and values are interpolated appropriately.
2D Images
For 2D images with shape (h, w) or (c, h, w):
Coordinates are ordered as [y, x] where:
yis the position in the height dimension (first dimension of shape)xis the position in the width dimension (second dimension of shape)
For example, in a (28, 28) image, valid coordinates range from [0, 0] to [27, 27]
3D Images
For 3D images with shape (d, h, w) or (c, d, h, w):
Coordinates are ordered as [z, y, x] where:
zis the position in the depth dimension (first dimension of shape)yis the position in the height dimension (second dimension of shape)xis the position in the width dimension (third dimension of shape)
For example, in a (28, 28, 28) volume, valid coordinates range from [0, 0, 0] to
[27, 27, 27].
Sample from image
import torch
import numpy as np
from torch_image_interpolation import sample_image_2d
# example (h, w) image
image = torch.rand((28, 28))
# make an arbitrary stack (..., 2) of 2d coords
coords = torch.tensor(np.random.uniform(low=0, high=27, size=(6, 7, 8, 2))).float()
# sampling returns a (6, 7, 8) array of samples
# using bilinear interpolation (the default)
sample_bilinear = sample_image_2d(image=image, coordinates=coords, interpolation='bilinear')
# or a different interpolation mode...
samples_nearest = sample_image_2d(image=image, coordinates=coords, interpolation='nearest')
samples_bicubic = sample_image_2d(image=image, coordinates=coords, interpolation='bicubic')
The API is identical for 3D (d, h, w) images but takes (..., 3) arrays of
coordinates.
Sampling is supported for multichannel images in both 2D (c, h, w) and 3D (c, d, h, w).
Sampling multichannel images returns (..., c) arrays of values.
Insert into image
import torch
import numpy as np
from torch_image_interpolation import insert_into_image_2d
# example (h, w) image
image = torch.zeros((28, 28))
# make an arbitrary stack (..., 2) of 2d coords
coords = torch.tensor(np.random.uniform(low=0, high=27, size=(3, 4, 2)))
# generate random values to place at coords
values = torch.rand(size=(3, 4))
# insert values into the image with bilinear interpolation (the default)
image_bilinear, weights_bilinear = insert_into_image_2d(
values, image=image, coordinates=coords, interpolation='bilinear'
)
# you can specify a different interpolation mode
image_nearest, weights_nearest = insert_into_image_2d(
values, image=image, coordinates=coords, interpolation='nearest'
)
The API is identical for 3D (d, h, w) images but requires (..., 3) arrays of
coordinates.
Insertion of is supported for multichannel images in both 2D (c, h, w) and 3D (c, d, h, w).
Inserting into multichannel images requires (..., c) arrays of values.
Similar packages
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_image_interpolation-0.0.7.tar.gz.
File metadata
- Download URL: torch_image_interpolation-0.0.7.tar.gz
- Upload date:
- Size: 53.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1878b6c51c4b5ffa9474386c5d1d059a837b4a6e8c28db4eaa8656d611c1730
|
|
| MD5 |
25df7efb108f2013ea7045f581fb3d11
|
|
| BLAKE2b-256 |
734aafa9a578da47d9d9a196e80cd3ccc0b8e02845da0e450fc4bf80762530b8
|
Provenance
The following attestation bundles were made for torch_image_interpolation-0.0.7.tar.gz:
Publisher:
ci.yml on teamtomo/torch-image-interpolation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_image_interpolation-0.0.7.tar.gz -
Subject digest:
f1878b6c51c4b5ffa9474386c5d1d059a837b4a6e8c28db4eaa8656d611c1730 - Sigstore transparency entry: 204773372
- Sigstore integration time:
-
Permalink:
teamtomo/torch-image-interpolation@eb6c38816106c85d7c9ef60e6ade06715dc944cf -
Branch / Tag:
refs/tags/v0.0.7 - Owner: https://github.com/teamtomo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@eb6c38816106c85d7c9ef60e6ade06715dc944cf -
Trigger Event:
push
-
Statement type:
File details
Details for the file torch_image_interpolation-0.0.7-py3-none-any.whl.
File metadata
- Download URL: torch_image_interpolation-0.0.7-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b3ed017babe2c8b58d6940173e39c1c3b878b85549fffd4b38cab1814d6c91f
|
|
| MD5 |
90b2a67a36be2b02a8446570dbe29941
|
|
| BLAKE2b-256 |
84220ffebd9209f7ad8b9aeca0510750e32e42261751a8b924b74abaeac8702f
|
Provenance
The following attestation bundles were made for torch_image_interpolation-0.0.7-py3-none-any.whl:
Publisher:
ci.yml on teamtomo/torch-image-interpolation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_image_interpolation-0.0.7-py3-none-any.whl -
Subject digest:
7b3ed017babe2c8b58d6940173e39c1c3b878b85549fffd4b38cab1814d6c91f - Sigstore transparency entry: 204773376
- Sigstore integration time:
-
Permalink:
teamtomo/torch-image-interpolation@eb6c38816106c85d7c9ef60e6ade06715dc944cf -
Branch / Tag:
refs/tags/v0.0.7 - Owner: https://github.com/teamtomo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@eb6c38816106c85d7c9ef60e6ade06715dc944cf -
Trigger Event:
push
-
Statement type: