Intro to Spatial Regions

Summary

This document contains a summary of geometric spatial regions, describing the supported shapes and boolean operations between them.

Description

Spatial region filtering allows a program to select regions of a FITS image to process using simple geometric shapes and boolean combinations of shapes. When an image is filtered, only pixels found within these shapes are processed.

Spatial region filtering is accomplished by means of region specifications. A region specification consists of one or more region expressions, which are geometric shapes,combined according to the rules of boolean algebra. Region specifications also can contain comments and local/global processing directives.

For example, to counts the photons within a circular region using the regcnts program, use:

  $ regcnts snr.fits "circle(512,512,5)"
This will filter a region of the image that looks like this:
  $ regdisp foo.fits "circle 512 512 5"
  regions: circle(512,512,5)
  .............
  .............
  ....11111....
  ...1111111...
  ..111111111..
  ..111111111..
  ..111111111..
  ..111111111..
  ..111111111..
  ...1111111...
  ....11111....
  .............
  .............
(NB: region displays are created using the regdisp program.)

Region Expressions

Region specifications consist of one or more lines containing:
  # comment until end of line
  global   keyword=value keyword=value  ... # set global value(s)
  # include the following file in the region descriptor
  @file
  # each region expression contains shapes separated by operators
  [region_expression1], [region_expression2], ...
  [region_expression], [region_expression], ...

A single region expression consists of:

  # parens and commas are optional, as is the + sign
  [+-]shape(num , num , ...) OP1 shape num num num OP2 shape ...

e.g.:

  ([+-]shape(num , num , ...) && shape num  num || shape(num, num)
  # a comment can come after a region -- reserved for local properties
  [+-]shape(num , num , ...)  # local properties go here, e.g. color=red

Thus, a region descriptor consists of one or more region expressions or regions, separated by comas, new-lines, or semi-colons. Each region consists of one or more geometric shapes combined using standard boolean operation. Several types of shapes are supported, including:

  shape:        arguments:
  -----         ----------------------------------------
  annulus       xcenter ycenter inner_radius outer_radius
  box           xcenter ycenter xwidth yheight (angle)
  circle        xcenter ycenter radius
  ellipse       xcenter ycenter xwidth yheight (angle)
  field         none
  line          x1 y1 x2 y2
  pie           xcenter ycenter angle1 angle2
  point         x1 y1
  polygon       x1 y1 x2 y2 ... xn yn

In addition, the following regions accept accelerator syntax:

  shape         arguments
  -----         ------------------------------------------
  annulus       xcenter ycenter radius1 radius2 ... radiusn
  annulus       xcenter ycenter inner_radius outer_radius n=[number]
  box           xcenter ycenter xw1 yh1 xw2 yh2 ... xwn yhn (angle)
  box           xcenter ycenter xwlo yhlo xwhi yhhi n=[number] (angle)
  circle        xcenter ycenter r1 r2 ... rn              # same as annulus
  circle        xcenter ycenter rinner router n=[number]  # same as annulus
  ellipse       xcenter ycenter xw1 yh1 xw2 yh2 ... xwn yhn (angle)
  ellipse       xcenter ycenter xwlo yhlo xwhi yhhi n=[number] (angle)
  pie           xcenter ycenter angle1 angle2 (angle3) (angle4) (angle5) ...
  pie           xcenter ycenter angle1 angle2 (n=[number])
  point         x1 y1 x2 y2 ... xn yn

Finally, the following are combinations of pie with different shapes (called "panda" for "Pie AND Annulus") allow for easy specification of radial sections:

  shape:        arguments:
  -----         ---------
  panda         xcen ycen ang1 ang2 nang irad orad nrad   # circular
  bpanda        xcen ycen ang1 ang2 nang xwlo yhlo xwhi yhhi nrad (ang) # box
  epanda        xcen ycen ang1 ang2 nang xwlo yhlo xwhi yhhi nrad (ang) # ellipse
The panda and cpanda specify combinations of annulus and circle with pie, respectively and give identical results. The bpanda combines box and pie, while epanda combines ellipse and pie.

See region examples for visual examples of region shapes.

The following "shapes" are ignored by generated by ds9 and ignored by the region filtering code:

  shape:        arguments:
  -----         ---------
  projection    x1 y1 x2 y2 width    # NB: ignored
  ruler         x1 y1 x2 y2          # NB: ignored
  text          x y                  # NB: ignored
  grid                               # NB: ignored
  tile                               # NB: ignored
  compass                            # NB: ignored

All arguments to regions are real values; integer values are automatically converted to real where necessary. All angles are in degrees and run counter-clockwise from the positive image x-axis. If a rotation angle is part of the associated WCS header, that angle is added implicitly as well.

Region Algebra

(See also Region Algebra for more complete information.)

Region shapes can be combined together using Boolean operators:

  Symbol        Operation       Use
  --------      ---------       -----------------------------------
  !             not             Exclude this shape from this region
  & or &&       and             Include only the overlap of these shapes
  | or ||       inclusive or    Include all of both shapes
  ^             exclusive or    Include both shapes except their overlap
Note that the !region syntax must be combined with another region in order that we be able to assign a region id properly. That is,
  !circle(512,512,10)
is not a legal region because there is no valid region id to work with. To get the full field without a circle, combine the above with field(), as in:
  field() && !circle(512,512,10)

Region Separators Also Are Operators

As mentioned previously, multiple region expressions can be specified in a region descriptor, separated by commas, new-lines, or semi-colons. When such a separator is used, the boolean OR operator is automatically generated in its place but, unlike explicit use of the OR operator, the region ID is incremented (starting from 1).

For example, two shapes connected by a boolean OR are given the same region value:

  regions: circle(510,510,5)||circle(516,516,5)
  ...................
  ...................
  ..........11111....
  .........1111111...
  ........111111111..
  ........111111111..
  ........111111111..
  ........111111111..
  ....1111111111111..
  ...1111111111111...
  ..1111111111111....
  ..111111111........
  ..111111111........
  ..111111111........
  ..111111111........
  ...1111111.........
  ....11111..........
  ...................
  ...................
On the other hand, the two shapes separated by a comma are given different region values:
  regions: circle(510,510,5),circle(516,516,5)
  ...................
  ...................
  ..........22222....
  .........2222222...
  ........222222222..
  ........222222222..
  ........222222222..
  ........222222222..
  ....1111122222222..
  ...1111111222222...
  ..1111111112222....
  ..111111111........
  ..111111111........
  ..111111111........
  ..111111111........
  ...1111111.........
  ....11111..........
  ...................
  ...................

Of course these two examples will both mask the same image pixels. However, in programs that distinguish region id's (such as regcnts ), they will act differently. The explicit OR operator will result in one region expression consisting of two shapes having the same region id and regcnts will report a single region. The comma operator will cause regcnts to report two region expressions, each with one shape, in its output.

As a matter of practice, commas or semi-colons are used to separate region expressions entered on the command line:

  # regions are added to the filename in bracket notation
  regcnts foo.fits "circle(512,512,100),circle(400,400,20)"
New-lines are used to separate region expressions in a file:
  # regions usually are separated by new-lines in a file
  # use @filename to include this file on the command line
  circle(512,512,100)
  circle(400,400,20)

If a pixel is covered by two different regions expressions, it is given the mask value of the first region that contains that pixel. That is, successive regions do not overwrite previous regions in the mask. In this way, an individual pixel is covered by one and only one region. This means that one must sometimes be careful about the order in which regions are defined. If region N is fully contained within region M, then N should be defined before M, or else it will be "covered up" by the latter:

  $ regdisp foo.fits "circle 512 512 5; circle 512 512 3"
  regions: circle 512 512 5; circle 512 512 3
  .............
  .............
  ....11111....
  ...1111111...
  ..111111111..
  ..111111111..
  ..111111111..
  ..111111111..
  ..111111111..
  ...1111111...
  ....11111....
  .............
  .............

  $ regdisp foo.fits "circle 512 512 3; circle 512 512 5"
  regions: circle 512 512 3; circle 512 512 5
  .............
  .............
  ....22222....
  ...2222222...
  ..221111122..
  ..221111122..
  ..221111122..
  ..221111122..
  ..221111122..
  ...2222222...
  ....22222....
  .............
  .............

The order in which mask values are assigned can be reversed by setting the REGIONS_PAINT environment variable to true. This puts region filtering into "paint mode", so that new regions overwrite old regions:

  $ env REGIONS_PAINT=true regdisp foo.fits "circle 512 512 5; circle 512 512 3"
  regions: circle 512 512 5; circle 512 512 3
  .............
  .............
  ....11111....
  ...1111111...
  ..112222211..
  ..112222211..
  ..112222211..
  ..112222211..
  ..112222211..
  ...1111111...
  ....11111....
  .............
  .............

Region Exclusion

Shapes also can be globally excluded from all the region specifiers in a region descriptor by using a minus sign before a region:
  operator      arguments:
  --------      -----------
  -             Globally exclude the region expression following '-' sign
                from ALL regions specified in this file
The global exclude region can be used by itself; in such a case, field() is implied.

A global exclude differs from the local exclude (i.e. a shape prefixed by the logical not "!" symbol) in that global excludes are logically performed last, so that no region will contain pixels from a globally excluded shape:

  regdisp foo.fits "-circle 512 512 3; circle 512 512 5; circle 514 512 5"
  regions: -circle 512 512 3; circle 512 512 5; circle 514 512 5
  ...............
  ...............
  ....1111122....
  ...111111122...
  ..11.....1122..
  ..11.....1122..
  ..11.....1122..
  ..11.....1122..
  ..11.....1122..
  ...111111122...
  ....1111122....
  ...............
  ...............
A local exclude is used in a boolean expression with an include shape, and only excludes pixels from that include shape. Global excludes cannot be used in boolean expressions.
  regdisp foo.fits '!circle 512 512 3&&circle 512 512 5; circle 514 512 5'
  regions: !circle 512 512 3&&circle 512 512 5; circle 514 512 5
  ...............
  ...............
  ....1111122....
  ...111111122...
  ..11222221122..
  ..11222221122..
  ..11222221122..
  ..11222221122..
  ..11222221122..
  ...111111122...
  ....1111122....
  ...............
  ...............
Here, the local exclude applied to the region at 512,512 and the space cleared was then filled in by the region at 514, 512.

Note that in REGIONS_PAINT mode, excludes are done in the order in which they appear, not globally at the end. Therefore, order matters:

env REGIONS_PAINT=true regdisp foo.fits "-circle 512 512 3; circle 512 512 5; circle 514 512 5"
regions: -circle 512 512 3; circle 512 512 5; circle 514 512 5
...............
...............
....1122222....
...112222222...
..11222222222..
..11222222222..
..11222222222..
..11222222222..
..11222222222..
...112222222...
....1122222....
...............
...............

env REGIONS_PAINT=true regdisp foo.fits "circle 512 512 5; circle 514 512 5; -circle 512 512 3"
regions: circle 512 512 5; circle 514 512 5; -circle 512 512 3
...............
...............
....1122222....
...112222222...
..11.....2222..
..11.....2222..
..11.....2222..
..11.....2222..
..11.....2222..
...112222222...
....1122222....
...............
...............

Include Files

The @filename directive specifies an include file containing region expressions. This file is processed as part of the overall region descriptor:
  regcnts foo.fits "@foo"

Global and Local Properties of Regions

The ds9 image display program describes a host of properties such as color, font, fix/free state, etc. Such properties can be specified globally (for all regions) or locally (for an individual region). The global keyword specifies properties and qualifiers for all regions, while local properties are specified in comments on the same line as the region:

  global color=red
  circle(10,10,2)
  circle(20,20,3) # color=blue
  circle(30,30,4)
The first and third circles will be red, which the second circle will be blue. Note that regions currently ignores these properties, as they are used in display only.

Coordinate Systems

For each region, it is important to specify the coordinate system used to interpret the region, i.e., to set the context in which position and size values are interpreted. For this purpose, the following keywords are recognized:
  name          description
  ----          ------------------------------------------
  PHYSICAL      pixel coords of original file using LTM/LTV
  IMAGE         pixel coords of current file
  FK4, B1950    sky coordinate systems
  FK5, J2000    sky coordinate systems
  GALACTIC      sky coordinate systems
  ECLIPTIC      sky coordinate systems
  ICRS          currently same as J2000

Specifying Positions, Sizes, and Angles

The arguments to region shapes can be floats or integers describing positions and sizes. They can be specified as pure numbers or using explicit formatting directives:
  position arguments    description
  ------------------    ------------------------------
  [num]                 context-dependent (see below)
  [num]d                degrees
  [num]r                radians
  [num]p                physical pixels
  [num]i                image pixels
  [num]:[num]:[num]     hms for 'odd' position arguments
  [num]:[num]:[num]     dms for 'even' position arguments
  [num]h[num]m[num]s    explicit hms
  [num]d[num]m[num]s    explicit dms

  size arguments        description
  --------------        -----------
  [num]                 context-dependent (see below)
  [num]"                arc seconds
  [num]'                arc minutes
  [num]d                degrees
  [num]r                radians
  [num]p                physical pixels
  [num]i                image pixels
When a "pure number" (i.e. one without a format directive such as 'd' for 'degrees') is specified, its interpretation depends on the context defined by the 'coordsys' keyword. In general, the rule is:

All pure numbers have implied units corresponding to the current coordinate system.

If no such system is explicitly specified, the default system is implicitly assumed to be PHYSICAL.

In practice this means that for IMAGE and PHYSICAL systems, pure numbers are pixels. Otherwise, for all systems other than linear, pure numbers are degrees. For LINEAR systems, pure numbers are in the units of the linear system. This rule covers both positions and sizes.

The input values to each shape can be specified in several coordinate systems including:

  name          description
  ----          ----------------------------
  IMAGE         pixel coords of current file
  LINEAR        linear wcs as defined in file
  FK4, B1950    various sky coordinate systems
  FK5, J2000
  GALACTIC
  ECLIPTIC
  ICRS
  PHYSICAL      pixel coords of original file using LTM/LTV

If no coordinate system is specified, PHYSICAL is assumed. PHYSICAL or a World Coordinate System such as J2000 is preferred and most general. The coordinate system specifier should appear at the beginning of the region description, on a separate line (in a file), or followed by a new-line or semicolon; e.g.,

  global coordsys physical
  circle 6500 9320 200
The use of celestial input units automatically implies WORLD coordinates of the reference image. Thus, if the world coordinate system of the reference image is J2000, then
  circle 10:10:0 20:22:0 3'
is equivalent to:
  circle 10:10:0 20:22:0 3' # j2000
Note that by using units as described above, you may mix coordinate systems within a region specifier; e.g.,
  circle 6500 9320 3' # physical

Note that, for regions which accept a rotation angle:

ellipse (x, y, r1, r2, angle)
box(x, y, w, h, angle)
the angle is relative to the specified coordinate system. In particular, if the region is specified in WCS coordinates, the angle is related to the WCS system, not x/y image coordinate axis. For WCS systems with no rotation, this obviously is not an issue. However, some images do define an implicit rotation (e.g., by using a non-zero CROTA value in the WCS parameters) and for these images, the angle will be relative to the WCS axes. In such case, a region specification such as:
fk4;ellipse(22:59:43.985, +58:45:26.92,320", 160", 30)
will not, in general, be the same region specified as:
physical;ellipse(465, 578, 40, 20, 30)
even when positions and sizes match. The angle is relative to WCS axes in the first case, and relative to physical x,y axes in the second.


Other pages of interest:
Last updated: August 7, 2015