Skip to content

Instantly share code, notes, and snippets.

@dgobbi
dgobbi / split_nifti.py
Created December 13, 2023 17:36
Split multi-volume nifti into volumes
#! /usr/bin/env python
"""
Split a 4D NIFTI file into separate volumes.
Dec 11, 2023
David Gobbi
"""
usage="""
@dgobbi
dgobbi / ViewImage2D.py
Created May 13, 2021 03:07
View large images with vtkImageResliceMapper
#! /usr/bin/env python
import sys
import os
import mmap
import vtk
usage = """
Usage: "ViewImage2D [options] image
@dgobbi
dgobbi / readdump.py
Last active August 7, 2020 17:29
Parse the output from dicomdump and write out as json
#! /usr/bin/env python
"""
This program reads the output from "dicomdump" and converts it to json.
The latest version of this code can be found at gist.github.com/dgobbi
Note that this code is incomplete, incorrect, and may destroy your data.
It comes with absolutely no warranties. Use at your own risk.
"""
@dgobbi
dgobbi / QVTKRenderWindowInteractor.py
Last active February 15, 2024 07:30
Simple QVTKRenderWindowInteractor example in Python
# coding=utf-8
"""
A simple VTK widget for PyQt or PySide.
See http://www.trolltech.com for Qt documentation,
http://www.riverbankcomputing.co.uk for PyQt, and
http://pyside.github.io for PySide.
This class is based on the vtkGenericRenderWindowInteractor and is
therefore fairly powerful. It should also play nicely with the
vtk3DWidget code.
@dgobbi
dgobbi / QtImageExample.py
Created November 28, 2019 19:23
Simple image display with QVTKRenderWindowInteractor
# coding=utf-8
"""
Example of displaying an image with VTK/Qt/Python.
Original author: David Gobbi
This example does not include proper image orientation or interaction.
"""
import sys
@dgobbi
dgobbi / dicom_to_nifti.py
Last active April 17, 2024 10:43
Program to convert DICOM to NIFTI, includes useful functions for reconstructing DICOM series
"""
Module: dicom_to_nifti.py
Nov 28, 2019
David Gobbi
"""
import pydicom
import nibabel as nib
import numpy as np
@dgobbi
dgobbi / nifti_readwrite.py
Created September 16, 2019 16:30
Example of reading and writing a NIfTI file with nibabel
"""
Program nifti_readwrite.py
Sep 16, 2019
David Gobbi
"""
import sys
import argparse
import nibabel as nib
@dgobbi
dgobbi / vtk_augment_nifti.py
Last active March 31, 2023 10:08
Generate many nifti files corresponding to transformations of one nifti file.
"""
Program vtk_augment_nifti.py
Sep 25, 2018
David Gobbi
dgobbi@ucalgary.ca
"""
import vtk
import sys
import numpy
Kb = 0.114
Kr = 0.299
full = numpy.array([
[1.0, 0.0, 2.0*(1.0-Kr) ],
[1.0, -2.0*Kb*(1.0-Kb)/(1.0-Kr-Kb), -2.0*Kr*(1.0-Kr)/(1.0-Kr-Kb) ],
[1.0, 2.0*(1.0-Kb), 0.0 ]
])
import numpy as np
def ellipsoid(radii, dtype='d'):
"""Generate an ellipsoid with the specified radii.
The ellipsoid will be centered in the output, which will always
have odd dimensions. The surface of the sphere will be antialiased,
that is, a linear ramp is applied at the surface instead of a hard
edge. The size of the ramp is exactly one sample spacing, i.e. points
right on the surface will have a value of 0.5 while points that are