FME Version
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 written out for Example 1, the other four examples only have instructions for the dataset and expressions.
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 PNG (Portable Network Graphics) 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, select PNG (Portable Network Graphics) as the Format, then for Dataset, browse to the negative.png dataset which is available for download from the Files section on this article.
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 only have one input parameter, we will leave the Mode set 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 | Expression |
Red8 | 255-A[0] |
Green8 | 255-A[1] |
Blue8 | 255-A[2] |
If you are unsure which band interpretation is what 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 on the Run button on the top toolbar, or by using Run > Run Workspace on 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 JPEG (Joint Photographic Experts Group) reader to the canvas and browse to the BrightnessSource.jpg dataset.
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 | Expression |
Red8 | A[0]*1.5 |
Green8 | A[1]*1.5 |
Blue8 | A[2]*1.5 |
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 case of RGB24), and the information in those areas will be lost.
Example 3: Color Correction
If one of the colors on 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 JPEG (Joint Photographic Experts Group) reader to the canvas and browse to the ColorCorrection.jpg dataset.
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 | Expression |
Red8 | A[0] |
Green8 | A[1]/1.2 |
Blue8 | A[2] |
Since the image appears greener in color, 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 JPEG (Joint Photographic Experts Group) reader to the canvas and browse to the ColorCorrection.jpg dataset.
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 | Expression |
Grey8 | 0.2989*A[0] + 0.5870*A[1] + 0.1140*A[2] |
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 | Expression |
Grey8 | 0.3333*A[0] + 0.3333*A[1] + 0.3333*A[2] |
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 Canadian Digital Elevation Data (CDED) reader to the canvas and browse to the 092g02_0101_deme.dem dataset.
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 | Expression |
Int32 | A[0]/0.3048 |
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
Comments
0 comments
Please sign in to leave a comment.