GPUImage Chroma Key filter

蹲街弑〆低调 提交于 2019-12-03 20:56:38

Found this gem after a couple of days of trying..... Apparently you are suppose to hang on (strong ref) to GPUImagePicture. I was under the impression that the filter chain internally would have a strong reference, but that's not the case. Keeping it as instance var solved the problem.

I think you want the GPUImageChromaKeyBlendFilter, not the GPUImageChromaKeyFilter.

The blend takes in two source images, and replaces pixels matching the keying color in the first image with the pixel from the second image. The regular chroma keying filter simply reduces the alpha channel of pixels matching the target color to 0.0, it doesn't blend in another image.

Similar to the original question, I wanted to put a green-screen video on top of a custom view hierarchy incl. live video. Turned out this was not possible with the standard GPUImage ChromaKey filter(s). Instead of alpha blending, it blended the green pixels with the background pixels. For example a red background became yellow and blue became cyan.

The way to get it working involves two steps:

1) make sure the filterview has a transparent background:

filterView.backgroundColor=[UIColor clearColor];

2) Modify GPUImageChromaKeyFilter.m

old: gl_FragColor = vec4(textureColor.rgb, textureColor.a * blendValue);

new: gl_FragColor = vec4(textureColor.rgb * blendValue, 1.0 * blendValue);

Now all keyed (for example green) pixels in the video become transparent and uncover whatever is below the filterview, incl. (live-)video.

Have you set the target and selector for the slider?

This worked for me

[(GPUImageChromaKeyFilter *)filter setThresholdSensitivity:[(UISlider *)sender value]];
        [image setImage:[filter imageByFilteringImage:sourceImage]];

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!