Skip to main content

Linear interpolation and gridding for 2D and 3D images in PyTorch

Project description

torch-image-lerp

License PyPI Python Version CI codecov

Linear 2D/3D image interpolation and gridding in PyTorch.

Why?

This package provides a simple, consistent API for

  • sampling 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 and sampling from complex valued images is supported.

Installation

pip install torch-image-lerp

Usage

Sample from image

import torch
import numpy as np
from torch_image_lerp import sample_image_2d

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)))

# sampling returns a (6, 7, 8) array of samples obtained by linear interpolation
samples = sample_image_2d(image=image, coordinates=coords)

The API is identical for 3D but takes (..., 3) coordinates and a (d, h, w) image.

Insert into image

import torch
import numpy as np
from torch_image_lerp import insert_into_image_2d

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))

# sampling returns a (6, 7, 8) array of samples obtained by linear interpolation
samples = insert_into_image_2d(values, image=image, coordinates=coords)

The API is identical for 3D but takes (..., 3) coordinates and a (d, h, w) image.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_image_lerp-0.0.3.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

torch_image_lerp-0.0.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file torch_image_lerp-0.0.3.tar.gz.

File metadata

  • Download URL: torch_image_lerp-0.0.3.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for torch_image_lerp-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a9df4406d412bee0c82c604742a4d18e8dbfa4ab42f8688549a120a2115dcdfb
MD5 1071f521238dae2f5d7125cb3580526c
BLAKE2b-256 9c3fc347f0fd7afdcb50eb8dace2b59866c7801325ebec3bdfaa17a5164f2e6b

See more details on using hashes here.

File details

Details for the file torch_image_lerp-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_image_lerp-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ef729f68045e31de5c5fce579529607f6598144d06dfcb9bbd2a4c9b57e39fdc
MD5 f13adf86935a75bbea4d5f2d3102b265
BLAKE2b-256 539af727af79e13edf2ed80a434db72b4cb91b9afb63a9d59eb70e3e9c72849d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page