added dilation and erosion to image filter

This commit is contained in:
Sven Vogel 2023-08-07 18:23:14 +02:00
parent 67dfef3af6
commit 8ebb7b181a
3 changed files with 95 additions and 0 deletions

View File

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

View File

@ -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.

Binary file not shown.

After

Width:  |  Height:  |  Size: 613 KiB