Skip to main content

Real space transformations of 2D/3D images in PyTorch

Project description

torch-transform-image

License PyPI Python Version CI codecov

Real space transformations of 2D/3D images in PyTorch

Motivation

This package provides a simple, consistent API for applying affine transformations to 2D/3D images in PyTorch. It enables efficient, GPU-accelerated geometric transformations of images.

Installation

pip install torch-transform-image

Features

  • Apply arbitrary affine transformations to 2D and 3D images
  • Support for various interpolation methods (nearest, bilinear, bicubic for 2D; nearest, trilinear for 3D)
  • Batched operations for efficient processing
  • Fully differentiable operations compatible with PyTorch's autograd

Coordinate System

This package uses the same coordinate system as NumPy/PyTorch array indexing:

  • For 2D images: coordinates are ordered as [y, x] for dimensions (height, width)
  • For 3D images: coordinates are ordered as [z, y, x] for dimensions (depth, height, width)

Transformation matrices left-multiply homogeneous pixel coordinates ([y, x, 1] for 2D and [z, y, x, 1] for 3D).

Generating Transformation Matrices

The companion package torch-affine-utils provides convenient functions to generate transformation matrices that work with homogenous pixel coordinates (yxw/zyxw):

from torch_affine_utils.transforms_2d import R, T, S  # Rotation, Translation, Scale for 2D
from torch_affine_utils.transforms_3d import Rx, Ry, Rz, T, S  # Rotation, Translation, Scale for 3D

Usage

2D Transformations

import torch
from torch_transform_image import affine_transform_image_2d
from torch_affine_utils.transforms_2d import R, T, S  # Rotation, Translation, Scale

# Create a test image (28×28)
image = torch.zeros((28, 28), dtype=torch.float32)
image[14, 14] = 1  # Place a dot at the center

# Create a transformation matrix to translate coordinates 4 pixels in y direction
translation = T([4, 0])  # Uses [y, x] coordinate order matching dimensions (h, w)

# Apply the transformation
result = affine_transform_image_2d(
    image=image,
    matrices=translation, 
    interpolation='bilinear',  # Options: 'nearest', 'bilinear', 'bicubic'
    yx_matrices=True,  # The generated translations have [y, x] order
)

# Compose multiple transformations
# First translate to origin, then rotate, then translate back
T1 = T([-14, -14])  # Move center to origin
R1 = R(45, yx=True)  # Rotate 45 degrees
T2 = T([14, 14])  # Move back
transform = T2 @ R1 @ T1  # Matrix composition (applied right-to-left)

# Apply the composed transformation
rotated = affine_transform_image_2d(
    image=image,
    matrices=transform,
    interpolation='bicubic',
    yx_matrices=True,
)

3D Transformations

import torch
from torch_transform_image import affine_transform_image_3d
from torch_affine_utils.transforms_3d import R, T, S  # Rotation, Translation, Scale

# Create a test volume (64×64×64)
volume = torch.zeros((64, 64, 64), dtype=torch.float32)
volume[32, 32, 32] = 1  # Place a dot at the center

# Create a transformation matrix (translate coordinates 5 voxels in z direction)
translation = T([5, 0, 0])  # Uses [z, y, x] coordinate order matching dimensions (d, h, w)

# Apply the transformation
result = affine_transform_image_3d(
    image=volume,
    matrices=translation, 
    interpolation='trilinear',  # Options: 'nearest', 'trilinear'
    zyx_matrices=True,  # The generated translations have [z, y, x] order
)

How It Works

Under the hood, the package:

  1. Creates a coordinate grid for the input image
  2. Applies the transformation matrix to these coordinates
  3. Samples the original image at the transformed coordinates using the specified interpolation method

All operations are performed in PyTorch, making them fully differentiable and GPU-compatible.

License

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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_transform_image-0.0.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_transform_image-0.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file torch_transform_image-0.0.2.tar.gz.

File metadata

  • Download URL: torch_transform_image-0.0.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for torch_transform_image-0.0.2.tar.gz
Algorithm Hash digest
SHA256 206548d1881c09e2271b9bb4c081a760696280a2d0bb831e31b51896927746e3
MD5 b5be4fd08cec282d97bcb8fdc77870b0
BLAKE2b-256 3436ac269583d948a5a8c3270622a34c1b9faf7d72712c957e22441d86a5a0f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_transform_image-0.0.2.tar.gz:

Publisher: ci.yml on teamtomo/torch-transform-image

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torch_transform_image-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_transform_image-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 63d466b0facc9b6869c6c0613ef94e3e589e8e560fd11a271074872b882eb351
MD5 5a9adf32cd0e8d8d7561dc13e84bd781
BLAKE2b-256 8dd5a8b93c5f5dbbf04d92be812e996d52b0458b2b896b13ee35a510d82d247e

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_transform_image-0.0.2-py3-none-any.whl:

Publisher: ci.yml on teamtomo/torch-transform-image

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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