转换黑色&白色图像透明& GPUImage中的白色图像? RGB - > Alpha基本上

时间:2013-05-30 21:01:49

标签: ios macos gpuimage alpha-transparency rgba

我从GPUImage中的边缘检测滤镜获得了黑白图像,其中白色代表边缘,大部分图像内容为黑色,不透明。问题是,我想将此图像叠加在另一个图像的顶部,以显示边缘如何与下面的图像对齐。

但是非边缘区域不是透明而是黑色,所以基本上我希望黑色是透明的,或者将黑度和白度的强度改变为透明度。

我没有为此转换任务找到直接的GPUImage过滤器。 GPUImageChromaKeyFilter似乎是一种这样做的方式,但它不仅仅是RGB到Alpha通道转换,并不适用于此任务。

1 个答案:

答案 0 :(得分:3)

修改: 感谢Brad的评论,没有交换频道的更有效的解决方案正在取代“gl_FragColor = vec4(vec3(mag),1.0);”用“gl_FragColor = vec4(mag);”在边缘检测过滤器源中。

您可以使用GPUImageColorMatrixFilter中的参数进行播放,并将通道从一个映射到另一个。示例代码:

GPUImagePicture *gpuImage = [[GPUImagePicture alloc] initWithImage:image];
GPUImageSobelEdgeDetectionFilter *edgeFilter = [[GPUImageSobelEdgeDetectionFilter alloc] init];

GPUImageColorMatrixFilter *conversionFilter = [[GPUImageColorMatrixFilter alloc] init];
conversionFilter.colorMatrix = (GPUMatrix4x4){
    {0.0, 0.0, 0.0, 1.0},
    {0.0, 0.0, 0.0, 1.0},
    {0.0, 0.0, 0.0, 1.0},
    {1.0,0.0,0.0,0.0},
};

[gpuImage addTarget:edgeFilter];
[edgeFilter addTarget:conversionFilter];
[gpuImage processImage];

return [conversionFilter imageFromCurrentlyProcessedOutputWithOrientation:orientation];

colorMatrix中指定的值基本上表示使用前一个Alpha通道(非透明图像全部为255)来替换RGB通道,并使用之前的R通道(黑白图像在RGB中具有相同的值)来替换Alpha通道。