Tuesday, May 22, 2012

Brightness Changing Algorithm - Image Processing Basic

There are many libraries which are used to Image processing applications. But the algorithms behind these libraries use basic mathematical operation. I hope to bring you series of blog posts which describe the fundamental image processing algorithms implemented in mathematical operation without using any library. Following java class change the brightness of an image.
We should input our image to the method changeBrightness as a BufferedImage. increasingFactor is an integer which determine the brightness level of the image. Positive values will increase the brightness while negative values decrees. Range of the increasingFactor should be between -255 to 255.

Original Image
Brightness Increased
Brightness Decreased

package lakj.comspace.imageprocessor;
import java.awt.Color;
import java.awt.image.BufferedImage;
/**
*Brightness Changing Class
*by
*Lak J Comspace
*
*/
public class Brightness {
public BufferedImage changeBrightness(BufferedImage inImage, int increasingFactor) {
//size of input image
int w = inImage.getWidth();
int h = inImage.getHeight();
BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
//Pixel by pixel navigation loop
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
//get the RGB component of input imge pixel
Color color = new Color(inImage.getRGB(i, j));
int r, g, b;
//change the value of each component
r = color.getRed() + increasingFactor;
g = color.getGreen() + increasingFactor;
b = color.getBlue() + increasingFactor;
//r,g,b values which are out of the range 0 to 255 should set to 0 or 255
if (r >= 256) {
r = 255;
} else if (r < 0) {
r = 0;
}
if (g >= 256) {
g = 255;
} else if (g < 0) {
g = 0;
}
if (b >= 256) {
b = 255;
} else if (b < 0) {
b = 0;
}
//set output image pixel component
outImage.setRGB(i, j, new Color(r, g, b).getRGB());
}
}
return outImage;
}
}
view raw Brightness.java hosted with ❤ by GitHub