修改RGB图像亮度的算法?

时间:2012-06-22 20:23:12

标签: image-processing rgb

我知道有RGB的公式 - >亮度,但我需要一个亮度参数来修改图像的RGB值。我该怎么做?

由于

7 个答案:

答案 0 :(得分:17)

最简单的方法是将R,G,B值中的每一个乘以某个常数 - 如果常数> 1则会使其更亮,如果< 1则更暗。如果你使它更亮,那么你必须测试每个值,以确保它不超过最大值(通常是255)。

这不仅比从RGB到HSL的转换更简单,而且更接近,但它更接近于在物理对象上照射不同光量时发生的情况。

答案 1 :(得分:16)

从RGB到HSL(色调/饱和度/亮度)的映射,保持色调和饱和度相同,只需修改亮度,然后执行从HSL到RGB的反向映射。

You can read more about the RGB to HSL and HSL to RGB transformations here.

答案 2 :(得分:4)

我的建议与ChrisA.的答案相同,但有一点不同:

使用HSP color space代替,因为它是Photoshop算法的近似值,效果更佳。

为了不仅仅链接到HSP的网站(坦率地应该绰绰有余;只是我不想在没有示例的情况下回答),这是我的C#实现,它遵循网站的:

#region Definitions
//Perceived brightness to Red ratio.
private const double Pr = .299;
//Perceived brightness to Green ratio.
private const double Pg = .587;
//Perceived brightness to Blue ratio.
private const double Pb = .114;
#endregion

//Expected ranges: Hue = 0-359... Other values = 0-1
public static ColorRGB ToRGB(double hue, double saturation, double perceivedBrightness, double alpha) {
    //Check values within expected range
    hue = hue < 0 ? 0 : hue > 359 ? 359 : hue;
    saturation = saturation < 0 ? 0 : saturation > 1 ? 1 : saturation;
    perceivedBrightness = perceivedBrightness < 0 ? 0 : perceivedBrightness > 1 ? 1 : perceivedBrightness;
    alpha = alpha < 0 ? 0 : alpha > 1 ? 1 : alpha;
    //Conversion
    var minOverMax = 1 - saturation;
    double r, g, b;
    if (minOverMax > 0) {
        double part;
        if (hue < 0.166666666666667D) { //R>G>B
            hue = 6 * (hue - 0); part = 1 + hue * (1 / minOverMax - 1);
            b = perceivedBrightness / Math.Sqrt(Pr / minOverMax / minOverMax + Pg * part * part + Pb);
            r = b / minOverMax; g = b + hue * (r - b);
        }
        else if (hue < 0.333333333333333D) { //G>R>B
            hue = 6 * (-hue + 0.333333333333333D); part = 1 + hue * (1 / minOverMax - 1);
            b = perceivedBrightness / Math.Sqrt(Pg / minOverMax / minOverMax + Pr * part * part + Pb);
            g = b / minOverMax; r = b + hue * (g - b);
        }
        else if (hue < 0.5D) {   //  G>B>R
            hue = 6 * (hue - 0.333333333333333D); part = 1 + hue * (1 / minOverMax - 1);
            r = perceivedBrightness / Math.Sqrt(Pg / minOverMax / minOverMax + Pb * part * part + Pr);
            g = r / minOverMax; b = r + hue * (g - r);
        }
        else if (hue < 0.666666666666667D) { //B>G>R
            hue = 6 * (-hue + 0.666666666666667D); part = 1 + hue * (1 / minOverMax - 1);
            r = perceivedBrightness / Math.Sqrt(Pb / minOverMax / minOverMax + Pg * part * part + Pr);
            b = r / minOverMax; g = r + hue * (b - r);
        }
        else if (hue < 0.833333333333333D) { //B>R>G
            hue = 6 * (hue - 0.666666666666667D); part = 1 + hue * (1 / minOverMax - 1);
            g = perceivedBrightness / Math.Sqrt(Pb / minOverMax / minOverMax + Pr * part * part + Pg);
            b = g / minOverMax; r = g + hue * (b - g);
        }
        else { //R>B>G
            hue = 6 * (-hue + 1D); part = 1 + hue * (1 / minOverMax - 1);
            g = perceivedBrightness / Math.Sqrt(Pr / minOverMax / minOverMax + Pb * part * part + Pg);
            r = g / minOverMax; b = g + hue * (r - g);
        }
    }
    else {
        if (hue < 0.166666666666667D) { //R>G>B
            hue = 6 * (hue - 0D); r = Math.Sqrt(perceivedBrightness * perceivedBrightness / (Pr + Pg * hue * hue)); g = r * hue; b = 0;
        }
        else if (hue < 0.333333333333333D) { //G>R>B
            hue = 6 * (-hue + 0.333333333333333D); g = Math.Sqrt(perceivedBrightness * perceivedBrightness / (Pg + Pr * hue * hue)); r = g * hue; b = 0;
        }
        else if (hue < 0.5D) { //G>B>R
            hue = 6 * (hue - 0.333333333333333D); g = Math.Sqrt(perceivedBrightness * perceivedBrightness / (Pg + Pb * hue * hue)); b = g * hue; r = 0;
        }
        else if (hue < 0.666666666666667D) { //B>G>R
            hue = 6 * (-hue + 0.666666666666667D); b = Math.Sqrt(perceivedBrightness * perceivedBrightness / (Pb + Pg * hue * hue)); g = b * hue; r = 0;
        }
        else if (hue < 0.833333333333333D) { //B>R>G
            hue = 6 * (hue - 0.666666666666667D); b = Math.Sqrt(perceivedBrightness * perceivedBrightness / (Pb + Pr * hue * hue)); r = b * hue; g = 0;
        }
        else { //R>B>G
            hue = 6 * (-hue + 1D); r = Math.Sqrt(perceivedBrightness * perceivedBrightness / (Pr + Pb * hue * hue)); b = r * hue; g = 0;
        }
    }
    return new ColorRGB(r, g, b, alpha);
}

//Expected ranges: 0-1 on all components
public static ColorHSP FromRGB(double red, double green, double blue, double alpha) {
    //Guarantee RGB values are in the correct ranges
    red = red < 0 ? 0 : red > 1 ? 1 : red;
    green = green < 0 ? 0 : green > 1 ? 1 : green;
    blue = blue < 0 ? 0 : blue > 1 ? 1 : blue;
    alpha = alpha < 0 ? 0 : alpha > 1 ? 1 : alpha;
    //Prepare & cache values for conversion
    var max = MathExtensions.Max(red, green, blue);
    var min = MathExtensions.Min(red, green, blue);
    var delta = max - min;
    double h, s, p = Math.Sqrt(0.299 * red + 0.587 * green + 0.114 * blue);
    //Conversion
    if (delta.Equals(0)) h = 0;
    else if (max.Equals(red)) {
        h = (green - blue) / delta % 6;
    }
    else if (max.Equals(green)) h = (blue - red) / delta + 2;
    else h = (red - green) / delta + 4;
    h *= 60;
    if (h < 0) h += 360;
    if (p.Equals(0))
        s = 0;
    else
        s = delta / p;
    //Result
    return new ColorHSP(h, s, p, alpha);
}

答案 3 :(得分:2)

添加到Mark Ransom的答案:最好将所述因子与255常量一起使用并将其添加到当前颜色值:

float brightnessFac = //between -1.0 and 1.0    
byte brightnessRed = red + (255f * brightnessFac);

如果您只使用介于0.0和1.0之间的因子

byte brightnessRed = red * brightnessFac;

值0保持为零。

答案 4 :(得分:0)

import java.io.*;
import java.awt.Color;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.image.BufferedImage;



    class psp{

public static void main(String a[]){
try{

File input=new File("abc.jpg");
File output=new File("output1.jpg");
        BufferedImage picture1 = ImageIO.read(input);   // original
BufferedImage picture2= new BufferedImage(picture1.getWidth(), picture1.getHeight(),BufferedImage.TYPE_INT_RGB);      
        int width  = picture1.getWidth();
        int height = picture1.getHeight();

int factor=50;
for (int y = 0; y < height ; y++) {//loops for images
for (int x = 0; x < width ; x++) {

Color c=new Color(picture1.getRGB(x,y));
int r=c.getRed()+factor;
int b=c.getBlue()+factor;
int g=c.getGreen()+factor;

if (r >= 256) {
    r = 255;
} else if (r < 0) {
r = 0;
}

if (g >= 256) {
g = 255;
} else if (g < 0) {
g = 0;
}

 if (b >= 256) {
b = 255;
} else if (b < 0) {
b = 0;
 }
picture2.setRGB(x, y,new Color(r,g,b).getRGB());


}
}




ImageIO.write(picture2,"jpg",output);       
}catch(Exception e){
System.out.println(e);
}





}




}

答案 5 :(得分:0)

你可以试试 LookupTable 和 LookupOp;以便您可以通过修改 LookupTable 来调整图片的亮度。 让图片变亮只是给RGB增加一些值。

BufferedImage dstImage = new BufferedImage(input.getWidth(), input.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
        LookupTable lookupTable = new ShortLookupTable(0, data);
        LookupOp op = new LookupOp(lookupTable, null);
        op.filter(toBeTone, dstImage);

答案 6 :(得分:-4)

调整图像的亮度是可以完成的最简单的图像处理操作之一。所涉及的只是为每个红色,绿色和蓝色成分添加所需的亮度变化。

它会是这样的:

colour = GetPixelColour(x, y)
   newRed   = Truncate(Red(colour)   + brightness)
   newGreen = Truncate(Green(colour) + brightness)
   newBlue  = Truncate(Blue(colour)  + brightness)
   PutPixelColour(x, y) = RGB(newRed, newGreen, newBlue)

代码,以确保红色,绿色和蓝色的新值在有效范围内。

Procedure Truncate(value)
      If value < 0 Then value = 0
      If value > 255 Then value = 255
      Return value
   EndProcedure