Lightweight image registration library using PyTorch
Project description
torchreg
torchreg is a tiny (~300 lines) PyTorch-based library for 2D and 3D image registration.
Usage
Affine Registration of two image tensors is done via:
from torchreg import AffineRegistration
# Load images as torch Tensors
big_alice = ... # Tensor with shape [1, 3 (color channel), 1024 (pixel), 1024 (pixel)]
small_alice = ... # Tensor with shape [1, 3 (color channel), 1024 (pixel), 1024 (pixel)]
# Intialize AffineRegistration
reg = AffineRegistration(is_3d=False)
# Run it!
moved_alice = reg(big_alice, small_alice)
Features
Multiresolution approach to save compute (per default 1/4 + 1/2 of original resolution for 500 + 100 iterations)
reg = AffineRegistration(scales=(4, 2), iterations=(500, 100))
Choosing which operations (translation, rotation, zoom, shear) to optimize
reg = AffineRegistration(with_zoom=False, with_shear=False)
Custom initial parameters
reg = AffineRegistration(zoom=torch.Tensor([[1.5, 2.]]))
Custom dissimilarity functions and optimizers
def dice_loss(x1, x2):
dim = [2, 3, 4] if len(x2.shape) == 5 else [2, 3]
inter = torch.sum(x1 * x2, dim=dim)
union = torch.sum(x1 + x2, dim=dim)
return 1 - (2. * inter / union).mean()
reg = AffineRegistration(dissimilarity_function=dice_loss, optimizer=torch.optim.Adam)
CUDA support (NVIDIA GPU)
moved_alice = reg(moving=big_alice.cuda(), static=small_alice.cuda())
After the registration is run, you can apply it to new images (coregistration)
another_moved_alice = reg.transform(another_alice, shape=(256, 256))
with desired output shape.
You can access the affine
affine = reg.get_affine()
and the four parameters (translation, rotation, zoom, shear)
translation = reg.parameters[0]
rotation = reg.parameters[1]
zoom = reg.parameters[2]
shear = reg.parameters[3]
Installation
pip install torchreg
Examples/Tutorials
There are three example notebooks:
- examples/basics.ipynb shows the basics by using small cubes/squares as image data
- examples/images.ipynb shows how to register alice_big.jpg to alice_small.jpg
- examples/mri.ipynb shows how to register MR images (Nifti files) including co-, parallel and multimodal registration
Background
If you want to know how the core of this package works, read the blog post!
TODO
- Add 2D support to SyN, NCC and LinearElasticity
- Add tests for SyN
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
Built Distribution
File details
Details for the file torchreg-0.1.2.tar.gz
.
File metadata
- Download URL: torchreg-0.1.2.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.9.12 Linux/5.15.0-105-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b472888a25193a7730819bcf453636ca62c68fe4ee3c23430f6ee428c7a19db |
|
MD5 | 3ce5e9a8fd1dc9984aa2949e2de88eba |
|
BLAKE2b-256 | 5432a8d8a6ee2f465ec926fb495d50c4c1cf33ed6e1989d692d019db81755e1c |
File details
Details for the file torchreg-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: torchreg-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.9.12 Linux/5.15.0-105-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e7ec756a0928df3db48bf01035bb0713b69e7071858632960a880a04518f613 |
|
MD5 | c865bcf03bafd03180bd5f8b34685ad7 |
|
BLAKE2b-256 | 29f13da2a8203dc7e3201747c817037d5cd792cc55344f6debfcfbb10a8c5868 |