ios - GPUImage 添加每个 RGB channel 的色调/颜色调整(将红色调整为更粉红色或橙色)

标签 ios objective-c opengl-es gpuimage

尝试调整特定 channel 的色调(或者更具体地说,可能是特定范围的颜色 - 在本例中为红色)时遇到困难。查看色调过滤器,我想也许我可以通过注释掉绿色和蓝色修饰符来达到某个目的,只影响红色 channel 的变化:

 precision highp float;
 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;
 uniform mediump float hueAdjust;
 const highp  vec4  kRGBToYPrime = vec4 (0.299, 0.587, 0.114, 0.0);
 const highp  vec4  kRGBToI     = vec4 (0.595716, -0.274453, -0.321263, 0.0);
 const highp  vec4  kRGBToQ     = vec4 (0.211456, -0.522591, 0.31135, 0.0);

 const highp  vec4  kYIQToR   = vec4 (1.0, 0.9563, 0.6210, 0.0);
 const highp  vec4  kYIQToG   = vec4 (1.0, -0.2721, -0.6474, 0.0);
 const highp  vec4  kYIQToB   = vec4 (1.0, -1.1070, 1.7046, 0.0);

 void main ()
 {
     // Sample the input pixel
     highp vec4 color   = texture2D(inputImageTexture, textureCoordinate);

     // Convert to YIQ
     highp float   YPrime  = dot (color, kRGBToYPrime);
     highp float   I      = dot (color, kRGBToI);
     highp float   Q      = dot (color, kRGBToQ);

     // Calculate the hue and chroma
     highp float   hue     = atan (Q, I);
     highp float   chroma  = sqrt (I * I + Q * Q);

     // Make the user's adjustments
     hue += (-hueAdjust); //why negative rotation?

     // Convert back to YIQ
     Q = chroma * sin (hue);
     I = chroma * cos (hue);

     // Convert back to RGB
     highp vec4    yIQ   = vec4 (YPrime, I, Q, 0.0);
     color.r = dot (yIQ, kYIQToR);
//  -->    color.g = dot (yIQ, kYIQToG); 
//  -->   color.b = dot (yIQ, kYIQToB);

     // Save the result
     gl_FragColor = color;
 }
);

但这只会让照片变成灰色/蓝色和褪色或紫绿色。我在正确的轨道上吗?如果不是,我如何修改此过滤器以影响单个 channel ,同时保持其他 channel 不变?

一些例子:

原创,以及我想要达到的效果:

(第二张图片几乎没有明显的不同,但是红色 channel 的色调稍微更粉一些。我需要能够在粉红色<->橙色之间进行调整)。

但这是我得到的 B 和 G 注释掉的结果:

(左侧:<0º,右侧:>0º)

在我看来,它并没有像我希望的那样影响红色的色调;可能我没有正确处理这个问题,或者如果我走对了路,这段代码没有正确调整红色 channel 色调?

(我也尝试使用 GPUImageColorMatrixFilter 实现这种效果,但我并没有取得太大进展)。

编辑:这是我当前使用@VB_overflow 的代码 + GPUImage 包装器对着色器进行的迭代,它在功能上以类似于我的目标的方式影响输入图像:

#import "GPUImageSkinToneFilter.h"

@implementation GPUImageSkinToneFilter

NSString *const kGPUImageSkinToneFragmentShaderString = SHADER_STRING
(
 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;

 // [-1;1] <=> [pink;orange]
 uniform highp float skinToneAdjust; // will make reds more pink

 // Other parameters
 uniform mediump float skinHue;
 uniform mediump float skinHueThreshold;
 uniform mediump float maxHueShift;
 uniform mediump float maxSaturationShift;

 // RGB <-> HSV conversion, thanks to http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
 highp vec3 rgb2hsv(highp vec3 c)
{
    highp vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    highp vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    highp vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    highp float d = q.x - min(q.w, q.y);
    highp float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

 // HSV <-> RGB conversion, thanks to http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
 highp vec3 hsv2rgb(highp vec3 c)
{
    highp vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    highp vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

 // Main
 void main ()
{

    // Sample the input pixel
    highp vec4 colorRGB = texture2D(inputImageTexture, textureCoordinate);

    // Convert color to HSV, extract hue
    highp vec3 colorHSV = rgb2hsv(colorRGB.rgb);
    highp float hue = colorHSV.x;

    // check how far from skin hue
    highp float dist = hue - skinHue;
    if (dist > 0.5)
        dist -= 1.0;
    if (dist < -0.5)
        dist += 1.0;
    dist = abs(dist)/0.5; // normalized to [0,1]

    // Apply Gaussian like filter
    highp float weight = exp(-dist*dist*skinHueThreshold);
    weight = clamp(weight, 0.0, 1.0);

    // We want more orange, so increase saturation
    if (skinToneAdjust > 0.0)
        colorHSV.y += skinToneAdjust * weight * maxSaturationShift;
    // we want more pinks, so decrease hue
    else
        colorHSV.x += skinToneAdjust * weight * maxHueShift;

    // final color
    highp vec3 finalColorRGB = hsv2rgb(colorHSV.rgb);

    // display
    gl_FragColor = vec4(finalColorRGB, 1.0);
}
);

#pragma mark -
#pragma mark Initialization and teardown
@synthesize skinToneAdjust;
@synthesize skinHue;
@synthesize skinHueThreshold;
@synthesize maxHueShift;
@synthesize maxSaturationShift;

- (id)init
{
    if(! (self = [super initWithFragmentShaderFromString:kGPUImageSkinToneFragmentShaderString]) )
    {
        return nil;
    }

    skinToneAdjustUniform = [filterProgram uniformIndex:@"skinToneAdjust"];
    skinHueUniform = [filterProgram uniformIndex:@"skinHue"];
    skinHueThresholdUniform = [filterProgram uniformIndex:@"skinHueThreshold"];
    maxHueShiftUniform = [filterProgram uniformIndex:@"maxHueShift"];
    maxSaturationShiftUniform = [filterProgram uniformIndex:@"maxSaturationShift"];

    self.skinHue = 0.05;
    self.skinHueThreshold = 50.0;
    self.maxHueShift = 0.14;
    self.maxSaturationShift = 0.25;

    return self;
}

#pragma mark -
#pragma mark Accessors

- (void)setSkinToneAdjust:(CGFloat)newValue
{
    skinToneAdjust = newValue;
    [self setFloat:newValue forUniform:skinToneAdjustUniform program:filterProgram];
}

- (void)setSkinHue:(CGFloat)newValue
{
    skinHue = newValue;
    [self setFloat:newValue forUniform:skinHueUniform program:filterProgram];
}

- (void)setSkinHueThreshold:(CGFloat)newValue
{
    skinHueThreshold = newValue;
    [self setFloat:newValue forUniform:skinHueThresholdUniform program:filterProgram];
}

- (void)setMaxHueShift:(CGFloat)newValue
{
    maxHueShift = newValue;
    [self setFloat:newValue forUniform:maxHueShiftUniform program:filterProgram];
}

- (void)setMaxSaturationShift:(CGFloat)newValue
{
    maxSaturationShift = newValue;
    [self setFloat:newValue forUniform:maxSaturationShiftUniform program:filterProgram];
}

@end

最佳答案

我在 ShaderToy 上做了一个例子.使用最新的 Chrome 来查看它,在我这边,它在 Firefox 或 IE 上不起作用,因为它使用视频作为输入。

经过一些实验后,在我看来,要使红色色调更“粉红色”,您需要降低色调,但要获得更多“橙色”,则需要增加饱和度。

在代码中,我转换为 HSV 而不是 YIQ,因为这样更快,可以调整饱和度并且仍然允许调整色调。而且HSV分量在[0-1]区间内,所以不需要处理弧度。

所以这是如何完成的:

  1. 您选择引用色调或颜色(在您的情况下为红色调)
  2. 着色器计算从当前像素色调到引用色调的“距离”
  3. 根据这个距离,想要粉色就调低色相,想要橙色就调高饱和度
  4. 重要的是要注意色调的行为不同于饱和度和明度:它应该被视为一个角度(更多信息 here)。

引用色调应该是硬编码的,由用户选择(通过颜色选择图像),或者通过分析图像内容找到。

计算距离的方法有很多种,在这个例子中我选择使用色调之间的角度距离。

您还需要在计算距离后应用某种过滤以仅“选择”最接近的颜色,像这样 gaussian like function .

这是代码,没有 ShaderToy 的东西:

precision highp float;

// [-1;1] <=> [pink;orange]
const float EFFECT_AMOUNT = -0.25; // will make reds more pink

// Other parameters 
const float SKIN_HUE = 0.05;
const float SKIN_HUE_TOLERANCE = 50.0;    
const float MAX_HUE_SHIFT = 0.04;
const float MAX_SATURATION_SHIFT = 0.25;

// RGB <-> HSV conversion, thanks to http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
vec3 rgb2hsv(vec3 c)
{
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

// HSV <-> RGB conversion, thanks to http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
vec3 hsv2rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

// Main
void main ()
{   
    // Sample the input pixel
    vec4 colorRGB = texture2D(inputImageTexture, textureCoordinate);

    // get effect amount to apply
    float skin_tone_shift = EFFECT_AMOUNT;

    // Convert color to HSV, extract hue
    vec3 colorHSV = rgb2hsv(colorRGB.rgb);  
    float hue = colorHSV.x;

    // check how far from skin hue
    float dist = hue - SKIN_HUE;        
    if (dist > 0.5)
        dist -= 1.0;
    if (dist < -0.5)
        dist += 1.0;
    dist = abs(dist)/0.5; // normalized to [0,1]

    // Apply Gaussian like filter
    float weight = exp(-dist*dist*SKIN_HUE_TOLERANCE);  
    weight = clamp(weight, 0.0, 1.0);

    // We want more orange, so increase saturation
    if (skin_tone_shift > 0.0)
        colorHSV.y += skin_tone_shift * weight * MAX_SATURATION_SHIFT;
    // we want more pinks, so decrease hue
    else
        colorHSV.x += skin_tone_shift * weight * MAX_HUE_SHIFT;

    // final color
    vec3 finalColorRGB = hsv2rgb(colorHSV.rgb);     

    // display
     gl_FragColor = vec4(finalColorRGB, 1.0);
}

更多橙色:

enter image description here

更多粉红色: enter image description here

--编辑--

在我看来,您没有在 ObjectiveC 代码中设置统一值。如果您忘记了所有这些,此着色器将为零。

代码应该是这样的:

- (id)init
{
    if(! (self = [super initWithFragmentShaderFromString:kGPUImageSkinToneFragmentShaderString]) )
    {
        return nil;
    }

    skinToneAdjustUniform = [filterProgram uniformIndex:@"skinToneAdjust"];
    [self setFloat:0.5 forUniform:skinToneAdjustUniform program:filterProgram]; // here 0.5 so should increase saturation

    skinHueUniform = [filterProgram uniformIndex:@"skinHue"];
    self.skinHue = 0.05;
    [self setFloat:self.skinHue forUniform:skinHueUniform program:filterProgram];

    skinHueToleranceUniform = [filterProgram uniformIndex:@"skinHueTolerance"];
    self.skinHueTolerance = 50.0;
    [self setFloat:self.skinHueTolerance forUniform:skinHueToleranceUniform program:filterProgram];

    maxHueShiftUniform = [filterProgram uniformIndex:@"maxHueShift"];
    self.maxHueShift = 0.04;
    [self setFloat:self.maxHueShift forUniform:maxHueShiftUniform program:filterProgram];

    maxSaturationShiftUniform = [filterProgram uniformIndex:@"maxSaturationShift"];    
    self.maxSaturationShift = 0.25;        
    [self setFloat:self.maxSaturationShift forUniform:maxSaturationShiftUniform program:filterProgram];

    return self;
}
@end

关于ios - GPUImage 添加每个 RGB channel 的色调/颜色调整(将红色调整为更粉红色或橙色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080747/

相关文章:

ios - 如何在子类中设置只读属性?

objective-c - -[Foo类]:在[super dealloc]上发送到已释放实例的消息

javascript - Three.js 的累积着色器

android - 如何在运行时检测 OpenGl 版本。安卓

ios - 快速获取 UIPickerView 中的字符串

iOS navite 框架 View 推送不适用于 react-native 应用程序

ios - IOS 中的 AdjustPan 模拟和 UITextField 的获取/丢失焦点

opengl-es - Texture2D 与安卓手机上的计算着色器不兼容?

ios- "swapping"UIViews 可能吗?

ios - 如何从特定轨道开始播放专辑?