编辑:
感谢 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 中具有相同的值)来替换阿尔法通道。