6. Image Masking

As explained in Part I, masking is a fundamental image processing operation. The images for this demonstration, shown in Figure 6.1 consist of a time series of thermal infrared scenes [courtesy of Roger Berger, CRREL]. The application is to create a mask of certain objects in the image, such as the trees in the foreground, apply the mask sequentially to the other images and generate histograms of the changing IR brightnesses of the masked features. This is possible because the images are co-registered, that is, the features of interest are more or less in the same location from image to image.

In this section, we introduce an advanced Unix concept, the C-shell foreach loop. We have shown the commands explicitly, however, if you are unfamiliar with this aspect of Unix and want more information consult Appendix 3.

6.1 Creating the Mask

6.1.1 Select Area to Mask

The images for this demonstration are located in the directory ipwdemo/mask. Outline an area on the image using xv. and save the coordinates in a coords file.

% xv thermal1.ipw &

% xv thermal2.ipw &

% xv thermal3.ipw &

The area must be filled, so use the fill mode with the version of xv you're using. Exit xv using q or the Quit button.

6.1.2 Create a Blank Image

To build the mask it is necessary to start with a blank image (with all pixels set to zero) of the same pixel dimensions as the image to be masked. The command blank applys a look-up table containing all 0's to the image you are working with. Once you have created a blank image, you can continue to use it with any image of the same size.

% blank thermal1.ipw > blank.ipw

6.1.3 Scribe onto Blank Image

This command line puts the filled areas onto the blank (black) image creating a mask:

% scribe -i blank.ipw -c coords -f > mask.ipw

% xv mask.ipw

By default, the areas are set to 255 (white) as required for mask. The entire procedure for creating a mask is shown in Flowchart 3.

6.2 Using the mask

6.2.1 Apply the Mask

A bitwise `and' operation is used to apply the mask image with the original thermal image and two other thermal images for which the mask is appropriate. Features in the image which coincide with the white areas on the mask image will be displayed; other areas will be black. This is achieved since the white areas have a DN value of 255, which corresponds to all bits set on in bitwise mode.

% foreach i (1 2 3)

? mux mask.ipw thermal$i.ipw | bitcom -a > thermal$i.mask.ipw

? xv thermal$i.mask.ipw &

? end

The 3 resultant images are shown in Figure 6.2

6.2.2 Compute Area within Mask

The number of pixels in the total masked area can be obtained by computing a histogram of the image mask.

% hist < mask.ipw | prhist

Like the distances computed in the section on line lengths, these areas are pixel areas, a scaling factor is required to obtain the actual area.

6.2.3 Print Values of Masked Area

Use the following command sequence to print the DN values of all pixels within the masked area on the standard output (terminal, | another program, or > file).

% poly < coords | primg -i thermal1.ipw

6.2.4 Create a Histogram of the Masked Area

The hist command with the mask option will compute a histogram of all non-zero areas in the image. The output of hist is an IPW image file containing the values of the histogram. This histogram can be converted to a human readable format using either xhist or prhist. The program xhist uses the public domain program xgraph to plot the histogram on an X display. The plotted histogram can be printed on a postscript printer by pressing the Hardcopy button in xgraph. The following command sequence will plot three histograms on the screen. The -t option specifies a title for the histogram.

% foreach i (1 2 3)

? hist -m mask.ipw < thermal$i.ipw | xhist &

? end

These histograms are shown in Figure 6.3

The program prhist prints the numerical values of the histogram on the standard output.

% hist -m mask.ipw < thermal1.ipw | prhist

6.2.5 Generate Statistics for the Masked Area

The command mstats computes the mean and variance. If a mask is provided, it will compute the mean and variance of the unmasked area and masked area separately

% mstats -m mask.ipw < thermal1.ipw > stats

The format of mstats output is:

	#< stats>                          mstats output identifier
	number_of_bands                   # bands in the input image
	mean_0 mean_1 ...                 mean vector
	variance_0                        variance-covariance matrix
	covariance_1,0 variance_1
	...
	* 1                               class number
	number_of_pixels                  number of pixels 

This is repeated for each class in the mask (as generated by the label option of xv, or merge).