zoukankan      html  css  js  c++  java
  • 图像亮度调整

    在RGB色彩空间进行亮度图像亮度调整的方法步骤:

    1)计算像素在R、G、B三个分量上的平均值

    2)对三个平均值分别乘以对应的亮度系数brightness,默认为1则表示亮度不变,大于1 表示亮度提高,小于1 表示亮度变暗

    3)对每个像素值在R、G、B上的分量,首先减去第一步计算出来的平均值,然后再加上第二步的计算结果。

    Pnew = Pold +(brightness -1 )*means

    Pnew 处理之后的像素,Pold 处理之前的像素,brightness 亮度系数(取值范围为【0~3】),means图像像素的平均值

    代码如下:

    package chapter4;

    import java.awt.image.BufferedImage;

    /**
    * Created by LENOVO on 18-1-29.
    */
    public class BrightFilter extends AbstractBufferedImageOp {
    private float brightness = 1.2f;//定义亮度系数
    public BrightFilter(){
    //this(1.2f);
    }
    public BrightFilter(float brightness){
    this.brightness = brightness;
    }
    public float getBrightness() {
    return brightness;
    }

    public void setBrightness(float brightness) {
    this.brightness = brightness;
    }
    public BufferedImage filter(BufferedImage src,BufferedImage dest){
    int width = src.getWidth();
    int height = src.getHeight();
    if(dest == null){
    dest = creatCompatibleDestImage(src,null);
    }
    int[] inpixels = new int[width*height];
    int[] outpixels = new int[width*height];
    getRGB(src,0,0,width,height,inpixels);

    int index = 0;
    int[] rgbmeans = new int[3];
    double redSum = 0;double greenSum = 0;double blueSum = 0;
    double total = width*height;
    for(int row=0;row<height;row++){
    int ta = 0,tr = 0,tg = 0,tb = 0;
    for(int col=0;col<width;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;
    }
    }
    //1、计算RGB各分量平均值
    rgbmeans[0] = (int)(redSum/total);
    rgbmeans[1] = (int)(greenSum/total);
    rgbmeans[2] = (int)(blueSum/total);

    for(int row=0;row<height;row++){
    int ta = 0,tr = 0,tg = 0,tb = 0;
    for(int col=0;col<width;col++){
    index = row*width+col;
    ta = (inpixels[index] >> 24) & 0xff;
    tr = (inpixels[index] >> 16) & 0xff;
    tg = (inpixels[index] >> 8) & 0xff;
    tb = inpixels[index] & 0xff;

    //2、减去平均值
    tr -= rgbmeans[0];
    tg -= rgbmeans[1];
    tb -= rgbmeans[2];

    //3、加上平均值乘以亮度系数的值
    tr += rgbmeans[0]*brightness;
    tg += rgbmeans[1]*brightness;
    tb += rgbmeans[2]*brightness;

    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);
    }
    }

    测试代码同上

  • 相关阅读:
    P2762 [网络流24题]太空飞行计划问题(最小割)
    poj2987 Firing[最小割]
    P2051 [AHOI2009]中国象棋[线性DP]
    poj1637 Sightseeing tour[最大流+欧拉回路]
    hdu3739 Anti LIS[最小割]
    P2766 [网络流24题]最长不下降子序列问题
    P2764 [网络流24题]最小路径覆盖问题[最大流]
    P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]
    BZOJ4278 [ONTAK2015]Tasowanie[后缀数组+贪心]
    Robot framework之元素定位实战
  • 原文地址:https://www.cnblogs.com/bigdream6/p/8378343.html
Copyright © 2011-2022 走看看