Initial Publication Date: July 20, 2011

Image Math3

By now, it should be second nature for you to think of images as arrays of numbers. You've used the tools in ImageJ to do some amazing things with those numbers. Not it's time to learn how to operate on the numbers themselves with the most powerful tool of all, math!

There are three ways to "do" math on images.

  • Math Operate on a single image. Operate on all of the pixel values in the input image using a constant or mathematical function (log, square, root, reciprocal) to produce an output image.
  • Image Math Operate between two images. Perform an operation between two input images, pixel-by-pixel, with the resulting values being written to the corresponding location in the output image.
  • Matrix Math Operate on a single image. Operate on each pixel in an image using a moving matrix of values, a process called convolution. This is the basis for many ImageJ filters, such as sharpen, smooth, and reduce noise.

You've already experimented a bit with filters, so this session will focus on the first two types of image math.

Math and Contrast Enhancement

Virtually all image operations involve math, but the mechanics are all done behind the scenes where you can't see the exact numbers and operations involved. Earlier in this course you used the Brightness and Contrast window to adjust the contrast of Landsat images in order to create snappier true and false color images. In this exercise, you'll replicate the process using nothing but simple mathematical operations. You'll be making lots of histogramsbe sure the satellite image is the active image window before you make each histogram.

You'll use the Landsat band that typically has the lowest contrast of the visible light bands, blue. You may remember that the Band 1 contrast is low because Earth's atmosphere preferentially scatters blue light. This appears as an even haze over the images in the blue band.

A low contrast image

You will work with another Landsat scenethis one of our nation's capitol.

  • Download and unzip the following Landsat band 1 (blue) image. Landsat P15R33 Band 1 (Zip Archive 22MB Jul21 11)
  • Open the image in ImageJ.
  • Create a histogram of the image.
  • Looking only at the plot, and ignoring the Min (black background) and Max (a few bright pixels) values reported in the histogram window, estimate the range of values from the low end of the histogram curve to the high end. This is more of an eyeball process than a strict mathematical operation.
  • What is the maximum range for an 8-bit grayscale image?

Contrast strrrrretch

Do you see the problem? The image is not using the full range of values available. The narrower this range (the "bump" in the histogram), the lower the contrast. You'll use math to literally "stretch" the narrow range of pixel values out to the full 0255 range available.

Making sense of what you did

    The beginning of this section stated that you would replicate what you can do with the Brightness and Contrast (B&C) image adjustment controls using basic math operations. Compare your results to what can be done with the B&C controls.

    If you think about it, subtraction changed the brightness of the image. In fact, the brightness control in the B&C panel works by adding (increasing the brightness) or subtracting (decreasing the brightness) a constant from every pixel. The contrast control in the B&C window works by multiplying or dividing the image by a constant. So, the sliders in the B&C window work by changing the constants in the equation that converts input pixel values to output pixel values. That equation has a nameit's called the transfer equation, and it simply converts old pixel values to new pixel values.

    See that diagonal line in the histogram at the top of the B&C window? Do you remember the standard formula for a line that you learned in school? That equation was Y = mX + b, where b is the y-intercept and m is the slope. Play with the brightness and contrast sliders in the B&C window. Which one adjusts the slope? Which one adjusts the y-intercept?

    So (drum roll, please), what is the formula for the linear equation you used to adjust the contrast of this Landsat image?

    Disappearing Pixels?

    It may have occurred to you that there are some pixels in this image that were less than 99 and greater than 158 in the original image. When these values are fed into our contrast-o-matic equation above, the result is values less than 0 and greater than 255. What happens to these pixels? Are they sent to pixel purgatory, never to be seen again? ImageJ automatically assigns output pixels with values less than 0 to 0 and pixels with values greater than 255 to 255, so all is well with the world. What you will notice on the final histogram is a large pileup of pixels on both the high and low ends of the histogram.


  • Save any images you want to keep, then close all open images.

Image Math and NDVI

You have just performed mathematical operations ON images. Now it's time to learn how to perform mathematical operations between images. Again, you will be working with Landsat images of Washington, DC. This operation can be memory-intensive, so the images were cropped from the full scene and scaled down 50%.

What is an NDVI?

NDVI stands for Normalized Difference Vegetation Index. It is a type of image that provides a standard method of quantifying and comparing vegetation "greenness" using satellite data. The NDVI is calculated by adding, subtracting, multiplying, and dividing one image by another image (rather than by a constant). The "formula" for NDVI is:

NDVI = (near IR band - red band) / (near IR band + red band)

Index values can range from -1.0 to 1.0, but vegetation values usually range between 0.1 and 0.7. Higher values are associated with healthier vegetation, and snow and clouds have a value around zero. For Landsat Thematic Mapper, red is Band 3 and near IR is Band 4, so the formula could be written NDVI = (Band 4 - Band 3) / (Band 4 + Band 3).

The process of creating an NDVI image will produce several intermediate images. Be prepared to carefully name and save each image, in TIFF format to preserve the integrity of the data.

Download and open both images

  • Download the Band 3 (Zip Archive 1.1MB Jul22 11) and Band 4 (Zip Archive 1MB Jul22 11) images to your computer. (They are zipped TIFF files, to you don't need to unzip them.)
  • Open the Band 3 and Band 4 images into ImageJ. The band 3 image is the red band. Look closely at the agricultural area south of the lake. Notice how dark the fields appear. The water also appears fairly dark. Band 4 is near infrared. In the near IR, the water looks very dark and many of the fields look very bright. These characteristics are typical of bands 3 and 4. They both tend to have very high contrast, especially compared to the Band 1 (blue) image you worked with earlier.

Subtract (Near IR - Red)

The first step in constructing an NDVI is to subtract the Band 3 image from the Band 4 Image. The values of most of the corresponding pixels in these two images are similar, so the numbers in the result will include both positive and negative values, most of them close to zero. To handle the negative values, choose the 32-bit real result option.

  • Choose Process > Image Calculator and enter the first calculation:


  • The result should look like this:


  • Save this image as 4minus3.tif

Add (Near IR + Red)

Next, add the same two images. (This is the image you "normalize" or divide by.)

  • Choose Process > Image Calculator and enter the second calculation:


  • The result should look like this:


  • Save this image as 4plus3.tif Since you're adding, these will all be positive values, but some will be larger than 255. You don't want the range of values to be scaled or truncated to 255, so again you chose a 32-bit floating point result.

Divide (Near IR - Red) / (Near IR + Red)

Finally, divide 4minus3.tif by 4plus3.tif.

  • Choose Process > Image Calculator and enter the final calculation:


  • The result should look like this:


  • Save this image as NDVI.tif
  • Mouse around and look at the values in the image. Most of them should range between -0.1 and 0.9.

Make the data LOOK good

The data should all be in good shape, but it's kind of boring to look at. A special lookup table was created for NDVI images that can be added in ImageJ.

  • Download and unzip this lookup table (.lut) file. NDVI LUT (Zip Archive 749bytes Jul22 11)
  • Activate the NDVI window and drag and drop the lookup table file onto the ImageJ status bar. The lookup table currently covers a much wider range of values than this image contains. You need to better match the lookup table to the values in the image.
  • Choose Image > Adjust > Window/Level, click the Set button, and enter the values shown. Level represents the middle NDVI value and Window is the width of the range of values (-0.1 to 0.9 = 1.0).

  • The only remaining problem is that the lookup table is inverted (upside down). To fix this problem, choose Image > Lookup Tables > Invert LUT. Your final NDVI image should look like this: (click to examine a full-size version of the NDVI Image with a calibration bar)

When you do this with your own images, be very careful to save the intermediate images, keep them open, and clearly label everything.

  • For more information on NDVIs, see this NASA article on Measuring Vegetation.

    Your Assignment: Create a Normalized Difference Vegetation Index (NDVI) Image

    1. Choose a Landsat scene that is interesting to you. To download Landsat band images (you only need bands 3 and 4), go to USGS GloVis or UNH Click 'n Pick.
    2. Download the required bands and crop both images identically to the desired area. You may also scale the images down to a more workable size.
    3. Use the techniques you learned here to create an NDVI image. Try to add the NDVI lookup table and scale and calibration bars to the image. You may also add a scale bar and calibration bar. (The original resolution for Landsat 5 is 30m/pixel, and for Landsat 7 is 28.5 m/pixel.)
    4. Take a screen shot showing your NDVI image.
    5. When you are finished, post the screenshot to the Part 1: Share and Discuss Page. Describe the area shown in your image.

    Source

    1Adapted from Earth Exploration Toolbook chapter instructions under Creative Commons license Attribution-NonCommercial-ShareAlike 1.0.
    2Adapted from Eyes in the Sky II online course materials, Copyright 2010, TERC. All rights reserved.
    3New material developed for Earth Analysis Techniques, Copyright 2011, TERC. All rights reserved.