In the following we explain how to use the Grasshopper components related to IO and Image Operations, as well as the Image Filter component Modify Origin Spacing Flipping.
IO: Read File and Write Image
Component Read File has an input file path and an output Rhino3DMedical image. The file path points to an image (available formats DICOM, MHD/MHA, TIFF, NII, NRRD). In the case of DICOM, the component will recognize all other DICOM files in the same directory (though no distinction between images is possible, so all DICOM files should belong to the same image). It is possible to set the file path by right-clicking over File Path and then choosing Select one existing file.
Component Write Image has as input a Rhino3DMedical image, a path for the file to be written and optionally a path to a DICOM file in case we want to take DICOM header information into the output file (only when the output is also DICOM).
- If we want to write the image as non-DICOM: we must define the input path as a file specifying its format (if no format is specified, the component internally adds a .mhd extension). We can set this by right-clicking over File Path and choose Select new file location, specifying the name of the file and a format among .mhd/.mha/.tiff/.nii/.nrrd, for example myImage.mhd.
- If we want to write the image as DICOM: we must define the input path as a directory. The DICOM files will be placed there. We can set a directory with right-click over File Path and select Select a directory. If in addition the component input Initial DICOM Path is defined (not as a directory but as a DICOM file path), the output DICOM files will copy its header. Otherwise a blank new header will be created.
Note that we can write to file any Rhino3DMedical image or mask (as masks are a particular kind of images) at any step.
Read File and Write Image components
Image Operations components deal with transformations of images mostly through masks. Masks are images themselves whose voxel intensities are either 0 or 1. They can be used internally to multiply images and extract subregions.
Mask from Threshold and Masked Image
The easiest way to create a mask is from a threshold. Component Mask from Threshold lets us plug in an image and a threshold (at least one for a range limited by the image upper bound intensity, and two for a defined range). The output is a mask that has voxel intensity 1 over the threshold range and 0 elsewhere.
Component Masked Image plugs in an image and a mask, and extracts a new image that keeps the voxel intensities of the input image where the mask is 1, and has uniform background values elsewhere.
Check the following two pictures for a demonstration of these two components. They have the same setting but in the first picture the Masked Image component has been unplugged and the Extract Slice component rewired so that we see the full input image. In the second picture we connect to the output the masked image for a given threshold: the output image keeps all grey values in the voxels that were defined by the threshold that we set.
Result before and after applying Masked Image component
Lastly, component Masked Image from Threshold is internally the exact superposition of Mask from Threshold and Masked Image: it takes an input image and a threshold and gives a new image that is masked by the threshold.
Mask from Mesh
Component Mask from Mesh is similar to Mask from Threshold, but it takes as masking primitive a closed mesh instead of a threshold. The mesh must be provided as input. Take a look at the following video to see the applications of it:
A second way of creating a mask is to extract a white mask from an image, e.g. a mask with the same dimensions that has value 1 or 0 everywhere. This can be a reference to be later added, subtracted or intersected. Component White Mask does such a thing, by plugging an image and optionally an integer value that is 0 or 1 (by default it will be 1).
Component Invert Mask receives an input mask and outputs an inverted mask (1 goes to 0 and 0 goes to 1).
These masks can be put together in component Mask Operation, which takes inputs Mask 1, Mask 2 and an Operation integer (0 is for addition, 1 for subtraction and 2 for intersection). If addition, output Mask will be addition of masks 1 and 2; if subtraction, mask 2 will be subtracted from mask 1; if intersection, mask 1 and 2 will be intersected and given as output Mask.
Important: it is crucial that mask 1 and 2 originally were obtained from the same image (e.g. one coming from thresholding such image, the other one from placing a bounding box or a white image). The ultimate compatibility condition is that the two masks have the same number of voxels. If this is not true, the component will not be able to compute an output.
We can combine these components together to extract a masked image that keeps the original intensity values outside of the threshold range, as it can be seen in the following picture. Note that although the voxel values look different than the ones in previous pictures (due to an internal render mesh normalization carried out in the Extract Slice component for visualization purposes), internally they are consistent.
Using Mask Operation and White Mask
Reading Masks from file
As a mask is also an image, we can then extract masks by reading from a file. For example, we can open the medical image in Rhino3DMedical (non-Grasshopper), extract Mask from Threshold with the lower threshold to the minimum and upper threshold to the maximum, then place a bounding box (Segmentation tab Manual Segmentation section) and press Export Mask to extract the bounding box mask (as TIFF). We can also obtain such a mask by, when we open the image, directly clicking Invert in Segmentation tab Manual Segmentation section.
Mask from a Bounding Box
We then apply the following scheme in Grasshopper: on one side we extract a mask from threshold, on the other side we read the bounding box mask from file, and we intersect them with Mask Operation, then extract the masked image. We obtain the thresholded are lying only within the bounding box.
Using Mask Operation with a manual mask read from file
Image Filter Modify Origin Spacing Flipping
This component is in the Image Filters category because it has an image plus geometrical information as input and an image as output. We though present this filter in this post as it can be thought as a means of image operation, because it places/rescales/orients the input without modifying its grey-scale values.
Input Origin Point lets us choose a Rhino point that will represent the new origin of the image. Spacing X, Y, Z modify the spacing of the unit voxel. Flipping X, Y, Z flip the image orientation axis in X, Y, Z. All inputs except the image have a default value, so you can input only the origin point for example. This is very useful if your input image lies far away from the Rhino origin.
Example of using Modify Origin Spacing Flipping component to change the origin of an image
Live session video
Watch here the live session explaining this post (except component Mask from Mesh, which is shown in its corresponding section) with a broader overview of Rhino3DMedical Grasshopper: