Adjust brightness and contrast of Image Processing
In many cases, an image is too white to be exposed, or the light is too dark, sometimes the background and image characters
You can adjust the two basic attributes of the image-brightness and contrast to obtain the overall effect.
To get images of higher quality.
Basic Principles:
The brightness of each pixel in an image essentially. The brightness of each pixel is essentially the size of an RGB value, and the RGB value is 0.
If the pixel is black and RGB is 255, the pixel is the brightest and white. Contrast is the difference between different pixels,
The greater the difference, the more obvious the contrast. From the histogram analysis point of view, the better the contrast, the more the histogram Curve
Obviously, the distribution is more even.
Algorithm flow:
The algorithm for adjusting the brightness and contrast of an image consists of the following steps:
1. Calculate the RGB pixel mean of the image-m
2. Remove average-m for each pixel of the image
3. Multiply the pixel P after the average value is removed by the contrast coefficient.
4. Add m to the pixel P after processing on the step multiplied by the brightness System
5. assign a new value to the pixel RGB value.
Algorithm Coefficient
The optimal range of contrast is [0 ~ 4],
The optimal range of brightness is [0 ~ 2]
For the source code of the algorithm, see the final source code section.
Program effect:
The source code of the filter to adjust the brightness and contrast is as follows:
package com.process.blur.study;import java.awt.image.BufferedImage;/** * this filter illustrate the brightness and contrast of the image * and demo how to change the both attribute of the image. * * @author gloomy fish * */public class ConBriFilter extends AbstractBufferedImageOp {private float contrast = 1.5f; // default value;private float brightness = 1.0f; // default value;public ConBriFilter() {// do stuff here if you need......}@Overridepublic BufferedImage filter(BufferedImage src, BufferedImage dest) {int width = src.getWidth(); int height = src.getHeight(); if ( dest == null ) dest = createCompatibleDestImage( src, null ); int[] inPixels = new int[width*height]; int[] outPixels = new int[width*height]; src.getRGB( 0, 0, width, height, inPixels, 0, width ); // calculate RED, GREEN, BLUE means of pixelint index = 0;int[] rgbmeans = new int[3];double redSum = 0, greenSum = 0, blueSum = 0;double total = height * width; for(int row=0; row