图像亮度调整

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

测试代码同上

转载于:https://www.cnblogs.com/bigdream6/p/8378343.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值