【问题标题】:Sobel filter in C/C++ using OpenGL ES使用 OpenGL ES 在 C/C++ 中的 Sobel 过滤器
【发布时间】:2012-03-08 02:32:09
【问题描述】:

如果我不需要,我宁愿不重新创建轮子,而且这必须在以前完成。有没有使用 OpenGL ES 的 Sobel 过滤器的实现?

【问题讨论】:

标签: c++ c opengl-es opengl-es-2.0


【解决方案1】:

如果 Objective-C 可以接受,您可以查看我的 GPUImage 框架及其 GPUImageSobelEdgeDetectionFilter。这应用了使用 OpenGL ES 2.0 片段着色器的 Sobel 边缘检测。您可以在this answer 的“草图”示例中看到此输出。

如果您不想深入研究 Objective-C 代码,这里的关键工作是由两组着色器执行的。在第一遍中,我将图像降低到其亮度并将该值存储在红色、绿色和蓝色通道中。我使用以下顶点着色器执行此操作:

 attribute vec4 position;
 attribute vec4 inputTextureCoordinate;

 varying vec2 textureCoordinate;

 void main()
 {
    gl_Position = position;
    textureCoordinate = inputTextureCoordinate.xy;
 }

和片段着色器:

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

之后,我实际上使用这个顶点着色器执行了 Sobel 边缘检测(在这种情况下,较亮的像素是边缘):

 attribute vec4 position;
 attribute vec4 inputTextureCoordinate;

 uniform highp float imageWidthFactor; 
 uniform highp float imageHeightFactor; 

 varying vec2 textureCoordinate;
 varying vec2 leftTextureCoordinate;
 varying vec2 rightTextureCoordinate;

 varying vec2 topTextureCoordinate;
 varying vec2 topLeftTextureCoordinate;
 varying vec2 topRightTextureCoordinate;

 varying vec2 bottomTextureCoordinate;
 varying vec2 bottomLeftTextureCoordinate;
 varying vec2 bottomRightTextureCoordinate;

 void main()
 {
     gl_Position = position;

     vec2 widthStep = vec2(imageWidthFactor, 0.0);
     vec2 heightStep = vec2(0.0, imageHeightFactor);
     vec2 widthHeightStep = vec2(imageWidthFactor, imageHeightFactor);
     vec2 widthNegativeHeightStep = vec2(imageWidthFactor, -imageHeightFactor);

     textureCoordinate = inputTextureCoordinate.xy;
     leftTextureCoordinate = inputTextureCoordinate.xy - widthStep;
     rightTextureCoordinate = inputTextureCoordinate.xy + widthStep;

     topTextureCoordinate = inputTextureCoordinate.xy + heightStep;
     topLeftTextureCoordinate = inputTextureCoordinate.xy - widthNegativeHeightStep;
     topRightTextureCoordinate = inputTextureCoordinate.xy + widthHeightStep;

     bottomTextureCoordinate = inputTextureCoordinate.xy - heightStep;
     bottomLeftTextureCoordinate = inputTextureCoordinate.xy - widthHeightStep;
     bottomRightTextureCoordinate = inputTextureCoordinate.xy + widthNegativeHeightStep;
 }

还有这个片段着色器:

 precision highp float;

 varying vec2 textureCoordinate;
 varying vec2 leftTextureCoordinate;
 varying vec2 rightTextureCoordinate;

 varying vec2 topTextureCoordinate;
 varying vec2 topLeftTextureCoordinate;
 varying vec2 topRightTextureCoordinate;

 varying vec2 bottomTextureCoordinate;
 varying vec2 bottomLeftTextureCoordinate;
 varying vec2 bottomRightTextureCoordinate;

 uniform sampler2D inputImageTexture;

 void main()
 {
    float i00   = texture2D(inputImageTexture, textureCoordinate).r;
    float im1m1 = texture2D(inputImageTexture, bottomLeftTextureCoordinate).r;
    float ip1p1 = texture2D(inputImageTexture, topRightTextureCoordinate).r;
    float im1p1 = texture2D(inputImageTexture, topLeftTextureCoordinate).r;
    float ip1m1 = texture2D(inputImageTexture, bottomRightTextureCoordinate).r;
    float im10 = texture2D(inputImageTexture, leftTextureCoordinate).r;
    float ip10 = texture2D(inputImageTexture, rightTextureCoordinate).r;
    float i0m1 = texture2D(inputImageTexture, bottomTextureCoordinate).r;
    float i0p1 = texture2D(inputImageTexture, topTextureCoordinate).r;
    float h = -im1p1 - 2.0 * i0p1 - ip1p1 + im1m1 + 2.0 * i0m1 + ip1m1;
    float v = -im1m1 - 2.0 * im10 - im1p1 + ip1m1 + 2.0 * ip10 + ip1p1;

    float mag = length(vec2(h, v));

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

imageWidthFactorimageHeightFactor 只是输入图像大小的倒数(以像素为单位)。

您可能会注意到,这种两遍方法比上面链接的答案中的方法更复杂。这是因为在移动 GPU(至少是 iOS 设备中的 PowerVR 种类)上运行时,最初的实现并不是最高效的。通过删除所有相关的纹理读取并预先计算亮度,这样我只需要从最终着色器中的红色通道进行采样,在我的基准测试中,这种调整后的边缘检测方法比一次性完成所有这些的天真的方法快 20 倍。

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2017-12-16
    • 2012-07-04
    • 2019-03-11
    • 2019-03-27
    • 2020-04-01
    相关资源
    最近更新 更多