# Algorithms for Adjusting Brightness and Contrast of an Image

A variety of simple graphical filters are available to camera phone users to enhance their photos on the fly, these filters often stylize, saturate or age a photo. With the enormous growth of social media (uploading filtered images), image processing has also increased.
Let’s see two of the basic image processing algorithms:- brightness adjustment and contrast adjustment.

Brightness
Brightness is a relative term defined as the intensity of a pixel relative to another pixel.
To change the brightness of an image we need to first load an image, then change all the pixel values.
To increase the brightness we need to increase the intensity of each pixel by a constant and similarly to darken the image we need to decrease the intensity of every pixel of the image.

The pseudo code for changing the brightness is as follow

colour = GetPixelColour(x, y)

newRed = Truncate(Red(colour)   + brightness)

newGreen = Truncate(Green(colour) + brightness)

newBlue = Truncate(Blue(colour)  + brightness)

PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)

Truncate is used to keep the value in the valid range i.e. 0 to +255. If the value goes below 0 it will truncate it to zero and if the value goes above 255 it will truncate it to 255.
For example : (-10) will be truncated to 0 and 270 will be truncated to 255.

Here is the procedure Truncate() in pseudo
code:

Procedure Truncate(value)

If value < 0 then value = 0

If value > 255 then value = 255

Return value

EndProcedure

The value of brightness will usually be in the range of -255 to +255 for a 24 bit palette. Negative values will darken the image and, conversely, positive values will brighten the image.

Contrast
Contrast can simply be explained as the difference between maximum and minimum pixel intensity in an image.
To change the contrast of an image we just need to change the value of the max and min intensity pixel.
But if we are just changing the value of two pixels there won’t be any difference in the two images.

So the first step is to calculate a contrast correction factor which is given by the following formula:

F = 259*(255+C)/255*(259-C)

In order for the algorithm to function correctly the value for the contrast correction factor (F) needs to be stored as a floating point number and not as an integer. The value C in the formula denotes the desired level of contrast.

Now we perform the actual contrast adjustment. The adjustment for red colour us as below:

R’ = F(R-128) +128

The above two formulas can be translated in pseudo code as

factor = (259 * (contrast + 255)) / (255 * (259 – contrast))

colour = GetPixelColour(x, y)

newRed   = Truncate(factor * (Red(colour)   – 128) + 128)

newGreen = Truncate(factor * (Green(colour) – 128) + 128)

newBlue  = Truncate(factor * (Blue(colour)  – 128) + 128)

PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)

Truncate is used to keep the value in the valid range i.e. 0 to +255
The pseudo code for it was explained above.

The value of contrast will be in the range of -255 to +255. Negative values will decrease the amount of contrast and, conversely, positive values will increase the amount of contrast.

REFERENCES:-

Image Processing Algorithms

https://www.geeksforgeeks.org/opencv-understanding-brightness-in-an-image/

https://www.tutorialspoint.com/dip/brightness_and_contrast.htm