Level adjustment 2015-04-19

Level adjustment transforms the input to a new output as specified by minimum, mid-point and maximum tones. Inputs below the minimum are transformed to 0, those above the maximum to 255 and those inbetween are gamma adjusted depending on the mid-point, gamma = 2 * mid_point / (max - min).

Level adjustment allows the tone of the image to be improved, by utilising the colour range more efficiently. This is why the colour histograms are displayed, as it helps guide level adjustment. For example an image with a histogram that has no values around 0 brightness (left of the histogram), should be adjusted with a higher minimum point. This will stretch the tones over a greater range of brightness by utilising the range below the minimum point thereby, increasing the contrast and improving the visibility. Equally decreasing the maximum point for an image lacking bright pixels increases the contrast by utilising the range above hte maximum point.

The tones inbetween the maximum and minimum points are gamma adjusted between the minimum and maximum points, with the gamma value set by the mid point. Adjusting the mid point will enhance the contrast of brighter tones if moved to the left at the expense of decreased contrast of the darker tones. Therefore the mid point is best moved towards sparsely populated histogram regions.

Level adjustment is coded as below. (See the stet image representation if this isn't clear).

  
function adjust(input)
{
  if(input < min)
    return 0;
  else if(input > max)
    return 255;
  else
    return ((input - min) / (max - min)) ^ gamma;
}

for(var byte in image)
{
  image[byte] = adjust(image[byte]);
  image[byte + 1] = adjust(image[byte + 1]);
  image[byte + 2] = adjust(image[byte + 2]);
}
  
Back to blog