Masking Images

Masking involves setting some of the pixel values in an image to zero, or some other "background" value. Masking can be done in one of two ways:

  • Using an image as a mask. A mask image is simply an image where some of the pixel intensity values are zero, and others are non-zero. Wherever the pixel intensity value is zero in the mask image, then the pixel intensity of the resulting masked image will be set to the background value (normally zero). You might, for example, create a mask image using the Particle Analysis tool.
  • Using a set of ROIs as the mask. The ROIs for each slice are used to define the mask.

Note: you can also perform masking operations on individual slices or whole images in a similar way from the ROI Toolkit. There are three main differences:

  • In the ROI toolkit, masking can be controlled on a slice-by-slice basis if required.
  • The way slices without ROIs defined on them is handled differently:
  • In the Masker Tool, a slice without ROIs is treated as being completely outside the mask.
  • In the ROI Toolkit, a slice without ROIs is not affected by masking operations.
  • Use of the Masker tool allows operation from the stand-alone tools (and therefore masking can be run from scripts and batch files).

If you use ROIs as a mask, you may also perform soft masking, to set the intensity of masked pixels not to the background value, but a value that depends on how much of an individual pixel is inside the mask.

Normally, pixels that are inside the mask do not have their pixel values changed: they are left at the same intensity as the input image. However, you can also create images with binary intensity values (either 0 outside the mask, or 1 inside the mask) by selecting Select to create a binary masked output image.

Selecting Masker from the Process menu Menu item to bring up the Image Masker tool of the main display frame brings up the Image Masking Tool:

The Image Masker tool GUI

The Image Masking tool takes an input image, masks it, and produces a new image which is a copy of the input image, except that the new image will have its pixel intensity value set to zero (or some other chosen background intensity value) according to the mask and the masking operations performed. The new (masked) image can either be saved to disk, or loaded into Jim depending on your selection:

Load the result into the display or save thre result to disk

Load the input image and the mask image by clicking on the Icon to open an image file icon, by typing in the folder (directory) and file name of the image, or by pressing the right mouse button and selecting from the menu of recently-used images.

Selecting the input image

Next, set the background pixel value (which defaults to zero). Pixel intensities in the masked image that are affected by masking operations will have their intensity set to the background pixel intensity value.

Choose whether you want to set the pixels intensities outside the mask to the background, or those inside the mask to be the background, by clicking on the appropriate button:

Selecting the mask action

If you are masking using ROIs, choose whether you want hard or soft masking. Note: the selection of hard or soft masking has no impact when using an image mask.

  • Hard masking. Pixels affected by masking have their intensity set to the background value.
  • Soft masking. For pixels affected by masking, the resulting intensity change is depends on how much of the pixel is inside the ROI. For masking outside, if only a small portion of the pixel is inside an ROI, the resulting will be close to the background value; if a large portion is inside the ROI, its intensity will be largely unaffected. Below you can see the result masking of an image using an Irregular ROI, with both hard and soft masking. Hard and soft masking with an ROI. Note the soft edge created by soft masking.
Original, unmasked image After hard masking After soft masking
Before masking. Hard masking. Soft masking.

Note: hard and soft masking only applies when you mask using ROIs. If you mask using an image, soft masking behaves the same as hard masking.

If you want the output image to be a "binary" image (i.e., an image where the pixel intensities are exclusively either zero or one) then select Making the output image binary.

Then, choose whether you will use an image as the mask, or a set of ROIs by clicking the appropriate button:

Mask with an image, or with ROIs

  • If using an image as the mask, set the mask image in the mask image selection panel:

    The the image with which to mask

  • If using a set of ROIs as the mask, first choose the ROI file that contains the ROIs you wish to use:

    Mask using ROIs

Then choose the way in which you wish ROIs in each slice to be combined:

Select how overlapping ROIs are combined

The following illustrates the effect of the different ROI combination modes:

The effect of the different ROI combination modes for a slice with two overlapping ROIs.
Note: Mask action was set to "Mask Outside".
Before masking with two overlapping ROIs After masking in XOR mode After masking in Union mode After masking in Intersection mode
Before
masking
XOR Union Intersection

The "XOR" mode is useful where you wish to define regions of the image that have a "hole" in them. By having one ROI inside another, the internal ROI then defines the hole in the image when ROIs are combined using the XOR operation. Note, however, that you can also create hollow ROIs to obtain a similar result.

  • Finally (when masking using ROIs) set the pixel area threshold for inclusion in the mask using the pixel area threshold panel:

    ROI pixel area threshold for inclusion in the mask

    Select:

    • > 0: if any part of a pixel is inside an ROI, that pixel will be defined to be within the mask.
    • > 50%: if 50% or more of a pixel is inside an ROI, that pixel will be defined to be within the mask.
    • 100%: the whole of a pixel must be inside an ROI for that pixel to be within the mask.

Now click on the Do the masking button to perform the masking. If you have chosen to save the result to disk, then a File Chooser will pop up, prompting you to choose a file name for the new masked image. Otherwise, the result will be loaded into Jim.

Note: if the input image to be masked is a 4-D image (multiple 3-D volumes) or an image with even higher dimensionality, then you can mask all the 3-D volumes within the input image using:

  • An ROI file where all ROIs are defined on slices within one 3-D volume, or
  • A 3-D mask image where the number of slices in the mask image is equal to the number of slices in each 3-D volume of the input image.