C# Tutorial: Contrast Stretching with Normalization
This post is a short revision of Contrast Stretch post we already worked on in the past. Where we talked about histogram equalization, which is a little more complex method than...
Filter by Category
This post is a short revision of Contrast Stretch post we already worked on in the past. Where we talked about histogram equalization, which is a little more complex method than...
What is Log Transformation Anyway? Log transformation in image processing is a part of gray level transformations. It works by transforming each pixel individually. It is usually...
Where Do We Start? There are numerous ways you can flip an image horizontally. It’s pretty basic functionality in image processing. I wanted to challenge myself and not just...
Let’s Get Started A very basic problem that we face in image processing is how to scale images without making them look deformed when we want to make it of an arbitrary...
List of contents: Short intro First Method – Google Opinion Rewards Second Method – Taking & Fortifying Gyms Third Method – Answer a Few Questions List of...
Zero padding an image is useful when we’re convolving it with a filter of certain size. How much padding should we use depends on how big our filter is. What is the purpose...
Gamma correction is a process, which allows you to set the brightness of screen. You can often run into this setting in video games, where you set the brightness of your screen...
A negative image is a complete inversion of an image, we would say to be normal. In other words, dark areas will appear light, and light areas will become dark. More in detail, a...
Gaussian blur is an image processing operation, that reduces noise in images. It does so by a convolution process, using a matrix that contains values calculated by a Gaussian...
Erosion is a morphological process, where pixels at an object boundaries are removed. Erosion and dilation are a pair of basic morphological transformations, which are completely...
This post is a short revision of Contrast Stretch post we already worked on in the past. Where we talked about histogram equalization, which is a little more complex method than normalization.
Normalization in image processing is a process that changes the ranges of intensities in an image. For example let’s say we have a grayscale image which has the lowest intensity value of 50 and the highest the value of 120.
This image appears to be very “washed out”. So to enhance it, we will use normalization technique that will change the intensities’ range from 50 – 120 to 0 – 255.
Normalization takes each pixel and recalculates a new intensity value individually.
Pout = (Pin – c) * (b – a) / (d – c) + a
Where Pout and Pin are variables for pixel values on the output and input of the equation.
b and a represent values for the new range of intensities we are trying to get (a is the minimum and b is the maximum value).
d and c are the maximum and minimum intensity values of our input image.
As I mentioned before there is already a post on contrast stretching. But I feel the method we are describing here is much easier for those who are still new to image processing.
Although this technique may not be as efficient as the one we already described – histogram equalization, results can still be satisfactory to a certain degree.
public static Bitmap Normalization(this Bitmap img)
{
int w = img.Width;
int h = img.Height;
BitmapData sd = img.LockBits(new Rectangle(0, 0, w, h),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int bytes = sd.Stride * sd.Height;
byte[] buffer = new byte[bytes];
byte[] result = new byte[bytes];
Marshal.Copy(sd.Scan0, buffer, 0, bytes);
img.UnlockBits(sd);
int current = 0;
byte max = 0;
byte min = 255;
for (int i = 0; i < buffer.Length; i++)
{
max = Math.Max(max, buffer[i]);
min = Math.Min(min, buffer[i]);
}
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
current = y * sd.Stride + x * 4;
for (int i = 0; i < 3; i++)
{
result[current + i] = (byte)((buffer[current + i] - min) * 100 / (max - min));
}
result[current + 3] = 255;
}
}
Bitmap resimg = new Bitmap(w, h);
BitmapData rd = resimg.LockBits(new Rectangle(0, 0, w, h),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(result, 0, rd.Scan0, bytes);
resimg.UnlockBits(rd);
return resimg;
}