ios - 对 UIImage 应用 Sepia、RGB、GrayScale 效果

标签 ios opengl-es uiimage

我想在我的应用程序中对图像应用滤镜效果。 我是 Open GL 的新手,想在我的应用中对图像应用Sepia、RGB、GrayScale 效果。 我实现了亮度、对比度、饱和度效果,但无法找到任何关于灰度、RGB 和棕褐色效果的解决方案。

提前致谢。

最佳答案

您没有指定是要在 OpenGL ES 1.1 还是 2.0 中执行此操作,因此我假设您指的是较新的 2.0。如果 1.1 确实是您的目标,那么您需要像 Apple 在其 GLImageProcessing example 中那样使用混合模式。 .

对于 OpenGL ES 2.0,您可以使用片段着色器来实现这些效果中的每一个。对于图像的灰度版本,您可以使用以下片段着色器仅提取亮度:

 precision highp float;

 varying vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;

 const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);

 void main()
 {
     float luminance = dot(texture2D(inputImageTexture, textureCoordinate).rgb, W);

     gl_FragColor = vec4(vec3(luminance), 1.0);
 }

对于棕褐色调,您可以使用我在 this answer 中演示的颜色矩阵操作着色器。 :

 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;

 uniform lowp mat4 colorMatrix;
 uniform lowp float intensity;

 void main()
 {
     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     lowp vec4 outputColor = textureColor * colorMatrix;

     gl_FragColor = (intensity * outputColor) + ((1.0 - intensity) * textureColor);
 }

矩阵为

self.colorMatrix = (GPUMatrix4x4){
        {0.3588, 0.7044, 0.1368, 0},
        {0.2990, 0.5870, 0.1140, 0},
        {0.2392, 0.4696, 0.0912 ,0},
        {0,0,0,0},
    };

我不知道您所说的“RGB 效果”是什么意思。也许您的意思是颜色矩阵操作,在这种情况下,上述内容也适用于您。

所有这些都是我开源中的内置过滤器 GPUImage框架(参见 GPUImageBrightnessFilter、GPUImageContrastFilter、GPUImageSaturationFilter、GPUImageSepiaFilter 和 GPUImageColorMatrixFilter)。如果您真的是 OpenGL ES 的新手,您将需要大量代码来设置场景、将 UIImage 作为纹理拉入、针对它运行顶点和片段着色器、检索该图像并保存它作为 UIImage 退出。 GPUImage 将通过几行 Objective-C 代码为您完成所有这些工作。

关于ios - 对 UIImage 应用 Sepia、RGB、GrayScale 效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9700499/

相关文章:

ios - 从包含 CGImage 的 UIImage 对象获取 NSData

iphone - 以编程方式设置 UIDatepicker

ios - 将测试广告更改为真实广告

ios - 为什么标签没有被添加到 View 中?

ios - 如何将多重纹理用作一种纹理?

ios - createCGImage(_ :from:) not using correct rect

ios - 当应用程序从后台返回时显示 View Controller (iOS 13)

GLSL 中的矩阵转换被无限拉伸(stretch)

iphone - 如何像 Talking Tomcat 应用程序在 iphone 中一样录制屏幕视频?

ios - 在 UIImage 上为文本创建渐变