图像处理之调整亮度与对比度

图像处理之调整亮度与对比度

 

很多时候,一张图像被过度曝光显得很白,或者光线不足显得很暗,有时候背景跟图像人物

也观察不清楚,这个时候可以通过调节图像的两个基本属性-亮度与对比度来获得整体效果

的提升,从而得到质量更高的图片。

 

基本原理:

图像亮度本质上图像中每个像素的亮度,每个像素的亮度本质上RGB值的大小,RGB值为0

是像素点为黑色,RGB都为255时像素点最亮,为白色。对比度则是不同像素点之间的差值,

差值越大,对比度越明显。从直方图分析的观点来看,对比度越好的图片,直方图曲线会越

明显,分布也越显得均匀。

 

算法流程:

调整图像亮度与对比度算法主要由以下几个步骤组成:

1.      计算图像的RGB像素均值– M

2.      对图像的每个像素点Remove平均值-M

3.      对去掉平均值以后的像素点 P乘以对比度系数

4.      对步骤上处理以后的像素P加上 M乘以亮度系统

5.      对像素点RGB值完成重新赋值

 

算法系数

对比度 contrast的最佳取值范围在[0 ~ 4],

亮度 brightness的最佳取值范围在[0~ 2]之间

算法的源程序代码见最后源代码部分

 

程序效果:

调整亮度与对比度的滤镜源代码如下:

[java]  view plain  copy
  1. package com.process.blur.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4.   
  5. /** 
  6.  * this filter illustrate the brightness and contrast of the image 
  7.  * and demo how to change the both attribute of the image. 
  8.  *  
  9.  * @author gloomy fish 
  10.  * 
  11.  */  
  12. public class ConBriFilter extends AbstractBufferedImageOp {  
  13.   
  14.     private float contrast = 1.5f; // default value;  
  15.     private float brightness = 1.0f; // default value;  
  16.       
  17.     public ConBriFilter() {  
  18.         // do stuff here if you need......  
  19.     }  
  20.       
  21.     @Override  
  22.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  23.         int width = src.getWidth();  
  24.         int height = src.getHeight();  
  25.   
  26.         if ( dest == null )  
  27.             dest = createCompatibleDestImage( src, null );  
  28.   
  29.         int[] inPixels = new int[width*height];  
  30.         int[] outPixels = new int[width*height];  
  31.         src.getRGB( 00, width, height, inPixels, 0, width );  
  32.           
  33.         // calculate RED, GREEN, BLUE means of pixel  
  34.         int index = 0;  
  35.         int[] rgbmeans = new int[3];  
  36.         double redSum = 0, greenSum = 0, blueSum = 0;  
  37.         double total = height * width;  
  38.         for(int row=0; row<height; row++) {  
  39.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  40.             for(int col=0; col<width; col++) {  
  41.                 index = row * width + col;  
  42.                 ta = (inPixels[index] >> 24) & 0xff;  
  43.                 tr = (inPixels[index] >> 16) & 0xff;  
  44.                 tg = (inPixels[index] >> 8) & 0xff;  
  45.                 tb = inPixels[index] & 0xff;  
  46.                 redSum += tr;  
  47.                 greenSum += tg;  
  48.                 blueSum +=tb;  
  49.             }  
  50.         }  
  51.           
  52.         rgbmeans[0] = (int)(redSum / total);  
  53.         rgbmeans[1] = (int)(greenSum / total);  
  54.         rgbmeans[2] = (int)(blueSum / total);  
  55.           
  56.         // adjust contrast and brightness algorithm, here  
  57.         for(int row=0; row<height; row++) {  
  58.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  59.             for(int col=0; col<width; col++) {  
  60.                 index = row * width + col;  
  61.                 ta = (inPixels[index] >> 24) & 0xff;  
  62.                 tr = (inPixels[index] >> 16) & 0xff;  
  63.                 tg = (inPixels[index] >> 8) & 0xff;  
  64.                 tb = inPixels[index] & 0xff;  
  65.                   
  66.                 // remove means  
  67.                 tr -=rgbmeans[0];  
  68.                 tg -=rgbmeans[1];  
  69.                 tb -=rgbmeans[2];  
  70.                   
  71.                 // adjust contrast now !!!  
  72.                 tr = (int)(tr * getContrast());  
  73.                 tg = (int)(tg * getContrast());  
  74.                 tb = (int)(tb * getContrast());  
  75.                   
  76.                 // adjust brightness  
  77.                 tr += (int)(rgbmeans[0] * getBrightness());  
  78.                 tg += (int)(rgbmeans[1] * getBrightness());  
  79.                 tb += (int)(rgbmeans[2] * getBrightness());  
  80.                 outPixels[index] = (ta << 24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb);  
  81.             }  
  82.         }  
  83.         setRGB( dest, 00, width, height, outPixels );  
  84.         return dest;  
  85.     }  
  86.       
  87.     public int clamp(int value) {  
  88.         return value > 255 ? 255 :(value < 0 ? 0 : value);  
  89.     }  
  90.   
  91.     public float getContrast() {  
  92.         return contrast;  
  93.     }  
  94.   
  95.     public void setContrast(float contrast) {  
  96.         this.contrast = contrast;  
  97.     }  
  98.   
  99.     public float getBrightness() {  
  100.         return brightness;  
  101.     }  
  102.   
  103.     public void setBrightness(float brightness) {  
  104.         this.brightness = brightness;  
  105.     }  
  106.   
  107. }  

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值