码迷,mamicode.com
首页 > 其他好文 > 详细

图像亮度调整

时间:2018-01-29 22:32:48      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:chap   定义   pixel   getwidth   tpi   默认   ack   计算   lamp   

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

测试代码同上

图像亮度调整

标签:chap   定义   pixel   getwidth   tpi   默认   ack   计算   lamp   

原文地址:https://www.cnblogs.com/bigdream6/p/8378343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!