Title: Perform binary contrast enhancement interactively in C#
The example Perform binary contrast enhancement on an image in C# performs binary contrast enhancement by setting each pixel to black or white depending on whether the sum of its red, green, and blue component values is greater than 3 * 128 = 384. For images that are particularly dark or light, however, the result may be too dark or too light.
This example lets you use a scroll bar to determine what cutoff value to use in performing the adjustment. Whenever the program loads a new image or you adjust the scroll bar, the code calls the following PerformEnhancement method.
// Perform binary contrast enhancement.
private void PerformContrastEnhancement()
if (picOriginal.Image == null) return;
Cursor = Cursors.WaitCursor;
// Perform contrast enhancement.
Bitmap bm = new Bitmap(picOriginal.Image);
BinaryContrast(bm, 3 * hscrCutoff.Value);
// Display the result.
picOriginal.Visible = true;
if (picResult.Image != null) picResult.Image.Dispose();
picResult.Image = bm;
picResult.Left = picOriginal.Right + 4;
picResult.Visible = true;
Cursor = Cursors.Default;
The code creates a new Bitmap that is a copy of the original image. It then calls the BinaryContrast method (see the earlier post for a description of that method) to perform the binary contrast enhancement, passing it the value selected by the scroll bar. It then displays the result.
Download the example to experiment with it and to see additional details.