IOS – OpenGL ES 图像加亮边缘 GPUImage3x3ConvolutionFilter

打印 上一主题 下一主题

主题 562|帖子 562|积分 1688

目录
零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础
零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 转场
零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 特效
零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 函数
零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES GPUImage 使用
零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES GLSL 编程
一.简介

GPUImage 共 125 个滤镜, 分为四类
1、Color adjustments : 31 filters , 颜色处理相关
2、Image processing : 40 filters , 图像处理相关.
3、Blending modes : 29 filters , 混合模式相关.
4、Visual effects : 25 filters , 视觉效果相关.
GPUImage3x3ConvolutionFilter 属于 GPUImage 图像视觉效果相关,用来处理图像加亮边缘。shader 源码如下:
  1. /******************************************************************************************/
  2. //@Author:猿说编程
  3. //@Blog(个人博客地址): www.codersrc.com
  4. //@File:IOS – OpenGL ES 图像加亮边缘 GPUImage3x3ConvolutionFilter
  5. //@Time:2022/06/08 06:30
  6. //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
  7. /******************************************************************************************/
  8. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
  9. NSString *const kGPUImage3x3ConvolutionFragmentShaderString = SHADER_STRING
  10. (
  11. precision highp float;
  12. uniform sampler2D inputImageTexture;
  13. uniform mediump mat3 convolutionMatrix;
  14. varying vec2 textureCoordinate;
  15. varying vec2 leftTextureCoordinate;
  16. varying vec2 rightTextureCoordinate;
  17. varying vec2 topTextureCoordinate;
  18. varying vec2 topLeftTextureCoordinate;
  19. varying vec2 topRightTextureCoordinate;
  20. varying vec2 bottomTextureCoordinate;
  21. varying vec2 bottomLeftTextureCoordinate;
  22. varying vec2 bottomRightTextureCoordinate;
  23. void main()
  24. {
  25.      mediump vec3 bottomColor = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;
  26.      mediump vec3 bottomLeftColor = texture2D(inputImageTexture, bottomLeftTextureCoordinate).rgb;
  27.      mediump vec3 bottomRightColor = texture2D(inputImageTexture, bottomRightTextureCoordinate).rgb;
  28.      mediump vec4 centerColor = texture2D(inputImageTexture, textureCoordinate);
  29.      mediump vec3 leftColor = texture2D(inputImageTexture, leftTextureCoordinate).rgb;
  30.      mediump vec3 rightColor = texture2D(inputImageTexture, rightTextureCoordinate).rgb;
  31.      mediump vec3 topColor = texture2D(inputImageTexture, topTextureCoordinate).rgb;
  32.      mediump vec3 topRightColor = texture2D(inputImageTexture, topRightTextureCoordinate).rgb;
  33.      mediump vec3 topLeftColor = texture2D(inputImageTexture, topLeftTextureCoordinate).rgb;
  34.      mediump vec3 resultColor = topLeftColor * convolutionMatrix[0][0] + topColor * convolutionMatrix[0][1] + topRightColor * convolutionMatrix[0][2];
  35.      resultColor += leftColor * convolutionMatrix[1][0] + centerColor.rgb * convolutionMatrix[1][1] + rightColor * convolutionMatrix[1][2];
  36.      resultColor += bottomLeftColor * convolutionMatrix[2][0] + bottomColor * convolutionMatrix[2][1] + bottomRightColor * convolutionMatrix[2][2];
  37.      gl_FragColor = vec4(resultColor, centerColor.a);
  38. }
  39. );
  40. #else
  41. NSString *const kGPUImage3x3ConvolutionFragmentShaderString = SHADER_STRING
  42. (
  43. uniform sampler2D inputImageTexture;
  44. uniform mat3 convolutionMatrix;
  45. varying vec2 textureCoordinate;
  46. varying vec2 leftTextureCoordinate;
  47. varying vec2 rightTextureCoordinate;
  48. varying vec2 topTextureCoordinate;
  49. varying vec2 topLeftTextureCoordinate;
  50. varying vec2 topRightTextureCoordinate;
  51. varying vec2 bottomTextureCoordinate;
  52. varying vec2 bottomLeftTextureCoordinate;
  53. varying vec2 bottomRightTextureCoordinate;
  54. void main()
  55. {
  56.      vec3 bottomColor = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;
  57.      vec3 bottomLeftColor = texture2D(inputImageTexture, bottomLeftTextureCoordinate).rgb;
  58.      vec3 bottomRightColor = texture2D(inputImageTexture, bottomRightTextureCoordinate).rgb;
  59.      vec4 centerColor = texture2D(inputImageTexture, textureCoordinate);
  60.      vec3 leftColor = texture2D(inputImageTexture, leftTextureCoordinate).rgb;
  61.      vec3 rightColor = texture2D(inputImageTexture, rightTextureCoordinate).rgb;
  62.      vec3 topColor = texture2D(inputImageTexture, topTextureCoordinate).rgb;
  63.      vec3 topRightColor = texture2D(inputImageTexture, topRightTextureCoordinate).rgb;
  64.      vec3 topLeftColor = texture2D(inputImageTexture, topLeftTextureCoordinate).rgb;
  65.      vec3 resultColor = topLeftColor * convolutionMatrix[0][0] + topColor * convolutionMatrix[0][1] + topRightColor * convolutionMatrix[0][2];
  66.      resultColor += leftColor * convolutionMatrix[1][0] + centerColor.rgb * convolutionMatrix[1][1] + rightColor * convolutionMatrix[1][2];
  67.      resultColor += bottomLeftColor * convolutionMatrix[2][0] + bottomColor * convolutionMatrix[2][1] + bottomRightColor * convolutionMatrix[2][2];
  68.      gl_FragColor = vec4(resultColor, centerColor.a);
  69. }
  70. );
  71. #endif
复制代码
二.效果演示

使用GPUImage3x3ConvolutionFilter** 完成**图像加亮边缘****,原图如下:

使用GPUImage3x3ConvolutionFilter** 完成****图像加亮边缘******,效果如下:

三.源码下载

OpenGL ES Demo 下载地址 : IOS – OpenGL ES 图像加亮边缘 GPUImage3x3ConvolutionFilter

四.猜你喜欢

本文由博客 - 猿说编程 猿说编程 发布!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表