When you are working with imagery, you may have the need to replace one or more colors in an image. A common example is a satellite image that comes with black or white borders.

Using the image processing API, you can programmatically apply a pixel replace operation to such data and replace unwanted colors with other, potentially transparent colors. An alternative approach is to rely on OGC SLD/SE styling and use Luciad’s vendor-specific options to replace colors:

Program: Defining a style to replace black and white colors with a transparent color
<?xml version='1.0' encoding='UTF-8'?>
<FeatureTypeStyle xmlns="http://www.opengis.net/se"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.opengis.net/se http://schemas.opengis.net/se/1.1.0/FeatureStyle.xsd"
                  version="1.1.0">
  <Name>color_replacement_style</Name>
  <Rule>
    <Name>color_replacement_style_rule</Name>
    <RasterSymbolizer>
      <VendorOption name="colorReplace">#000000:#00000000,#ffffff:#00000000</VendorOption>
      <VendorOption name="colorReplaceTolerance">0</VendorOption>
    </RasterSymbolizer>
  </Rule>
</FeatureTypeStyle>

To define a color replacement, you can use these options:

  • colorReplace: This option takes a string value consisting of a comma-separated list of value pairs defined as hexSourceColor:hexDestinationColor. In this example,

    #000000:#00000000,#ffffff:#00000000

    replaces the source colors black (#000000) and white (#ffffff) with an identical transparent destination color (#00000000). Note that multiple color replacements are not applied together, but one after the other according to the specified order. As such, a destination color from a previous color replacement is replaced again if it is being used as input color in a subsequent color replacement. For example, if a first color replacement replaces red with green and a subsequent color replacement replaces green with blue, than all red and green colors are replaced by blue.

  • colorReplaceTolerance: This option takes an integer value used for matching the source color in the input image. It defines how closely a color in the source image must match the specified source color to be selected for replacement. The value is defined in the 0-to-255 range and defines the maximum distance between the red, green, blue and alpha components of the source colors specified in the colorReplace option and the corresponding components of the actual colors in the image. If you leave out this option, the default value 0 is used.

The source image may have grayscale, palette, RGB, or RGBA band semantics. The destination colors may be specified as RBG or RGBA colors. If there is a mismatch between the source image and the destination colors, the TLcdColorConvertOp color conversion operation will be performed first to allow for the color replacement. As a result, the visualized image will have RGBA band semantics if the destination colors or the source image include an alpha component; in all other cases, the visualized image will have RGB semantics.

If you save this style in an .sld file with the same file name as the raster data file and store it in the same folder, it is picked up automatically when the data loads in Lucy or Lucy Map Centric.

See Styling data with OGC SLD for more information about using and applying OGC SLD/SE styling.