BYTSCL Function

Scales and converts an array to byte data type.

Usage

    result = BYTSCL(array)

Input Parameters

array—The array to be scaled and converted to byte data type.

Returned Value

result—A copy of array whose values have been scaled and converted to bytes.

Keywords

Max—The maximum value of array elements to be considered. If Max is not specified, array is searched for its largest value.

Min—The minimum value of array elements to be considered. If Min is not specified, array is searched for its smallest value.

Top—The maximum value of the scaled result. (Default: 255)

Discussion

BYTSCL can be used in a variety of applications—for example, to compress the gray levels in an image to suit the levels supported by the particular hardware you are using. It can also be used to increase or reduce the contrast of an image by expanding or restricting the number of gray levels used.

BYTSCL linearly scales all values of array that lie in the range (Minx ≤ Max) into the range (0xTop). The result has the same number of dimensions as the original array.

If the values of array are outside this range (MinxMax), BYTSCL maps all values of array < Min to zero, and maps all values of array > Max to Top (255 by default).

Example 1

To scale an array of floats to byte values, you might enter:

arr = FINDGEN(100)
byt = BYTSCL(arr, Max=50.0)
PRINT, SIZE(byt)
; PV-WAVE prints: 1         100           1         100
PRINT, byt

; PV-WAVE prints the following:
; 0 5 10 15 20 25 30 35 40 45 50 56 61 66 71 76 81 86 91
; 96 101 107 112 117 122 127 132 137 142 147 152 158 163 168 173
;  178 183 188
; 193 198 203 209 214 219 224 229 234 239 244 249 255 255 255 255 
;  255 255 255
; 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
;  255 255 255
; 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
;  255 255 255
; 255 255 255 255 255

Example 2

This example uses the BYTSCL function to enhance the contrast of an image. The image is stored in a byte array, b. The argument:

BYTSCL(b, Min = 50, Max = 70)

in the second call to the TV procedure scales the values of b so all bytes with a value less than or equal to 50 are set to 0, and all bytes with a value greater than or equal to 70 are set to 255. All bytes with a value between 50 and 70 are scaled to lie in the range {0...255}. Galaxy Image before (left) and after (right) contrast enhancement shows the results.

; Open the file galaxy.dat for reading.
OPENR, unit, FILEPATH('whirlpool.img', Subdir = 'data'), $
   /Get_Lun
; Retrieve first galaxy image, which is stored as a 256-by-256
; byte array.
b = BYTARR(512,512)
READU, unit, b
FREE_LUN, unit
!Order = 1
; Load the red temperature color table and create a window big
; enough for two images.
LOADCT, 3
; Display the image, without any contrast enhancement, at left
; side of window.
WINDOW, 0, Xsize = 1024, Ysize = 512
TV, b, 0
; Display the contrast enhanced image at right side of window.
TV, BYTSCL(b, Min = 50, Max = 70), 1

Galaxy Image before (left) and after (right) contrast enhancement

See Also

BYTE,  BYTARR,  BINDGEN