Image processing (Brightness algorithm)

In the last image processing post I explained about Gray mode algorithm and how to make color of a specific picture gray. This post is about brightness algorithm and demonstrates how to manipulate the brightness of the picture.

Changing brightness of a picture can cause either making picture lighter in terms of color or darker. Thus brightness algorithm should be divided into two sub sections. One section does the task of increasing brightness based on the user selection value and the other one does exactly opposite task.

Now the question is that how brightness of picture can be manipulated. As mentioned before each pixel of an image has three dimensions. First two dedicated to X and Y positions of the pixel respectively and the last dimension holds the color of that pixel. Now, depends on selected programming language value of color can be different. For instance, in some programming languages the color of pixel might be in the range between 0 to 255. In more complex way the color parameter (dimension) is an array with three dimensions and each dimension holds value between 0 to 255. Three dimensions of color array in order represent Red(R), Blue (B), Green (G) colors. Hence, the first dimension of color parameter is red and so on.

In order to change the brightness of the picture we just need to increase or decrease the values of RGB and in addition, we should be careful about overflow or underflow of the each value of RGB array.

The following example demonstrates brightness algorithm clearly with considering about overflow and underflow matter.

 private void brightnessDown(PictureBox picImp)
 {
 int[] RGB = new int[3];
 for (int i = 0; i <= x.Width - 1; i++)
 {
 for (int j = 0; j <= x.Height - 1; j++)
 {
 z = x.GetPixel(i, j);
 RGB[0] = (z.R) - 15;
 RGB[1] = (z.G) - 15;
 RGB[2] = (z.B) - 15;
if (RGB[0] < 0)
 {
RGB[0] = 0;
}
if (RGB[1] < 0)
{
RGB[1] = 0;
}
if (RGB[2] < 0)
{
RGB[2] = 0;
}
y.SetPixel(i, j, Color.FromArgb(RGB[0], RGB[1], RGB[2]));
}
}
Message.setMessage(PicSave.savechange(picImp, y), "Brightness Down");
}
private void brightnessUp(PictureBox picImp)
{
int[] RGB = new int[3];
for (int i = 0; i <= x.Width - 1; i++)
{
for (int j = 0; j <= x.Height - 1; j++)
{
z = x.GetPixel(i, j);
RGB[0] = (z.R) + 15;
RGB[1] = (z.G) + 15;
RGB[2] = (z.B) + 15;
if (RGB[0] > 255)
{
RGB[0] = 255;
}
if (RGB[1] > 255)
{
RGB[1] = 255;
}
if (RGB[2] > 255)
{
RGB[2] = 255;
}
y.SetPixel(i, j, Color.FromArgb(RGB[0], RGB[1], RGB[2]));
}
}
Message.setMessage(PicSave.savechange(picImp, y), "Brightness Up");
}

As you can see in the above code the amount of increasing and decreasing brightness is 15. This value is not constant and can be change by the user taste. As a result, using a seek bar can allow this chance to the user to increase or decrease picture brightness by his need.

For downloading source code of above example please click here.

send your idea and information to kasra.madadipouya@geeksweb.tk

Leave a Reply