Skip to main content

No project description provided

Project description

Nitrain-image

Coverage Status Build

Welcome to the numpy-like library for medical images. The nitrain-image library lets you read, write, visualize, and operate on medical imaging data in a natural, pythonic way. All of the most common image formats - dicom, nifti, etc. - are supported.

If you are interested in training medical imaging AI models, then you may want to check out the nitrain library. You can also learn more about using nitrain-image and nitrain in the open-source book "Becoming a medical imaging AI expert with Python".


Quickstart

Here is a 10 minute introduction to the nitrain-image library. If you have used numpy before, that is about all you will need to start working with medical images like a pro.

To begin, the easiest way to install the nitrain-image package is from PyPi using pip.

Installation

pip install nitrain-image

Loading and saving

You can load basically any type of medical image using nitrain-image.

import ntimage as nti
img = nti.load('image.nii.gz')

Saving is equally easy.

nti.save(img, 'image.nii.gz')

Creation

If you have a numpy array without any medical imaging metadata, you can create an ntimage.

import numpy as np
img = nti.from_numpy(np.random.randn((128,128)))

Converting any ntimage - even those read from file - to a numpy array can also be done.

arr = img.numpy()

If you have an ntimage with associated metadata such as origin, spacing, direction, and so on, then it is possible to transfer that info when creating an ntimage from a numpy array.

img.set_origin((10,10))
img2 = nti.from_numpy_like(np.random.randn((128,128)), img)

As with numpy or torch, you can also create an ntimage in a variety of convenient ways:

img = nti.ones((128,128))
img = nti.zeros((128,128))
img = nti.rand((128,128))

The ntimage class has the same datatypes as numpy arrays. If you want to cast an ntimage to another datatype, you can do so with the clone function.

img = img.clone('float32')

Indexing

You can index an ntimage as you would a numpy array, but keep in mind that indexing an ntimage returns another ntimage and NOT a numpy array. This makes it very convenient to crop images.

img = nti.ones((128,128))
img[:20,:20]

Assignment of values is also possible via indexing.

img = nti.ones((128,128))
img[:20,:20] = 0

Indexing an ntimage with another ntimage is also supported. In this case, the image you use to index will be treated like a mask and the resulting image will have any values outside of the index set to zero.

img = nti.rand((128,128))
mask = nti.zeros((128,128))
mask[:20,:20] = 1
img2 = img[mask] # all indices outside of (:20, :20) will now be zero

Math operations

As with numpy, you can use any core math operation on ntimages.

img = nti.ones((128,128))
img2 = nti.ones((128,128))

img = img * 10
img = img + img2
img = img / 10

There are also many other math functions such as exp and log that can be applied to an ntimage. Additionally, all summary functions such as median, mean, sum, min, max, and so on, are available.

img = nti.exp(img)
img = nti.log(img)

Logical functions will also work as expected on ntimages.

img = nti.ones((128,128))
img2 = nti.rand((128,128)) > 0.5

img3 = img & img2

Image operations

You can use many traditional image procesing operations on ntimages. The smooth function is one example.

img = nti.load(nti.example_data('r16'))
img2 = nti.smooth(img, sigma=2)

The resample function also has many important use cases for ntimages.

img2 = nti.resample(img, (64,64))

Because nitrain-image is built on top of ITK, you can build you own custom image processing function using the itk package in Python and integrate it using the from_itk function.

img = nti.load(nti.example_data('r16'))

def my_function(image):
    itk_image = image._image
    # perform some itk processing...
    # ...
    return from_itk(itk_image)

The interoperability of nitrain-image with numpy and itk make it easy to do pretty much anything.


Plotting

The visualization functions of nitrain-image are highly intuitive with sensible defaults, but they are still flexible. The plot function will take care of most needs.

img = nti.load(nti.example_data('mni'))
nti.plot(img)

The plot function also supports overlays. This is useful for showing labels on top of your images.

img2 = img > img.median()
nti.plot(img, img2)

Other functions such as grid and plot_ortho provide you with more flexibility to create unique, publication-quality visualizations from your images.

Lastly, the plot_hist function can come in handy to view the distribution of an image's intensity values.

img = nti.load(nti.example_data('r16'))
nti.plot_hist(img)

Contributing

If you have a bug to report or are interested in contributing new features to nitrain-image, feel free to create an issue here on GitHub. The nitrain-image library builds upon the excellent Insight Toolkit (ITK).

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

nitrain_image-0.1.1.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

nitrain_image-0.1.1-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file nitrain_image-0.1.1.tar.gz.

File metadata

  • Download URL: nitrain_image-0.1.1.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for nitrain_image-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b3f04deae81e021a2a6fc9369c435582be60ef54cd6751bf42a8a3184652027f
MD5 95e2804a977d4a5b0b6ba1c42a60b342
BLAKE2b-256 90f557662bccb7ef85f6c34848f5555c4d65f3dd2ab65d596f429e1aa43afc60

See more details on using hashes here.

File details

Details for the file nitrain_image-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nitrain_image-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d84d89c96264b9b989f2a7535cb7a6a74322051a84d208ea1646b47635a0fe54
MD5 30899db7832ca62998012d0248c9503f
BLAKE2b-256 51e7a61e853d00d0ecf9b26bc10f6b95a96bbc2c7f44ee64323585991b27aca8

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