0

I want to apply chroma keying shader for my application. For this I used GPUImage plugin. In that I found GPUImageChromaKeyingBlendFilter class to satisfy my needs.

In plugin, GPUImageChromaKeyingBlendFilter class contains wrong program for chroma keying so that one I replaced with iOS version of similar plugin.

The program content represented as below.

public static final String CHROMA_KEY_BLEND_FRAGMENT_SHADER = "precision highp float;\n"
        + "\n"
        + " varying highp vec2 textureCoordinate;\n"
        + " varying highp vec2 textureCoordinate2;\n"
        + "\n"
        + " uniform float thresholdSensitivity;\n"
        + " uniform float smoothing;\n"
        + " uniform vec3 colorToReplace;\n"
        + " uniform sampler2D inputImageTexture;\n"
        + " uniform sampler2D inputImageTexture2;\n"
        + "\n"
        + " void main()\n"
        + " {\n"
        + "     vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);\n"
        + "     vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2);\n"
        + "\n"
        + "     float maskY = 0.2989 * colorToReplace.r + 0.5866 * colorToReplace.g + 0.1145 * colorToReplace.b;\n"
        + "     float maskCr = 0.7132 * (colorToReplace.r - maskY);\n"
        + "     float maskCb = 0.5647 * (colorToReplace.b - maskY);\n"
        + "\n"
        + "     float Y = 0.2989 * textureColor.r + 0.5866 * textureColor.g + 0.1145 * textureColor.b;\n"
        + "     float Cr = 0.7132 * (textureColor.r - Y);\n"
        + "     float Cb = 0.5647 * (textureColor.b - Y);\n"
        + "\n"
        + "     float blendValue = 1.0 - smoothstep(thresholdSensitivity - smoothing, thresholdSensitivity , abs(Cr - maskCr) + abs(Cb - maskCb));\n"
        + "//     float blendValue = smoothstep(thresholdSensitivity, thresholdSensitivity + smoothing, distance(vec2(Cr, Cb), vec2(maskCr, maskCb)));\n"
        + "//     gl_FragColor = mix(textureColor, textureColor2,blendValue);\n"
        + "     vec4 chromaTexture = vec4(textureColor.rgb, textureColor.a *  blendValue);\n"
        + "      gl_FragColor = chromaTexture;\n" + " }\n";

In all other forum post, I found similar program to achieve chroma keying. But this program now working for me. I don't have good knowledge in opengl programs.

So please friends help to come out of this.

EDIT:

private int mThresholdSensitivityLocation;
private int mSmoothingLocation;
private int mColorToReplaceLocation;
private float mSmoothing = 1.0f;
private float mThresholdSensitivity = 0.4f;
private float[] mColorToReplace = new float[] { 0.0f, 1.0f, 0.0f };

public GPUImageChromaKeyBlendFilter() {
    super(CHROMA_KEY_BLEND_FRAGMENT_SHADER);
}

@Override
public void onInit() {
    super.onInit();
    mThresholdSensitivityLocation = GLES20.glGetUniformLocation(
            getProgram(), "thresholdSensitivity");
    mSmoothingLocation = GLES20.glGetUniformLocation(getProgram(),
            "smoothing");
    mColorToReplaceLocation = GLES20.glGetUniformLocation(getProgram(),
            "colorToReplace");
}

@Override
public void onInitialized() {
    super.onInitialized();
    setSmoothing(mSmoothing);
    setThresholdSensitivity(mThresholdSensitivity);
    setColorToReplace(mColorToReplace[0], mColorToReplace[1],
            mColorToReplace[2]);
}

public void setSmoothing(final float smoothing) {
    mSmoothing = smoothing;
    setFloat(mSmoothingLocation, mSmoothing);

}

/**
 * The threshold sensitivity controls how similar pixels need to be colored
 * to be replaced The default value is 0.3
 */
public void setThresholdSensitivity(final float thresholdSensitivity) {
    mThresholdSensitivity = thresholdSensitivity;
    setFloat(mThresholdSensitivityLocation, mThresholdSensitivity);
}


public void setColorToReplace(float redComponent, float greenComponent,
        float blueComponent) {
    mColorToReplace = new float[] { redComponent, greenComponent,
            blueComponent };
    setFloatVec3(mColorToReplaceLocation, mColorToReplace);
}

In this code I can able to get value change in mSmoothing and mThresholdSensitivity but no change appear in actual image output.

Also this class extends GPUImageTwoInputFilter class.

More code of same class. If you need more information then provide detail about it.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Siddharth
  • 4,142
  • 9
  • 44
  • 90
  • What are you asking? Did you mean the program is *not* working for you? You need to supply more details of how it isn't working. – GuyRT Jul 01 '14 at 13:20
  • This program is not working for me. There is no change into source image after applying this shader. I have edited my question with more information. – Siddharth Jul 01 '14 at 13:27

0 Answers0