Use a scroll bar to change the cutoff value for binary contrast enhancement 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. In the picture, the cutoff value was fairly high so many of the mid-shade pixels in the original image were changed to black.

Whenever the program loads a new image or the user adjusts the scroll bar, the code calls the following PerformEnhancement method.

// Perform the binary contrast enhancement.
private void PerformEnhancement()
{
    // Perform contrast enhancement.
    Bitmap bm = new Bitmap(picOriginal.Image);
    BinaryContrast(bm, scrCutoff.Value);

    // Display the result.
    picOriginal.Visible = true;
    picResult.Image = bm;
    picResult.Left = picOriginal.Right + 4;
    picResult.Visible = true;
    this.ClientSize = new Size(
        picResult.Right + picOriginal.Left,
        picResult.Bottom + picOriginal.Left);
}

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 about that method) to perform the binary contrast enhancement, passing it the value selected by the scroll bar. It then displays the result.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.