如何使用GPUImage实现GPUImageMaskFilter [英] How to Implement GPUImageMaskFilter using GPUImage

查看:738
本文介绍了如何使用GPUImage实现GPUImageMaskFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用蒙版从完整图像剪切并创建蒙版图像。

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

+ =

我尝试了以下内容:

UIImage *imgMask = [UIImage imageNamed:@"Mask.png"];
UIImage *imgBgImage = [UIImage imageNamed:@"Full.png"];



GPUImageMaskFilter *maskingFilter = [[GPUImageMaskFilter alloc] init];


GPUImagePicture * maskGpuImage = [[GPUImagePicture alloc] initWithImage:imgMask ];

GPUImagePicture *FullGpuImage = [[GPUImagePicture alloc] initWithImage:imgBgImage ];




[maskGpuImage addTarget:maskingFilter];
[maskGpuImage processImage];


[maskingFilter useNextFrameForImageCapture];


[FullGpuImage addTarget:maskingFilter];
[FullGpuImage processImage];



UIImage *OutputImage = [maskingFilter imageFromCurrentFramebuffer];

但是,我生成的输出图像是:

But , my generated output image is:

请大家一起携手。
干杯。

Please guys join hands. Cheers.

此外,感谢 BradLarson

推荐答案

掩码是第二个目标,如过滤器中所示着色器代码(textureColor2)。

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);

然后你需要反转你的面具:黑色背景上的白心,因为过滤器使用的是RGB像素值的权重,用于设置目标图像上的alpha值。

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.

所以你的代码应该是

// 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

预期结果。

这篇关于如何使用GPUImage实现GPUImageMaskFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆