很多時(shí)候,一張圖像被過(guò)度曝光顯得很白,或者光線不足顯得很暗,有時(shí)候背景跟圖像人物 也觀察不清楚,這個(gè)時(shí)候可以通過(guò)調(diào)節(jié)圖像的兩個(gè)基本屬性-亮度與對(duì)比度來(lái)獲得整體效果 的提升,從而得到質(zhì)量更高的圖片。 基本原理: 圖像亮度本質(zhì)上圖像中每個(gè)像素的亮度,每個(gè)像素的亮度本質(zhì)上RGB值的大小,RGB值為0 是像素點(diǎn)為黑色,RGB都為255時(shí)像素點(diǎn)最亮,為白色。對(duì)比度則是不同像素點(diǎn)之間的差值, 差值越大,對(duì)比度越明顯。從直方圖分析的觀點(diǎn)來(lái)看,對(duì)比度越好的圖片,直方圖曲線會(huì)越 明顯,分布也越顯得均勻。 算法流程: 調(diào)整圖像亮度與對(duì)比度算法主要由以下幾個(gè)步驟組成: 1. 計(jì)算圖像的RGB像素均值– M 2. 對(duì)圖像的每個(gè)像素點(diǎn)Remove平均值-M 3. 對(duì)去掉平均值以后的像素點(diǎn) P乘以對(duì)比度系數(shù) 4. 對(duì)步驟上處理以后的像素P加上 M乘以亮度系統(tǒng) 5. 對(duì)像素點(diǎn)RGB值完成重新賦值 算法系數(shù) 對(duì)比度 contrast的最佳取值范圍在[0 ~ 4], 亮度 brightness的最佳取值范圍在[0~ 2]之間 算法的源程序代碼見(jiàn)最后源代碼部分 程序效果: 調(diào)整亮度與對(duì)比度的濾鏡源代碼如下:
- 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......
- }
-
- @Override
- public 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 pixel
- int index = 0;
- int[] rgbmeans = new int[3];
- double redSum = 0, greenSum = 0, blueSum = 0;
- double total = height * width;
- for(int row=0; row<>
- int ta = 0, tr = 0, tg = 0, tb = 0;
- for(int col=0; col<>
- index = row * width + col;
- ta = (inPixels[index] >> 24) & 0xff;
- tr = (inPixels[index] >> 16) & 0xff;
- tg = (inPixels[index] >> 8) & 0xff;
- tb = inPixels[index] & 0xff;
- redSum += tr;
- greenSum += tg;
- blueSum +=tb;
- }
- }
-
- rgbmeans[0] = (int)(redSum / total);
- rgbmeans[1] = (int)(greenSum / total);
- rgbmeans[2] = (int)(blueSum / total);
-
- // adjust contrast and brightness algorithm, here
- for(int row=0; row<>
- int ta = 0, tr = 0, tg = 0, tb = 0;
- for(int col=0; col<>
- index = row * width + col;
- ta = (inPixels[index] >> 24) & 0xff;
- tr = (inPixels[index] >> 16) & 0xff;
- tg = (inPixels[index] >> 8) & 0xff;
- tb = inPixels[index] & 0xff;
-
- // remove means
- tr -=rgbmeans[0];
- tg -=rgbmeans[1];
- tb -=rgbmeans[2];
-
- // adjust contrast now !!!
- tr = (int)(tr * getContrast());
- tg = (int)(tg * getContrast());
- tb = (int)(tb * getContrast());
-
- // adjust brightness
- tr += (int)(rgbmeans[0] * getBrightness());
- tg += (int)(rgbmeans[1] * getBrightness());
- tb += (int)(rgbmeans[2] * getBrightness());
- outPixels[index] = (ta <>24) | (clamp(tr) <>16) | (clamp(tg) <>8) | clamp(tb);
- }
- }
- setRGB( dest, 0, 0, width, height, outPixels );
- return dest;
- }
-
- public int clamp(int value) {
- return value > 255 ? 255 :(value <>0 ? 0 : value);
- }
-
- public float getContrast() {
- return contrast;
- }
-
- public void setContrast(float contrast) {
- this.contrast = contrast;
- }
-
- public float getBrightness() {
- return brightness;
- }
-
- public void setBrightness(float brightness) {
- this.brightness = brightness;
- }
-
- }
|