着色器图像处理(边缘检测)

拉普拉斯算子(laplacian)

如果在图像中一个较暗的区域中出现了一个亮点,那么用拉普拉斯运算就会使这个亮点变得更亮。

拉普拉斯变换效果:

  1. 强调图像中灰度突变
  2. 降低灰度慢变化的区域

参考文档https://wenku.baidu.com/view/23a4720a6c85ec3a87c2c598.html

拉普拉斯运算模板

0, 1, 0
1, -4, 1
0, 1, 0

当我们的每一个像素点通过拉普拉斯过滤后, 就会呈现边缘化


precision highp float;
varying lowp vec2 varyTextCoord;
uniform sampler2D texMap;
uniform float stepValue;

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

// 卷积核大小
const int kernelSize = 9;
//0-0.009
void main()
{
    int i;
    vec4 sum = vec4(0.0);
    
    float Kernel[kernelSize];
    Kernel[6] = 0.0; Kernel[7] = 1.0; Kernel[8] = 0.0;
    Kernel[3] = 1.0; Kernel[4] = -4.0; Kernel[5] = 1.0;
    Kernel[0] = 0.0; Kernel[1] = 1.0; Kernel[2] = 0.0;
    
    float fStep = stepValue;
    vec2 Offset[kernelSize];
    Offset[0] = vec2(-fStep,-fStep); Offset[1] = vec2(0.0,-fStep); Offset[2] = vec2(fStep,-fStep);
    Offset[3] = vec2(-fStep,0.0);    Offset[4] = vec2(0.0,0.0);    Offset[5] = vec2(fStep,0.0);
    Offset[6] = vec2(-fStep, fStep); Offset[7] = vec2(0.0, fStep); Offset[8] = vec2(fStep, fStep);
    
    for (i = 0; i < kernelSize; i++)
    {
        vec4 tmp = texture2D(texMap, varyTextCoord.st + Offset[i]);
        sum += tmp * Kernel[i];
    }
    
    float luminance = dot(sum.rgb, W);

    gl_FragColor = vec4(vec3(luminance), 1.0);
}
灰度边缘检测

Sobel

边检测是一种十分经典的图像处理技术,且可在片元着色器中方便地得以实现。边检测处理技:使用两个Sobel过滤器,分别用于处理分量和垂直分量。前述内容已对Sobel过滤器有所提及,垂直Sob过滤器除了旋转90°之外与水平Sobel过滤器相同。水平过滤器和垂直过滤器分别表示为:

-1, -2, -1
 0,  0,  0        
 1,  2,  1
 -1, 0, 1
 -2, 0, 2
 -1, 0, 1

Sobel过滤器比较间隔为一列(或行)的两列数据(或两行数据,取决于过滤器的类型),若存在边,则颜色值之间彼此接近,过滤器将返回一个较小值。若返回值或数据值较大,则当前处理过程可判断出边的存在。该测试可在原始图像或仅包含亮度值的图像上进行。


precision highp float;
varying lowp vec2 varyTextCoord;
uniform sampler2D texMap;
/距离中心点多元的距离
uniform float stepValue;
//原色和灰度色的混合比例
uniform float ratioValue;

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

const int kernelSize = 9;
//0-0.009
void main()
{
    int i;
    float hSum = 0.0;
    float vSum = 0.0;
    vec3 irgb = texture2D( texMap,  varyTextCoord).rgb;
    vec4 color = vec4(0.0);

    float hKernel[kernelSize];
    hKernel[0] = -1.0; hKernel[1] = -2.0; hKernel[2] = -1.0;
    hKernel[3] = 0.0; hKernel[4] = 0.0; hKernel[5] = 0.0;
    hKernel[6] = 1.0; hKernel[7] = 2.0; hKernel[8] = 1.0;

    float vKernel[kernelSize];
    vKernel[0] = -1.0; vKernel[1] = 0.0; vKernel[2] = 1.0;
    vKernel[3] = -2.0; vKernel[4] = 0.0; vKernel[5] = 2.0;
    vKernel[6] = -1.0; vKernel[7] = 0.0; vKernel[8] = 1.0;

    float fStep = stepValue;
    vec2 Offset[kernelSize];
    Offset[0] = vec2(-fStep,-fStep); Offset[1] = vec2(0.0,-fStep); Offset[2] = vec2(fStep,-fStep);
    Offset[3] = vec2(-fStep,0.0);    Offset[4] = vec2(0.0,0.0);    Offset[5] = vec2(fStep,0.0);
    Offset[6] = vec2(-fStep, fStep); Offset[7] = vec2(0.0, fStep); Offset[8] = vec2(fStep, fStep);
    
    //水平soble过滤器
    for (i = 0; i < kernelSize; i++)
    {
        vec4 tmp = texture2D(texMap, varyTextCoord.st + Offset[i]);
        hSum += dot(tmp.rgb, W) * hKernel[i];
    }
     //垂直soble过滤器
    for (i = 0; i < kernelSize; i++)
    {
        vec4 tmp = texture2D(texMap, varyTextCoord.st + Offset[i]);
        vSum += dot(tmp.rgb, W) * vKernel[i];
    }

    float mag = sqrt( hSum*hSum + vSum*vSum);
    vec3 target = vec3( mag,mag,mag );
    color = vec4( mix( irgb, target, ratioValue), 1.);

    gl_FragColor = color;
}

混合函数是为了将边缘的灰度与原色进行混合, 以达到彩色边缘的效果

    color = vec4( mix( irgb, target, ratioValue), 1.);
原色灰度插值边缘检测

你可能感兴趣的:(着色器图像处理(边缘检测))