Binary selection mask (source code)

= Binary selection mask
{title2=$M_{ij}\in\{0,1\}$}

A \b[binary selection mask] records which pixels belong to a chosen subset of an image. On a finite pixel grid $\Omega$, it is the <indicator function>
$$
M_{ij}=
\begin{cases}
1,&(i,j)\text{ is selected},\\
0,&(i,j)\text{ is excluded}.
\end{cases}
$$
It can represent an area <region of interest>, the result of <image thresholding> or a combination of selection rules. The mask describes membership; its entries need not equal the underlying image intensities.

For a selected pixel count $N>0$, the count and selected <arithmetic mean> are
$$
N=\sum_{(i,j)\in\Omega}M_{ij},
\qquad
\bar I_M=\frac{\sum_{(i,j)\in\Omega}M_{ij}I_{ij}}{N}.
$$
Consider the original arrays
$$
I=\begin{pmatrix}0&10&20\\30&40&50\end{pmatrix},
\qquad
M=\begin{pmatrix}1&0&1\\0&1&0\end{pmatrix}.
$$
The selected values are 0, 20 and 40, so $N=3$ and $\bar I_M=20$. The selected zero-valued pixel still contributes to the denominator. Replacing excluded pixels by zeros and averaging all six entries would instead give $60/6=10$: that asks a different question. An empty mask has $N=0$, so this selected-mean formula is undefined.

For rectangular pixels with calibrated dimensions $\Delta x=0.5\,\mathrm{mm}$ and $\Delta y=0.2\,\mathrm{mm}$, the complete-pixel <area from a calibrated pixel count> is $A=N\Delta x\Delta y=0.30\,\mathrm{mm^2}$. These are stipulated array values and cell dimensions, not a measurement of an object.

A stored mask can use another two-value encoding. <ImageJ>'s Create Mask command stores 255 inside an area selection and 0 outside, corresponding to $B_{ij}=255M_{ij}$ in this model.<ImageJ User Guide by Tiago Ferreira and Wayne Rasband>[IJ1.46r, section27.12.10, Create Mask]{ref} Summing $B$ gives 765 in the example, rather than the selected count 3. Convert the encoding or count selected entries before interpreting a sum as an area.

Two normalized masks $M$ and $Q$ combine by <set intersection> through $M_{ij}Q_{ij}$. Their complement within the specified grid is $1-M_{ij}$. Record the <foreground-background polarity>, coordinate alignment and grid extent when transferring a mask between images.