How to Implement GPUImageMaskFilter using GPUImage

后端 未结 2 1324
难免孤独
难免孤独 2021-02-08 15:31

I need to Cut from the Full Image using the mask and created the masked Image.

\"Full +

2条回答
  •  情深已故
    2021-02-08 16:07

    The mask is the second target, as can been seen in the filter shader code (textureColor2).

    //Averages mask's the RGB values, and scales that value by the mask's alpha
    //
    //The dot product should take fewer cycles than doing an average normally
    //
    //Typical/ideal case, R,G, and B will be the same, and Alpha will be 1.0
    
     lowp float newAlpha = dot(textureColor2.rgb, vec3(.33333334, .33333334, .33333334)) * textureColor2.a;
    
     gl_FragColor = vec4(textureColor.xyz, newAlpha);
    

    Then you need to "invert" your mask : white heart on black background, as the filter uses the "weight" of the RGB pixel value to set the alpha value on the target image.

    So your code should be

    // Image first, Mask next
    [FullGpuImage addTarget:maskingFilter];
    [FullGpuImage processImage];
    
    [maskingFilter useNextFrameForImageCapture];
    
    [maskGpuImage addTarget:maskingFilter];
    [maskGpuImage processImage];    
    

    and your mask (ok I did an ugly quick test, use a proper image) like

    white heart on black background

    for the expected result.

    masked image

提交回复
热议问题