The COLORIZE_SAMPLE function colorizes a grayscale image by matching luminance levels with an RGB sample table. This is done by finding any matches between the luminance values in the source image and the luminance values of the RGB sample color table. Matching luminance values are replaced with the RGB values from the sample table, therefore colorizing the image.

Note: COLORIZE_SAMPLE can be an imperfect operation because the luminance value does not completely represent the RGB color it could have. There are many RGB values that have the same luminance value. A carefully constructed sample table may produce reasonable results in certain situations.

If there is no luminance value in the sample table that matches image luminance values, the nearest luminance value from the sample table is then used.

If more than one RGB sample color has a given luminance, the colors are distributed in the image where that luminance is present, using the same percentage of RGB distribution of that luminance in the sample table. For example, COLOR1 and COLOR2 are colors in the sample table that have the same luminance value. If COLOR1 appears in the sample table five times more frequently than COLOR2, then COLOR1 will be used to replace five times more pixels than COLOR2 in the resulting image. These multiple colors are distributed randomly to reduce clumping of too many similar colors. COLORIZE_SAMPLE may not produce expected results because of the location and distribution of multiple colors that all have the same luminance values.

Examples


This example colorizes an image with an 8-color sample table:

image = BYTSCL(DIST(400))
sample = BYTARR(3,8, /NOZERO)
sample[0,*] = BINDGEN(8) * 32
sample[1,*] = BINDGEN(8) * 32
result = COLORIZE_SAMPLE(image, sample)
; Display the images side by side:
IIMAGE, image, VIEW_GRID=[2,1], VIEW_TITLE='Original', $
   DIMENSIONS=[850, 550], $
   WINDOW_TITLE='COLORIZE_SAMPLE Example', /NO_SAVEPROMPT
IIMAGE, result, /VIEW_NEXT, VIEW_TITLE='Colorized'
; Increase the text size
ISETPROPERTY, 'text*', FONT_SIZE=40

Syntax


Result = COLORIZE_SAMPLE(Image, Sample)

Return Value


Result is a 3-channel byte array of the same width and height as Image.

Arguments


Image

A 2D or 3D array containing the input image with range [0-255]. A 2D array is treated as a grayscale image. A 3D array must contain RGB image data and be of the form [3 x m x n]. This image is assumed to be grayscale and so only the first channel is used.

Sample

A 2D or 3D array with range [0-255] containing RGB sample colors. The array must be of the form [3 x n] or [3 x m x n] and is treated as a simple list of RGB values.

Keywords


None

Version History


6.4

Introduced