Files
Introduction
The RasterExpressionEvaluator is a transformer that evaluates expressions on each cell in a raster, such as algebraic operations or conditional statements. This article will demonstrate commonly used raster transformations using the RasterExpressionEvaluator.
The basic syntax when using the RasterExpressionEvaluator is:
- A[x] Band x of raster feature A
- B[x] Band x of raster feature B
Bands start counting at zero (so a three-band raster is A[0], A[1], and A[2]). For more information on syntax or other examples, see the RasterExpressionEvaluator or the articles in the Additional Resources section.
Step-by-step Instructions
The following five examples have the same basic steps to create the workflow; the only differences are the input dataset and the expressions in the RasterExpressionEvaluator. The full steps are provided for Example 1; the other four examples include only instructions for the dataset and expressions.
To follow along with the tutorial, please download the data from the article's Files section.
Example 1: Inverting Colors
This example demonstrates how to invert images.
The images show Heritage Mountain Elementary School and the surrounding area in Google Earth
1. Create a New Workspace
Open FME Workbench and create a blank workspace.
2. Add a PNG (Portable Network Graphics) Reader
Add a reader to the canvas by clicking on the Reader button on the top menu bar or by going to Readers > Add Reader. In the Add Reader dialog, set the following:
- Format: PNG (Portable Network Graphics)
-
Dataset: negative.png
- Click on the ellipses to navigate to the location of the file on your computer
3. Evaluate Raster
Click on the negative reader feature type to select it. Then add a RasterExpressionEvaluator transformer to the canvas by typing “RasterExpressionEvaluator” to bring up the list of FME Transformers in the Quick Add Search. Select the RasterExpressionEvaluator from the list of Transformers by double-clicking or by using the arrow keys and the Enter key to add it.
Double-click on the RasterExpressionEvaluator to open the parameters. In the parameters, since we have only one input parameter, we will set Mode to One Raster; the raster will be referenced using the A variable. If we had two rasters, the other would be B. Next, set the Interpretation and Expression as follows:
-
Interpretation: Red8
-
Expression:
255-A[0]
-
Expression:
-
Interpretation: Green8
-
Expression:
255-A[1]
-
Expression:
-
Interpretation: Blue8
-
Expression:
255-A[2]
-
Expression:
Click OK.
If you are unsure which band interpretation corresponds to which number, open the raster in Visual Preview and inspect a single pixel. In the Feature Information window, each band will have its corresponding interpretation listed.
4. Run Workspace
Connect an Inspector transformer to the RasterExpressionEvaluator Result output port.
Run the workspace by clicking the Run button on the top toolbar, or by selecting Run > Run Workspace from the top menu bar.
After running the workspace, the data will be displayed in Visual Preview.
Example 2: Brightness Correction
If an image is too dark, we can boost the brightness
Port Moody, Greater Vancouver
1. Add a JPEG Reader
In a blank workspace, add a reader to the canvas and set the following parameters:
- Format: JPEG (Joint Photographic Experts Group)
-
Dataset: BrightnessSource.jpg
- Click on the ellipses to navigate to the location of the file on your computer
Click OK to add the reader to the canvas.
2. Increase Brightness
Add a RasterExpressionEvaluator to the canvas and connect it to the JPEG reader. In the parameters, set the Mode to One Raster and input the following expression:
-
Interpretation: Red8
-
Expression:
A[0]*1.5
-
Expression:
-
Interpretation: Green8
-
Expression:
A[1]*1.5
-
Expression:
-
Interpretation: Blue8
-
Expression:
A[2]*1.5
-
Expression:
Click OK.
This expression multiples each band by 1.5. Note that this method should be used cautiously - if there are bright areas in the image, they will reach or exceed the maximum value (255 in my example), which will make them purely white (255, 255, 255 in the case of RGB24), and the information in those areas will be lost.
Example 3: Color Correction
If one of the colors in an image seems to dominate over the others, we can change only one band (or apply different coefficients to different bands).
Port Moody, Greater Vancouver
1. Add a JPEG Reader
In a blank workspace, add a reader to the canvas and set the following:
- Format: JPEG (Joint Photographic Experts Group)
-
Dataset: ColorCorrection.jpg
- Click on the ellipses to navigate to the location of the file on your computer
Click OK.
2. Correct Color
Add a RasterExpressionEvaluator to the canvas and connect it to the JPEG reader. In the parameters, set the Mode to One Raster and input the following expression:
-
Interpretation: Red8
-
Expression:
A[0]
-
Expression:
-
Interpretation: Green8
-
Expression:
A[1]/1.2
-
Expression:
-
Interpretation: Blue8
-
Expression:
A[2]
-
Expression:
Click OK.
Since the image appears greener, we are decreasing the green band by 1.2.
Example 4: Color to Grayscale Conversion
Most sources suggest the following formula for a color-to-grayscale conversion (and many sources also mention that, depending on the image, it may vary). There are two commonly used formulas. The first formula gives better contrast.
Formula 1:
Vancouver, British Columbia
1. Add a JPEG Reader
In a blank workspace, add a reader to the canvas and set the following:
- Format: JPEG (Joint Photographic Experts Group)
-
Dataset: infraredRGB.jpg
- Click on the ellipses to navigate to the location of the file on your computer
Click OK.
2. Convert to Greyscale
Add a RasterExpressionEvaluator to the canvas and connect it to the JPEG reader. In the parameters, set the Mode to One Raster and input the following expression:
-
Interpretation: Grey8
-
Expression:
0.2989*A[0] + 0.5870*A[1] + 0.1140*A[2]
-
Expression:
Click OK.
Formula 2:
Vancouver, British Columbia
3. Convert to Greyscale
Add another RasterExpressionEvaluator to the canvas and connect it to the JPEG reader. In the parameters, set the Mode to One Raster and input the following expression:
-
Interpretation: Grey8
-
Expression:
0.3333*A[0] + 0.3333*A[1] + 0.3333*A[2]
-
Expression:
Click OK.
Example 5: Unit Conversion
Conversion between all kinds of units is a very natural operation for RasterExpressionEvaluator. In this example, we take a numeric raster representing a DEM in meters and convert the units into feet:
North Vancouver, Greater Vancouver
1. Add a CDED Reader
In a blank workspace, add a reader to the canvas and set the following:
- Format: Canadian Digital Elevation Data (CDED)
-
Dataset: 092g02_0101_deme.dem
- Click on the ellipses to navigate to the file location on your computer
Click OK.
2. Convert Units
Add a RasterExpressionEvaluator to the canvas and connect it to the CDED reader. In the parameters, set the Mode to One Raster and input the following expression:
-
Interpretation: Int8
-
Expression:
A[0]/0.3048
-
Expression:
Click OK.
Additional Resources
Advanced cell color manipulation with masks in the RasterExpressionEvaluator
Raster Calculations and Raster Palettes | NDVI Calculator
Elevation Zoning Scenario - Reclassify Rasters Using the RasterExpressionEvaluator
Using Conditions with the RasterExpressionEvaluator Transformer