zoukankan      html  css  js  c++  java
  • 图像处理------调整亮度与对比度 分类: 视频图像处理 2015-07-24 09:51 28人阅读 评论(0) 收藏

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

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

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

     

    基本原理:

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

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

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

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

     

    算法流程:

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

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

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

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

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

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

     

    算法系数

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

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

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

     

    程序效果:

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

    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. }  

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟
    VK Cup 2015
    DP总结 ——QPH
    Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq
    Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
    Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论
    Codeforces Round #311 (Div. 2) C. Arthur and Table Multiset
    Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题
    Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题
    Codeforces Round #260 (Div. 1) D. Serega and Fun 分块
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706368.html
Copyright © 2011-2022 走看看