Style: Autocontrast

The Style pane allows us automatically to set contrast for R, G, B, or A channels used in an image or by changing interval values for palettes when a single channel is used to drive a palette image.  See the About Images, Images and Channels, and Palette Images topics for background information.


We may be familiar with contrast settings from graphics editors, where changing a contrast setting increases or decreases the difference between darker and lighter pixels.  Contrast can be improved to accentuate the difference by altering how values in the data are displayed so that in some cases light pixels are even lighter and dark pixels even darker.


Graphics editors like Photoshop alter contrast by altering the actual data in the image.  Contrast between light and dark pixels is increased by altering the colors of those pixels.  Because data is more important in GIS, Manifold changes visual contrast not by changing the data, but by changing how that data is displayed.  The data remains unchanged while the rules for how given pixel values are displayed are changed on the fly.  We can change contrast settings as much as we like in Manifold and then at any time return to the original display settings, even if in the interim we have saved and exited the project and then re-launched it again.

Contrast and Grayscale Images

Consider how contrast works for grayscale images:  In a grayscale image, pixels can have a grayscale color value from 0 to 255.  When displayed on a computer monitor, that same grayscale value between 0 and 255 for each pixel is used to power all three R, G, and B display elements for that pixel.   When all three R, G, and B display LEDs for a pixel are powered to exactly the same brightness level, the visual result is a shade of gray.   Grayscale images on a computer monitor are driving the R, G and B display elements with a triplet of numbers where all three numbers are the same.  


For brevity, we will usually refer to a particular grayscale color with a single number, saying "the color 130" instead of "the color (130, 130, 130)".   For example, the light gray default fill color for areas in Manifold uses R, G and B values of 192, 192, and 192.  For brevity, we call that the "grayscale color 192."


If the values in the data are spread out using the full range of values from 0 to 255 to display an image, then features shown in different grayscale colors will tend to have good contrast, because there will be a big visual difference between very bright pixels with high values like in the 250 to 255 range and very dark pixels with low values from 0 to 5.  We can see this effect with a simple experiment.


Imagine an image that was created from pixels with grayscale color values that are all similar to each other, such as a range of grays from 120 to 130, where a line of pixels using the lighter gray color 130 would appear against a slightly darker background gray color made up of pixels in color 120, as seen below.



The visual difference between those two different shades of gray is very small so there is little contrast between the line and its background.   



There would be much greater contrast between the line and the background if the line were pure white, using a value of 255 against a background of pixels colored in pure black with a value of 0.   If we had an image where a line of pixels using color 130 appeared against a background of color 120 we could increase the visual contrast in the image dramatically by displaying those pixels on a monitor as if the pixels which had a value of 130 instead had a value of 255 and as if the pixels which had a value of 120 instead had a value of 0.   


The visual result would be the same as if the data contained pixels using a value of 255 for the line and pixels using the value 0 for the background, as seen in the image above.  This is an example of stretching the range 120 to 130  to fit into the range 0 to 255.   It is increasing the ten color difference in the range 120 to 130 by a factor of twenty five into the range 0 to 255 where the two colors in use are two hundred fifty five colors apart, a much bigger difference.


Although the greatest visual contrast is between the extremes of black and white, we do not usually want to display a data set that contains a range of grayscale colors as if the all the colors were either totally black or completely white.  Sometimes we might want to do that but usually that is too harsh. Instead, we normally would like to pick out which range of pixels will be adjusted and how that range will be stretched to give greater differences between colors in the range.  Usually we want to do that not in a purely trial and error way but to use some reproducible way of adjusting contrast in a standard manner.   Mathematically, a very good and standard way of picking out a range of pixels to adjust for contrast purposes is to calculate the standard deviation of the values which appear in pixels in the data set and to then adjust color values in some given range based on the standard deviation.  That is both repeatable and it usually produces good visual results.


Let us see how that works with an example image, the terrain elevation data for a section of Florida that is used in the Example: Autocontrast and Hill Shading Images using Style topic.   When we first open the image it is dark overall and has poor contrast.  



Why is the image so dark and with such poor contrast?   We can learn more by opening the Style pane and clicking on the right side of the style button to open up the channel arrangement:



The Style pane shows us a default interpretation of this particular data set, based on the format from which it was imported.  The raster data for this particular data set produces an RGBA image, where the single channel of data (one number for each pixel) within the imported file has been used to drive all three R, G, and B display outputs.  The alpha channel, which could be used to set transparency, is set to zero for all pixels, meaning zero transparency (fully opaque).   Since the color values for all three outputs are the same for each pixel, the result is a grayscale image.


Each R, G, or B display output used in computer monitor hardware can be powered at a level from 0 (totally dark) to 255 (full brightness).   The number ranges from 0 for 255 shown in the Style pane display above mean that numbers which appear in the data will be taken literally as the value to drive the display output for that pixel. By default, Manifold is interpreting each value in the data as an output level.   If the number 127 appears for a pixel in the data, that means the display outputs for that particular pixel will be driven at about half of full brightness.   


From working with this data set previously, we know the numbers in the data set are all between 0 and 92.    If we didn't know that from prior experience with the data set we could instantly see what the range of values are by using the Full Range command, as we will do in just a few paragraphs below.  


The values in the pixels for this data set are heights of terrain in that part of Florida, which ranges from almost 0 meters above sea level to approximately 92 meters above sea level.   The region shown surrounds the Lake Wales Ridge, a slight rise in elevations in the center of Florida.


Because the values in the data directly drive the display outputs, that means data values in the range from 0 to 92 result in grayscale output values in the range from 0 to 92, all of which are darker shades of gray.


A grayscale value of, say, 60, for a pixel means that the terrain in the region covered by that pixel is 60 meters above sea level.  The part of Florida covered by the data, like most parts of Florida, is relatively flat country with very little difference in the height above sea level in various parts of the terrain.  


If we simply use the height above sea level directly as a grayscale color the result therefore is an image that has very little difference in the gray colors used in the image, that is, very little contrast.   Because Florida is not only flat but also very low near sea level in most regions, the heights are also low values,  which means dark colors when those low values, such as 60, are used as grayscale colors.   The result is a dark image with poor contrast.


There are no brighter shades of gray such as those we might see if pixels had values from around 150 or so all the way to white at 255.  The image is dark and contrast is low since it consists of dark, grayscale colors that are visually similar to each other.  In general, an image that is formed from pixels that all fall within a narrow range of brightness within the possible range will have low contrast.   


To create a brighter image with more contrast we must emphasize the differences in heights, and thus colors, by increasing them from a range from 0 to 92 to a bigger range, from 0 to  255.   If we take a range of pixel values from 0 to 92 and display them as if each was multiplied to give a bigger range, from 0 to  255, we would increase the brightness difference, that is the contrast, between the darkest pixel and the lightest by about two and a half times.   



The Style pane shows that a single channel, data Channel 0 drives all three R, G and B display outputs.  Channel 0 is applied to all three display outputs since this image is raster data in the form of a single data channel.   To manipulate how that single channel is used to create an image we have two possible approaches, as shown in the Example: Autocontrast and Hill Shading Images using Style topic:




Applying the Full Range

We begin by illustrating the effect of the Full Range command.  In the Style pane we click into the list of outputs and ranges to move the focus into the list.



We then press Ctrl-A to select all of the rows.



When all of the rows are selected, we can change the ranges in all of them at once by changing the range in any of them.   To do that we right-click onto one of the range cells, such as the 0 or the 255 range cell in any of the three R, G, or B rows.



In the context menu we choose Full Range.  



That loads the range boxes for all three outputs to the full range of values found in the data, in this case from near 0 to approximately 92.   We press Update Style to apply the changes to the display.



Immediately, the display lightens and contrast improves.   This is the effect obtained by taking the Channel 0 values in the data, which range from 0 to 92, and displaying them as if they covered a bigger range, from 0 to  255.    Manifold is interpolating on the fly values for each pixel by multiplying them by approximately 2.7, so that a value of 10 in the data drives the display outputs as if it were 27, and a value of 92 drives the outputs at full power, approximately 255.    The colors are brightened by a factor of about 2.7, which makes for brighter shades of gray, and the range from the darkest color to the brightest color is also expanded by a factor of 2.7, which results in greater contrast.

Using Autocontrast

Using Full Range increases contrast, but the increase in contrast depends on whatever the range may be between lowest and highest value in the data.   That will vary depending upon the specific values in the data, so that if we simply applied Full Range to adjacent sets of data for various regions in Florida we likely would get different relative amounts of contrast change.   We can get a more standardized level of contrast change by asking Manifold to compute for us the standard deviation of values found in the data and to then use the standard deviation to derive how the range boxes should be set.


The Autocontrast choice allows us to fill the range boxes based on the automatically-computed standard deviation of the values in the channel data.  



To use Autocontrast, we right-click into any of the range boxes for the R, G or B outputs, choose Autocontrast in the context menu and then choose the level of contrast desired based on a choice of settings from one standard deviation to three standard deviations.


Choosing 2.0 stdev (medium) in the menu tells Manifold to load the range boxes using minimum and maximum values that span two standard deviations, abbreviated stdev, of pixel data values.  As a rule of thumb using two standard deviations results in a medium contrast and tends to give a good visual effect.  



When we choose 2.0 stdev (medium) Manifold will compute the data values within Channel 0 which fall within 2 standard deviations and will load the range boxes with the lower and upper bounds of that range, in this case a range of values from approximately 1 to 47.  


When we press Update Style, that range of values from about 1 to 47 will be shown as if those values filled the range from 0 to 255.   Any pixel value above 47 will be forced to 255, pure white, and any below will be forced to 0, pure black.   Pixels within two standard deviations (in this case being values from 1 to 47) will be proportionately interpolated into display outputs as if they filled the range from 0 to 255.



That will increase the difference in brightness between the darkest and lightest pixels in that range by over a factor of five, greatly increasing contrast.  Two standard deviations covers most of the pixels so relatively few will be forced to totally white or black, avoiding garish effects, as seen above.


For even higher contrast, we can command use of a smaller range of values from lowest to highest, using a range encompassing only one standard deviation of values instead of a range that extends through two standard deviations.



To do so, we right-click into one of the range cells and then choose 1.0 stdev (high).    The high annotation in parentheses reminds us this will increase contrast to a higher level.



Two standard deviations covered a range of values from about 1 to 47 while one standard deviation includes only those pixel values in a smaller range, approximately between 12 and 35.     This causes the range of pixel values from 12 to 35, approximately 23 values, to be multiplied tenfold to power display output levels from 0 to 255.   Values below 12 are forced to zero, full black, while values above 35 are forced to 255, full white.


We press Update Style to apply the change.



The result is too much contrast with too many pixels forced to white or forced to black.


We can look at the other extreme of Autocontrast choices by choosing three standard deviations for the range of values.



To do so, we right-click into one of the range cells and then choose 3.0 stdev (low).    The low annotation in parentheses reminds us this will decrease contrast to a lower level.



Choosing three standard deviations chooses a range from approximately -11 to 58, in round numbers.   The low bound is a negative number even though no negative numbers occur in the actual data because a standard deviation calculation using the distribution of actual values from 0 to 92 in the data weights the lower end of the range so much that a bracket of values computed for a width of three standard deviations extends at the lower end into negative numbers.  



The arithmetic result of interpolating a range of -11 to 58 that might appear in the channel into a display output range of 0 to 255 says, roughly, to take the 69 steps between -11 and 58 and to spread them into the 255 steps of the display output range.   Each step in the range from -11 to 58 thus occupies approximately 3.7 steps in the range of display outputs.   The actual lowest value in the data set, 0, thus is 11 steps into the three standard deviation range, so it gets placed about 11 * 3.7 or, roughly, at level 41 in the display output range.     


The lowest values in the actual data are thus painted using grayscale values of 41, a much lighter gray than the black color a data value of 0 would create if painted literally as a value of 0.  The highest values in the data range, above 58, are all forced to white, with a grayscale value of 255.  Because three standard deviations cover almost all of the pixels, there are relatively few pixels that are forced to white.   Most of the pixels are spread out with a multiplying effect of approximately 3.7, which is a lower range of contrast than the factor of approximately 10 spread that using one standard deviation put in play.  We see visibly less contrast with three standard deviations than with one standard deviation.

Using a Single Channel as a Palette

We can follow the procedure in the Example: Autocontrast and Hill Shading Images using Style topic to switch to using Channel 0 as a single number index that drives selection of colors from a palette.



In the illustration above, instead of the (use RGBA channels) choice in the source box we have selected the Channel 0 choice.  We have then specified 2 breaks, pressed the Tally button and then manually changed the values for the intervals to -10 and 58.  We knew to use values like those, a spread of approximately three standard deviations, based on the values we observed during our tinkering with Autocontrast settings earlier in this topic.   We change the color of the lower color box to white, we use a Fill method of interpolate, and then we press the Update Style button.



The result is a reasonably light display with contrast much better than the original.  



We click on the Options tab, check the use shading box and then we press Update Style to apply hill shading.   See Style: Hill Shading topic.



The result is a highly legible display showing the higher ground along the line of the Lake Wales Ridge.  



Since we are now using a single channel to drive a palette image display, we can follow the procedure in the Example: Autocontrast and Hill Shading Images using Style topic to apply colorful palettes, to show the display in false color that brings out whatever details of terrain elevation we want to emphasize.


The illustration above uses a reversed CB Spectral palette with 12 breaks, as seen in the Style pane below.



We can change palettes instantly to create a variety of displays, seen below zoomed into the Sebring, Florida, region around Lake Jackson.   All four illustrations above use the original range from 0 to 92 that is tallied into twelve Breaks and then colored with various palettes. We use color palettes and shading to provide visual distinctions that would be accomplished with greater contrast in a grayscale image that was not shaded.  


For example, the first two illustrations use the CB Spectral and the CB Accent palettes, both reversed.



The first three illustrations use a Fill rule of interpolation for smooth color progressions between different elevations.   The lower left image uses a modified CB Paired palette as discussed in the Notes below.   The lower right illustration uses a Fill rule of closest higher value to force pixels to be colored at the exact boundaries between ranges, giving the effect where each contour zone is colored differently.


This changes nothing - Not a line from a second-rate movie, but a reminder that we can do all the tinkering we like with settings in the Style pane without changing the data at all.   When using the (use RGBA channels) option, we could right-click into range boxes and choose Full Range,


Lake Wales Ridge - The data used shows the Lake Wales Ridge of Florida, which at a maximum height of 92 meters is one of the highest locations in central Florida.    The round lake seen near the center of the last illustration above is Lake Jackson, near Sebring, Florida.


Ethereal Coloring - Using a good palette, such as the adjusted palette in the lower left image in the set of four above, can bring out eye-popping detail.



The palette used is the CB Paired palette with the selected color intervals seen below reversed from the usual order.   The StylePixel property for the palette is:


{ "Channel": 0, "Fill": "boundaverage", "Shade": "compatible", "ShadeScaleZ": 1, "ShadeSun": 315, "StyleShadeSunAltitude": 45, "Value": 10931939, "Values": { "0.292986661195755": 10931939, "16.913": 16744192, "25.223": 16629615, "33.534": 14883356, "41.844": 16489113, "50.154": 3383340, "58.465": 11722634, "66.775": 13284054, "75.08500000000001": 6962586, "8.603": 2062516, "83.396": 16777113, "91.70643615722656": 11622696 } }



The result provides an ethereal effect, as well as highlighting slight differences in elevation that otherwise would be difficult to discern.  Click on the large thumbnail above to download a full resolution image in a new browser window.   The terrain data set is remarkably detailed for such a small amount of data.



The lower right illustration in the set of four uses a modified CB Accent palette, where several subset rows of colors were selected and then reversed to create the palette seen above.   The StylePixel property for the palette is:


{ "Channel": 0, "Fill": "boundmax", "Shade": "compatible", "ShadeScaleZ": 1, "ShadeSun": 315, "StyleShadeSunAltitude": 45, "Value": 12496596, "Values": { "0.292986661195755": 12496596, "16.913": 8374655, "25.223": 16629894, "33.534": 3697840, "41.844": 16777113, "50.154": 16711680, "58.465": 9713560, "66.775": 15729279, "75.08500000000001": 12540695, "8.603": 10468522, "83.396": 9658687, "91.70643615722656": 6710886 } }


Fill methods can have a big effect:



The illustration at above left uses closest higher value while the illustration at above right uses closest lower value, with exactly the same Style settings otherwise.



The illustration above uses interpolation as the Fill method.


Download the data - A small project containing the Florida data used in this example, both original and also colored by modified palettes as in the third and fourth illustrations above, may be downloaded from the Downloads page as an .MXB file.   Get the Florida_palette_hillshading_example.mxb file.   Open it in the free Viewer if you do not yet have Release 9.


See Also







Style: Channels and Outputs Tutorial


Style: Hill Shading


Style: Invisible Pixels


Style: Contouring using Colors


Style: Resample


Style: Palettes


About Images,


Images and Channels,


Palette Images


Example: Change the Contrast of an Image - In this example we use the Style pane to change the contrast of an image.


Example: Using the Assign Channels Button - The Assign Channels button in the Style pane for images allows us to assign channels to the standard three Red, Green, and Blue display outputs using frequently-desired arrangements.   The button provides a short cut way to assign all channels at once instead of doing each channel individually.


Example: Assign Channels - How to use the Style pane for images to assign channels to display outputs such as R, G, B or A.  This topic shows examples of channel combinations and the visual results.


Example: Display an NAIP Four Band Image as Color Infrared (CIR) - How to use the Style pane for images to re-assign channels in a four band NAIP image to produce a Color Infrared (CIR) image display.


Example: Set Image Transparency using Alpha - The A row in the Style pane allows us to specify what transparency we want to apply to the image, either by applying the same value for A for all pixels or by using one of the other channels to also control the A value.


Example: Autocontrast and Hill Shading Images using Style - This example shows how the Style pane can hill shade an image using the values of pixels as heights and generating shadows as if the Sun were located at the specified azimuth and altitude.   This capability is used most frequently with raster images to give an impression of three dimensionality in cases where the values of pixels represent terrain elevations.


Example: Style Applied to an Image Server Image - Because the Style pane simply changes the way an image is displayed and not the data, it can operate on read-only data served by various web servers such as WMS REST servers.    In this example we look at every detail of creating a data source using an image server and then manipulating the appearance of the display with Style.  We will connect to a WMS server that provides LiDAR data in various forms, including as terrain elevation.