diff --git a/Image-Filtering/Dilation-Erosion/Image.glsl b/Image-Filtering/Dilation-Erosion/Image.glsl new file mode 100644 index 0000000..564484a --- /dev/null +++ b/Image-Filtering/Dilation-Erosion/Image.glsl @@ -0,0 +1,91 @@ + +//#define DIAMOND_KERNEL + +float lum(in vec3 rgb) +{ + return dot(rgb, vec3(0.21, 0.72, 0.07)); +} + +vec3 dilate(in sampler2D tex, in vec2 uv, in int size, in float sep, in float minb, in float maxb) +{ + vec3 cc = texture(tex, uv).rgb; + vec3 tc = cc; + float cl = lum(cc); + + for (int x = -size; x <= size; ++x) + for (int y = -size; y <= size; ++y) + { + + #ifdef DIAMOND_KERNEL + + if (abs(x) > size - abs(y)) + continue; + + #else + + if (distance(vec2(x, y), vec2(0, 0)) > float(size)) + continue; + + #endif + + vec3 s = texture(tex, uv + vec2(x,y)/iResolution.xy*sep).rgb; + float b = lum(s); + + if (cl < b) + { + cl = b; + tc = s; + } + } + + return mix(cc, tc, smoothstep(minb, maxb, cl)); +} + +vec3 erode(in sampler2D tex, in vec2 uv, in int size, in float sep, in float minb, in float maxb) +{ + vec3 cc = texture(tex, uv).rgb; + vec3 tc = cc; + float cl = lum(cc); + + for (int x = -size; x <= size; ++x) + for (int y = -size; y <= size; ++y) + { + + #ifdef DIAMOND_KERNEL + + if (abs(x) > size - abs(y)) + continue; + + #else + + if (distance(vec2(x, y), vec2(0, 0)) > float(size)) + continue; + + #endif + + vec3 s = texture(tex, uv + vec2(x,y)/iResolution.xy*sep).rgb; + float b = lum(s); + + if (cl > b) + { + cl = b; + tc = s; + } + } + + return mix(tc, cc, smoothstep(minb, maxb, cl)); +} + +void mainImage( out vec4 fragColor, in vec2 fragCoord ) +{ + // Normalized pixel coordinates (from 0 to 1) + vec2 uv = fragCoord/iResolution.xy; + + vec3 dil = dilate(iChannel0, uv, 12, (uv.x-.5)*3., 0.0, 1.0); + vec3 er = erode(iChannel0, uv, 12, (uv.x-.5)*3., 0.0, 1.0); + + vec3 col = uv.x > 0.5 ? er : dil; + + // Output to screen + fragColor = vec4(col,1.0); +} \ No newline at end of file diff --git a/Image-Filtering/Dilation-Erosion/README.md b/Image-Filtering/Dilation-Erosion/README.md new file mode 100644 index 0000000..2620604 --- /dev/null +++ b/Image-Filtering/Dilation-Erosion/README.md @@ -0,0 +1,4 @@ +# Dilation / Erosion filter +![overview.png](https://git.teridax.de/teridax/Shadertoy-Shaders/raw/branch/main/Image-Filtering/Dilation-Erosion/overview.png) + +Dilation is the process of intesifying the brightessed pixel of a kernel, where as erosion intesifies the darkesed pixel of a kernel. \ No newline at end of file diff --git a/Image-Filtering/Dilation-Erosion/overview.png b/Image-Filtering/Dilation-Erosion/overview.png new file mode 100644 index 0000000..6e8ecf2 Binary files /dev/null and b/Image-Filtering/Dilation-Erosion/overview.png differ