[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Perform binary contrast enhancement on an image in C#

[Perform binary contrast enhancement on an image in C#]

In binary contrast enhancement, you change every pixel in an image to either black or white depending on whether it is brighter than some cutoff value. This example takes a simple approach and makes a pixel white if the sum of its red, green, and blue components is greater than 384 (3 * 128).

This example uses the following BinaryContrast method to perform binary contrast enhancement on an image.

// Perform binary contrast enhancement on the bitmap. private void BinaryContrast(Bitmap bm, int cutoff) { for (int y = 0; y < bm.Height; y++) { for (int x = 0; x < bm.Width; x++) { Color clr = bm.GetPixel(x, y); if (clr.R + clr.G + clr.B > cutoff) bm.SetPixel(x, y, Color.White); else bm.SetPixel(x, y, Color.Black); } } }

The code simply loops over each pixel, adds its red, green, and blue components, and makes the pixel black or white depending on whether the sum is greater than the cutoff value. The calling code sets cutoff to 3 * 128.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.