DodgeCmd Image Dodging Algorithm A Technical White Paper July 2008 Intergraph ZI Imaging 170 Graphics Drive Madison, AL 35758 USA www.intergraph.com
Table of Contents ABSTRACT...1 1. INTRODUCTION...2 2. ALGORITHM DESCRIPTION...3 3. PARAMETER SELECTION: TONE ENHANCEMENT CASE STUDIES...5 3.1 REGULAR CASE...5 3.2 DARK AND BRIGHT SPOTS...8 3.3 IMAGE BIT DEPTH AND HISTOGRAM RANGE...10 3.4 PRESERVING MODULATION IN HISTOGRAM OUTLIERS...11 3.4 PREVENTING OVERCOMPENSATION AND COLOR SHIFT...14 4. DODGECMD COMMAND LINE ARGUMENTS...15 New Dodge Algorithm Page ii
Abstract This document describes changes implemented in the 5.2 release of the Intergraph commercial photogrammetry software to the image dodging algorithm employed by ImageStation OrthoPro, AutoScan, and DMC PPS. The main Interface change is the name of the command-line executable; the new name is DodgeCmd.exe. The main algorithm change is in the way the pixel corrections applied to create the output image are calculated. In DodgeC, a local LUT was computed and applied per image tile. DodgeCmd uses a local remapping function selected for each individual pixel. This change eliminates pixilation (or tiling effects) in the dodged images which are especially noticeable over large bodies of water. This change has a major effect on parameter selection. Users should now set a small kernel size (-k=5), a large sub-tile size (-t=256 or up to the source image tile size), and should also enable processing from an existing overview (-u) to save processing time. New Dodge Algorithm Page 1
1. Introduction The image dodging process attempts to correct uneven lighting conditions across a photo frame due to the position of the camera with respect to the sun, fragmented cloud cover, and other factors. The same dodging process can also be applied to correct low spatial frequency changes across an image due to uneven reflectance caused by difference in soil moisture, etc. In general, image dodging can compensate for tonal trends in the imagery that can be approximated by a very smooth surface on the raster grid. The image dodging application, DodgeCmd.exe, is a raster file converter and image enhancement utility. The DodgeCmd executable is being delivered with the 5.2 release of the Intergraph commercial photogrammetry products and is a replacement for the earlier dodging executable DodgeC. The DodgeCmd executable can be executed via a GUI interface through ImageStation OrthoPro, ImageStation PhotoScan, and DMC PPS. It can also be run directly through a DOS command line interface. DodgeCmd will read from any ZIRFL supported raster file format and write to one of a few tiled image formats. It can output to both tiled TIFF and JP2000, or the user can specify the output format be the same as the import format if the input format supports tiling. Dodging is best applied to images having only a mild imbalance of base tone variation and a histogram spanning the full radiometric range. Given any deviation from these conditions, successful dodging may only be possible on a local basis; i.e., for any given region selected within an image, a special set of parameters would need to be applied to dodge just that area with the effect softly blended into the surrounding raster data. DodgeCmd is a global image utility which applies a single set of parameters to every area in the image. The point here is that the best parameter set may be highly selective from spot to spot and from image to image such that there may be no perfect setting that can be applied by DodgeCmd to a block of images with the same success unless the tone imbalance is very mild and the image content is very homogenous. DodgeCmd attempts to overcome tonal imbalances across a tiled image by applying a discrete correction value to each pixel in the source image. The correction values are calculated to darken bright areas of the image and lighten dark areas, shifting the entire image towards a user specified tonal center (usually pixel value 127 for the 8-bit image) or a histogram average for each band (auto mode). Briefly, the algorithm used by DodgeCmd works as follows: A low-pass filtered reference sub-image (an overview) is used to define a local tonal center for a group of pixel values within each sub-tile: an average (more specifically, the histogram median value) of the image pixels represents one pixel in the sub-image before filtering. The sub-tile size is a power-of-two New Dodge Algorithm Page 2
number ranging from 8 to the output tile size (usual tile size is 256). A local tonal center for each individual pixel position in the source image is computed by bi-linear interpolation within the sub-image and used to determine a remapping function that corrects this pixel value. Previously in DodgeC, the pixel remapping function consisted of a look up table (LUT) which was applied on a sub-tile by sub-tile basis with the risk of generating visible edges along the sub-tile boundaries. This is no longer the case, since bi-linear interpolation calculates a correction for each pixel separately 2. Algorithm Description The DodgeCmd algorithm performs the following tasks: 1. The histogram median pixel value is calculated for each sub-tile in the source image. The median calculation can be restricted to a user defined range of intensities (e.g. 3 < pixel_intensity < 252 for an 8 bit per band image). This is useful if the source image has fill areas that could throw off the calculations that occur later on in the process. The command line arguments for restricting the pixel range are +p (max intensity) and p (min intensity). This step is skipped if the user elects to use an existing overview (- u command line argument). 2. The array of median pixel values in the sub-image is clipped according to the min_gamma_shift and max_gamma_shift values (-g and +g command line options) in order for each source gamma center not to deviate above the threshold from the destination gamma center. A low-pass filter is then applied to smooth the sub-image pixel values. The size of the low pass filter is defined by the kernel size (-k command line option). This process reduces the effect of local anomalies. The filtered sub-image represents a given-resolution overview of the base tone variation map in the image. The larger the kernel size, the lesser the effect of a single patch of small dark or bright pixels on the calculation of the tonal variation map. 3. Based on the spatial tone map, each pixel will be assigned a correction based on a quadratic function designed to shift the 'average pixel value' of each local area to a destination pixel value (the defined histogram center or the computed band average). The shift will never exceed minus 'min_gamma_shift' or plus 'max_gamma_shift' defined by the user, regardless of the profile of the remapping function. The exact procedure for pixel remapping is as follows: For each pixel, a local base tone value is computed by bi-linear interpolation within a reduced resolution grid (sub-image) of base tone values. A difference between the interpolated tonal center and the target tonal center is multiplied by a soft gain to produce a correction value added to this pixel. The Soft gain LUT has the following quadratic profile shown in Figure 1, which is implemented in the code as a LUT of 12-bit scaled integer factors. New Dodge Algorithm Page 3
Figure1: Soft-gain LUT As a result of this composite operation, the general shape of LUT profiles is obtained as shown in Figure 2. LUTs for differential correction of value range center by ±32 pixels, and ±64 pixels are shown. Fig.2 LUT for pixel remapping function For the darkening mode, a dodging LUT profile is similar to a gamma LUT profile, while for the brightening mode, the effect is different due to the fact New Dodge Algorithm Page 4
that maximal dodging correction is always achieved in the center of LUT range. For dark pixels in the first half of the range, the brightening LUT performs more like a linear contrast stretch. For comparison, a family of γ 1 x gamma LUT profiles, y = xmax, is presented in Figure 3. xmax Fig.3 Gamma LUT profiles 3. Parameter Selection: Tone Enhancement Case Studies 3.1 Regular Case Dodging is best applied to images having mild imbalance of base tone variation and histogram spanning full radiometric range. Given any deviation from these conditions, dodging may only be possible on a local basis. That is, for any feathered region selected within an image, a special set of parameters would need to be applied to dodge just that given region with the effect softly blending into the surrounding raster data. DodgeCmd is a global image utility which applies a single set of parameters to every area in the image. The point here is that the best parameter set is highly selective from region to region, and from image to image such that there may be no perfect setting that can be applied to a block of images with the same success unless tone imbalance New Dodge Algorithm Page 5
is very mild and image content is very homogenous. Assuming these conditions to be the case, the first parameter to select is the sub-tile size. The tiles of an image are divided into smaller squares (sub-tiles). The greater the number of sub-tiles in an image, the higher the spatial frequency of the dodging effect. To avoid sharp variation of the tonal correction, a smoothing filter with a low-pass kernel is used to process the sub-image before the corrections are calculated. In this way, a more gradual transition of correction values across an image is obtained. This process is illustrated in Figures 4 through 7. Figure 4: Input and output data: -p=15 +p=220 g=-64 +g=64 k=5 -t=128 c=127 For an image size of 1500x1500 pixels, a tile size 256x256 pixels, a sub-tile size of 128x128 pixels, and a kernel size of 5x5 pixels, the resulting subimage is only 12x12 pixels in size. One can see in Figure 7, that in this example, the main brightening happens in lower two corners while main darkening happens in the upper mid-section. New Dodge Algorithm Page 6
Figure 5: Sub-image before and after smoothing (nearest neighbor view) Figure 6: Histogram of the base luminance tone map (smoothed sub-image) Figure 7: Base luminance tone deviation from target center (-c=127) New Dodge Algorithm Page 7
3.2 Dark and Bright Spots Figure 8: Image with cloud shadows and soil moisture variations (auto-stretched view) The image in Figure 8 has dark areas due to cloud cover and bright areas due to differences in soil moisture content. The source image is a single band, 12 bit DMC image with a histogram center pixel intensity of 583. The full histogram fits into the first half of the 12 bit intensity range making this image in effect an 11-bit, or possibly a 10-bit image. A default parameter set for this bit resolution might be: -p=31 +p=4094 g=-655 +g=655 k=9 -t=32 c=2047 The result of dodging with auto-selected histogram center (583 in this case) and target center of 2047 is demonstrated in Figure 9. The resulting histogram center is 549 for the middle image and 1251 for the right image. Due to the fact that source histogram center of 583 is too far from the range center of 2047, a contrast stretch of the left part of the range can drive the output histogram center only as far as 1251. One can observe that since bright spots have had less deviation from the source center than dark ones they mostly disappear in the output (see the middle image). The effect of cloud shade is somewhat attenuated as well. The resulting radiometric modulation in the middle image is significantly reduced (Note its histogram spread). This may not be the desired effect; moreover, one may need to preserve brighter part of radiometric modulation (white spots) while reducing the effect of shadows only. New Dodge Algorithm Page 8
Figure 9: Input, auto-selected and target-centered outputs New Dodge Algorithm Page 9
Figure 10: Output for setting: -p=31 +p=3087 g=-30 +g=2047 k=3 -t=8 c=3071 One possible solution to this problem would be to set target center farther to the right and restrict the darken maximum as illustrated in Figure 10. Since there are no input histogram values too far to the right that might be clipped by the brightening correction, a target value of 3071 is an appropriate choice; it drives target histogram to the 1744 center. A smaller sub-tile and smaller kernel size also help to amplify the effect in the dark spots that look relatively sharp over the forest background. Applying dodging twice (trying to amplify the effect) usually does not provide the desired effect, and therefore is not recommended. An example of repeating the process for the image in Figure 10 is shown in Figure 11. Figure 11: An example of double-dodge effect This example demonstrates severe degradation of contrast modulation transfer while amplifying the effect of cloud shadow compensation. 3.3 Image Bit Depth and Histogram Range There is a major difference in the dodging effect given the same set of parameters for the same image if one specifies two different bit resolutions. Consider the example of Section 3.2. It has been said that the full range of this image s input histogram fits into 2048 bins, i.e. the 11-bit data range. If New Dodge Algorithm Page 10
one changes the bit resolution packet in the image header to 11 bit, the result of the same operation can be quite different as shown in Figure 12. Figure 12: An example of dodging 11-bit image -p=31 +p=2044 g=-655 +g=655 k=9 -t=32 c=1023 The only significant parameter change is to specify a target center of 1023 for the right image since this is a center of 11-bit range. The main configuration options ±g, -k, -t remained the same, but the shape of the output histogram is quite different due to a difference in position of the source histogram center with respect to the value range center. In the latter case, the dodging effect on the spatial features of higher frequency is more profound (compare the right hand image histograms in Figures 9 and 12). If this effect is not desired, the sub-tile size and kernel size must be increased. 3.4 Preserving Modulation in Histogram Outliers Moving target histogram center to the right in order to achieve overall contrast stretch works well if there are no far-right outliers in the areas that contain valuable radiometric modulation data. Otherwise, information might be lost as demonstrated in Figure 13. New Dodge Algorithm Page 11
Figure 13: Input/Output: -p=31 +p=3087 g=-1024 +g=1024 k=5 -t=128 c=2047 New Dodge Algorithm Page 12
To prevent this situation, the recommendation is to select a target histogram center closer to the input histogram center or not specify it at all. This solution is demonstrated in Figure 14. Figure 14: Input/Output: -p=31 +p=3087 g=-1024 +g=1024 k=5 -t=128 c=1024 New Dodge Algorithm Page 13
3.4 Preventing Overcompensation and Color Shift There are situations when the default parameters may lead to severe overcompensation effects. This usually happens when the source image does not actually need any dodging. This is because an equally large shift in band intensities, even retaining the same hue, is perceived as a color change, as shown in Figure 15. Fig.15 Input/Output: -p=1 +p=254 g=-63 +g=63 k=5 -t=16 c=127 There is no need to dodge this image (at least not globally). However, if one wants to perform certain meaningful balance, the following can be recommended for the right parameter set: Since the input histogram is well spanned and centered at 150, one would want to preserve it. So, the task may be to brighten some areas at the bottom, stay at histogram center of 150, and admit no darkening. The settings illustrated in Figure 16 are a much better choice for this task. New Dodge Algorithm Page 14
Fig.16 Input/Output: -p=1 +p=254 g=-1 +g=63 k=5 -t=16 c=150 4. DodgeCmd Command Line Arguments If DodgeCmd is run without any command line arguments, the executable will list out the following help which is included here as a convenience Usage: DodgeCmd.exe -i=<"input_file"> -o=<"output_file"> [-key3=value3 <etc...>] or DodgeC.exe -i <input file> -o <output_file> [-key3 value3 <etc...>] Documented key info: -i <input file name> or <input path/wildcard> for batch console operation -o <output file name> or <output dir name> -d: "extra debug output mode on" -v: "verbose mode on" -q jpeq_q_factor: "JPEG compression factor (0=uncompressed)." -S tile_size: "tile size for TIFF output: 32, 64, 128, 256, 512, and 1024. Default = same as input file" -T: "force output image to be TIFF." -O: "make full set of averaged overviews." New Dodge Algorithm Page 15
-R: "make full set of overviews, using the specified method if possible a = averaged g = gaussian 8 = 8x8 Kodak ICA approximation Note: If the output image is TIFF, the software will try to use RSetGenDyn.DLL which supports all of the above methods. If it is unable to use RSetGenDyn.DLL (e.g., it is not on the machine or is unregistered), then it will try to use MKOV.DLL which only supports averaged and gaussian overviews. Warning messages will be displayed but processing will continue if the requested overviews cannot be created. " -u: "Use overviews (if existing) to analyze the input image. The advantage of this option will be processing speed. The disadvantage will be that the min & max_pixel_ignore values will not be processed on full resolution data." -c histogram_center: "Target value towards which all bands are shifted -- default is to use average from each band (this may shift the chromatic balance of the image!)" -t sub_tile_size: In pixels (64 means a 64x64 square subtile). Default = calculated automatically. -g max_shift_neg: "Max. allowable negative pixel change. Default = -35 * 2^(bits_per_pixel - 8)" +g max_shift_pos: "Max. allowable positive pixel change. Default = 35 * 2^(bits_per_pixel - 8)" -k odd_kernel_size: "Number of subtiles to average simultaneously. Default = 15" -p min_pixel_ignore: "Pixels less than this value are not used in calculations. Default = 0" +p max_pixel_ignore: "Pixels greater than this value are not used in calculations. Default = 2^(bits_per_pixel)-1" -f fill_value: "Fill value or transparency value. If set, each pixel having this value (for all bands) will remain unchanged and no other pixels in the image will be adjusted to this value." -n: "Nominal color tone balance: de-trend for common slope; i.e., linear gamma change across the entire image" -J target_comp_ratio: "JPEG2000 with Target Compression Ratio. Target compression: 1 to 100. (Note: Overrides Q Factor [-q], Overviews [-O and -R], Forced TIFF [-T], and does not support 4 band.)" -b <band order>: "Band permutation 1-based index order; default is 1 2 3 4, i.e., RGBN; e.g., use "-b 4 1 2 3" for false-color infrared combination to take RGB positions" -x output_file_prefix: prefix string. Default = "D". New Dodge Algorithm Page 16
Intergraph and the Intergraph logo are registered trademarks of Intergraph Corporation. Other brands and product names are trademarks of their respective owners. Intergraph believes that the information in this publication is accurate as of its publication date. Such information is subject to change without notice. Copyright 2003 Intergraph Corporation, Huntsville, AL 35824. New Dodge Algorithm Page 17