Mirror

How to change contrast in a colour image (Views: 100)


Problem/Question/Abstract:

How to change contrast in a color image

Answer:

Changing contrast in a greyscale image is fairly easy. You can use histogram equilization or histogram stretching. Contrast enhancing a color image is a bit tricker since there are three color planes. In some cases you can histostretch each color plane (like HistoStretchGrays above), but usually this will result in an undesirable color shift.

Here's some code for changing contrast in a 256 colour image:

function ContrastLUT(Amount: Integer): array[Byte] of Byte;
var
  i, z: Integer;
begin
  for i := 0 to 126 do
  begin
    z := i - ((Abs(128 - i) * Amount) div 256);
    if z > 255 then
      z := 255
    else if z < 0 then
      z := 0;
    Result[i] := z;
  end;
  for i := 127 to 255 do
  begin
    z := i + ((Abs(128 - i) * Amount) div 256);
    if z > 255 then
      z := 255
    else if z < 0 then
      z := 0;
    Result[i] := z;
  end;
end;

Apply the lookup table to your bitmap bytes to adjust contrast.

<< Back to main page