修改RGB图像亮度的算法?
image-processing
6
0

我知道有用于RGB->亮度的公式,但是我需要给定亮度参数来修改图像的RGB值。我怎么做?

谢谢

参考资料:
Stack Overflow
收藏
评论
共 4 个回答
高赞 时间 活跃

添加到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保持零。

收藏
评论

我的推荐与ChrisA相同的答案,有一个区别:

请改用HS P颜色空间 ,因为它是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);
}
收藏
评论

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

这不仅比从RGB到HSL并再返回的转换更简单,而且还更接近于当您向物理对象照射不同量的光时发生的情况。

收藏
评论

从RGB映射到HSL(色相/饱和度/亮度),保持色相和饱和度相同,只是修改亮度,然后从HSL到RGB进行向后映射。

您可以在此处阅读有关RGB到HSL以及HSL到RGB转换的更多信息。

收藏
评论
新手导航
  • 社区规范
  • 提出问题
  • 进行投票
  • 个人资料
  • 优化问题
  • 回答问题

关于我们

常见问题

内容许可

联系我们

@2020 AskGo
京ICP备20001863号