Mask borders of a FITS image

Often times it is useful to exclude the [noisy] pixels at the edges of a FITS image.
With the word “edge” or “border”, I here refer to those pixels separating the area of the image with valid data pixels (value > 0), and that which simply fill the rest the canvas (value = 0, sometimes “-nan”, which I will call “NULL pixels”).

For example, SExtractor can be confused by the edges and return unphysically large sources there.
Look how much of a difference it makes when running SExtractor with the exact same setup, without masking (left), and masking (right) the image borders (NOTE: the masked border is  represented by an horizontal dash in the left image):


Figure 1 – Left: SExtractor apertures obtained without masking the image borders. Right: SExtractor apertures obtained with the exact setup, but this time masking the image borders. Notice how the residual background (the background calculated by SExtractor is subtracted in these images) changes significantly also far away from the image borders.

PERL script to automatically mask borders

To mask the image edges I wrote a simple PERL script which automatically detects the image borders (as long as the NULL pixels have value = 0).
The border is “shrunk” (to exclude the [noisy] data pixels at the edge) and then masked.

▸ The code is available here for DOWNLOAD*:
The script is computationally intensive, so be patient! If you are not, use the “-verbose” keyword to check the progress.

*NOTE: it requires ds9, IRAF and some PERL packages installed (instructions within).


▸ Masking the borders of the image above


Figure 2 – Running PERL script ( image.fits -scale -15 -sample 1). Left: Original image; the “shrunk” border is overplotted in green. Right: Border mask. The nasty border corners can be “smoothed” using the sample parameter.


Figure 3 – Zoomed-out version of Figure 2.

▸ Expanding SExtractor detections (segmentation map)

A very useful application is to “expand” (instead of “shrunking”) each of the sources detected by SExtractor. Since the “tail” of the emission of a source extends well beyond any radius (whether parametric or non-parametric) this method can be used to generate a conservative mask for all the objects in the image. Such mask is useful e.g. when examining the background.
In this case, we can use the SExtractor “segmentation mask” as a starting point. The segmentation mask indicates the contiguous pixels composing each object. The script will draw a contour around each detection, then expand and mask it.
NOTE: the file produced in this way must be inverted (i.e. switch 1 and 0) to obtain a standard mask.


Figure 4 – Left: SExtractor sgmentation map (the funky color scheme is to highlight the different sources). Right: Mask in which each source has been expanded.

▸ Funny stuff

The script seems to be pretty flexible, so you can draw contours of anything you like!
Yes, I should still tell you how to load the image of an octopus into a FITS file, but that’s for an other time …


Figure 5 – The borders of our Lord Chtulhu



Posted on May 4, 2016, in Uncategorized. Bookmark the permalink. 1 Comment.

  1. its amazing!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

  • Histats
  • %d bloggers like this: