Previous IDL Reference Guide: Procedures and Functions Next

COLOR_EXCHANGE

The COLOR_EXCHANGE procedure replaces image pixels of a given color with pixels of a new color. For multi-channel images, every image color channel must match the specified color in order for replacement to occur. Specify a threshold value to replace colors close to the specified color.

Syntax

Result = COLOR_EXCHANGE(Image, Color, ReplaceColor [, THRESHOLD=vector])

Return Value

Result is an array of the same dimensions and type as Image.

Arguments

Image

A 2D or 3D array of any basic type containing the input image. 2D arrays are treated as one-channel images. 3D arrays must be of the form [N x n x m] where N is the number of image channels.

Color

An N-element vector that specifies the color in the image that is to be replaced, where N is the number of image channels.

ReplaceColor

An N-element vector that specifies the color to be placed in the image where the image color matches the Color argument, and where N is the number of image channels.

Keywords

THRESHOLD

An N-element vector that specifies the threshold of the color in the array to be replaced, where N is the number of image channels. Colors are replaced if every channel is in the range Color +/- Threshold, inclusive. The default value is a vector of zeroes, specifying that an exact match is needed for replacement.

If a threshold is specified, THRESHOLD promotes the datatype of the threshold value to ensure the comparisons do not overflow.

Examples

This example reads a TrueColor image file and displays the original and two versions to which a color exchange operation has been applied.

; Read a TrueColor image:  
file = FILEPATH('rose.jpg', SUBDIRECTORY = ['examples', 'data'])  
READ_JPEG, file, rose  
  
; Replace all of the image's pure red pixels with green pixels:  
rose2 = COLOR_EXCHANGE(rose, [255,0,0], [0,255,0])  
  
; Replace all of the pixels within a threshold of 60 from the pure 
; red pixel with green pixels:  
rose3 = COLOR_EXCHANGE(rose, [255,0,0], [0,255,0], $  
   THRESHOLD=[60,60,60])  
  
; Display the images side by side:  
WINDOW, XSIZE=(SIZE(rose))[2]*3, YSIZE=(SIZE(rose))[3]  
TVSCL, rose, 0, /TRUE  
TVSCL, rose2, 1, /TRUE  
TVSCL, rose3, 2, /TRUE  

The resulting images appear as follows:

 

Figure 3-18: TrueColor Rose Image: Original and Two with Color Exchange

Figure 3-18: TrueColor Rose Image: Original and Two with Color Exchange


Note
In the first replacement (the center image), there were no exact matches for the pixel value [255,0,0], so no pixels were replaced.

Version History

6.4
Introduced

  IDL Online Help (March 06, 2007)