How to Implement GPUImageMaskFilter using GPUImage-open source projects BradLarson/GPUImage

lazi74

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

for the expected result.