Polishing sharpen and edge Image filters

This commit is contained in:
Bruno Herbelin
2022-06-02 23:57:43 +02:00
parent d7be7a69ab
commit dd76135efd
8 changed files with 50 additions and 60 deletions

View File

@@ -590,8 +590,8 @@ const char* SharpenFilter::method_label[SharpenFilter::SHARPEN_INVALID] = {
std::vector< FilteringProgram > SharpenFilter::programs_ = {
FilteringProgram("Unsharp Mask", "shaders/filters/sharpen_1.glsl", "shaders/filters/sharpen_2.glsl", { { "Amount", 0.5} }),
FilteringProgram("Sharpen", "shaders/filters/sharp.glsl", "", { { "Amount", 0.5} }),
FilteringProgram("Sharp Edge", "shaders/filters/bilinear.glsl", "shaders/filters/sharpenedge.glsl", { { "Strength", 0.5} }),
FilteringProgram("Sharpen", "shaders/filters/sharpen.glsl", "", { { "Amount", 0.5} }),
FilteringProgram("Sharp Edge", "shaders/filters/sharpenedge.glsl","", { { "Amount", 0.5} }),
FilteringProgram("TopHat", "shaders/filters/erosion.glsl", "shaders/filters/tophat.glsl", { { "Radius", 0.5} }),
FilteringProgram("BlackHat", "shaders/filters/dilation.glsl", "shaders/filters/blackhat.glsl", { { "Radius", 0.5} }),
};

View File

@@ -21,9 +21,11 @@ void mainImage( out vec4 fragColor, in vec2 fragCoord )
vec4 c = texture(iChannel1, fragCoord.xy / iResolution.xy);
// Remove blurred image to original image
vec4 lumcoeff = vec4(0.299, 0.587, 0.114, 1.);
float luminance = dot( blur1D(fragCoord, vec2(0,1), 0.1 * Amount ) - c, lumcoeff);
c = blur1D(fragCoord, vec2(0,1), 0.1 * Amount ) - c;
// convert to greyscale
float luminance = dot(c, vec4(0.299, 0.587, 0.114, 1.));
// composition
fragColor = vec4(luminance, luminance, luminance, 1.0);
fragColor = vec4( vec3( smoothstep( 0.05, 0.2, luminance) ), 1.0);
}

View File

@@ -3,22 +3,22 @@ uniform float Threshold;
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 c[16];
vec4 c[16];
for (int i=0; i < 4; ++i)
for (int j=0; j < 4; ++j)
{
c[4*i+j] = texture(iChannel0, (fragCoord.xy+vec2(i-2,j-2)) / iResolution.xy).rgb;
c[4*i+j] = texture(iChannel0, (fragCoord.xy+vec2(i-2,j-2)) / iResolution.xy);
}
// pseudo Scharr masks 4 x 4
vec3 Lx = 10.0*(c[14]-c[2] + c[13]-c[1]) + 3.0*(c[15]-c[3] + c[12]-c[0]);
vec4 Lx = 10.0*(c[14]-c[2] + c[13]-c[1]) + 3.0*(c[15]-c[3] + c[12]-c[0]);
Lx += 2.0*(c[10]-c[6] + c[9]-c[5]) + (c[11]-c[7] + c[8]-c[4]);
vec3 Ly = 10.0*(c[8]-c[11]+ c[4]-c[7]) + 3.0*(c[12]-c[15] + c[0]-c[3]);
vec4 Ly = 10.0*(c[8]-c[11]+ c[4]-c[7]) + 3.0*(c[12]-c[15] + c[0]-c[3]);
Ly += 2.0*(c[9]-c[10] + c[5]-c[6]) + (c[13]-c[14] + c[1]-c[2]);
vec3 G = sqrt(Lx*Lx+Ly*Ly);
vec4 G = sqrt(Lx*Lx+Ly*Ly);
//vec3 G = abs(Lx)+abs(Ly);
fragColor = vec4( vec3( step( mix( 1.0, 10.0, Threshold), length(G)) ), 1.0);
fragColor = vec4( vec3( step( mix( 0.5, 10.0, Threshold), length(G)) ), 1.0);
}

View File

@@ -1,4 +1,4 @@
uniform float Factor;
uniform float Factor;
const mat3 G[9] = mat3[](
mat3( 0.3535533845424652, 0, -0.3535533845424652, 0.5, 0, -0.5, 0.3535533845424652, 0, -0.3535533845424652 ),
@@ -12,31 +12,24 @@ const mat3 G[9] = mat3[](
mat3( 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408 )
);
void freichen( out vec3 rgb, in vec2 fragCoord )
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
mat3 I;
mat3 I;
float cnv[9];
vec3 sample;
vec4 s;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++) {
sample = texture(iChannel0, (fragCoord + vec2(i-1,j-1)) / iResolution.xy ).rgb;
I[i][j] = length(sample);
s = texture(iChannel0, (fragCoord + vec2(i-1,j-1)) / iResolution.xy );
I[i][j] = length(s);
}
for (int i=0; i<9; i++) {
float dp3 = dot(G[i][0], I[0]) + dot(G[i][1], I[1]) + dot(G[i][2], I[2]);
cnv[i] = dp3 * dp3;
cnv[i] = dp3 * dp3;
}
float M = cnv[0] + cnv[1] + cnv[2] + cnv[3];
float S = M + cnv[4] + cnv[5] + cnv[6] + cnv[7] + cnv[8];
rgb = vec3( (1.0 + 10.0 * Factor) * sqrt(M/S) );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 v;
freichen(v, fragCoord);
fragColor = vec4( v, 1.0);
fragColor = vec4( vec3( (1.0 + 10.0 * Factor) * sqrt(M/S) ), 1.0);
}

View File

@@ -3,21 +3,21 @@ uniform float Amount;
vec4 sharp(sampler2D sampler, vec2 fc, vec2 re)
{
float strength = mix( 2.0, 20.0, Amount);
vec2 uv = fc / re;
vec4 c0 = texture(sampler,uv);
vec4 c1 = texture(sampler,uv-vec2( 1./re.x,.0));
vec4 c2 = texture(sampler,uv+vec2( 1./re.x,.0));
vec4 c3 = texture(sampler,uv-vec2(.0, 1./re.y));
vec4 c4 = texture(sampler,uv+vec2(.0, 1./re.y));
vec4 c5 = c0+c1+c2+c3+c4;
c5*=0.2;
vec4 mi = min(c0,c1); mi = min(mi,c2); mi = min(mi,c3); mi = min(mi,c4);
vec4 ma = max(c0,c1); ma = max(ma,c2); ma = max(ma,c3); ma = max(ma,c4);
return clamp(mi,(strength+1.0)*c0-c5*strength,ma);
vec2 uv = fc / re;
vec4 c0 = texture(sampler,uv);
vec4 c1 = texture(sampler,uv-vec2( 1./re.x,.0));
vec4 c2 = texture(sampler,uv+vec2( 1./re.x,.0));
vec4 c3 = texture(sampler,uv-vec2(.0, 1./re.y));
vec4 c4 = texture(sampler,uv+vec2(.0, 1./re.y));
vec4 c5 = c0+c1+c2+c3+c4;
c5*=0.2;
vec4 mi = min(c0,c1); mi = min(mi,c2); mi = min(mi,c3); mi = min(mi,c4);
vec4 ma = max(c0,c1); ma = max(ma,c2); ma = max(ma,c3); ma = max(ma,c4);
return clamp(mi,(strength+1.0)*c0-c5*strength,ma);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
fragColor = sharp(iChannel0, fragCoord.xy, iResolution.xy);
fragColor = sharp(iChannel0, fragCoord.xy, iResolution.xy);
}

View File

@@ -1,11 +1,13 @@
vec3 texSample(const int x, const int y, in vec2 fragCoord)
uniform float Amount;
vec4 texSample(const int x, const int y, in vec2 fragCoord)
{
vec2 uv = fragCoord.xy / iResolution.xy * iChannelResolution[0].xy;
uv = (uv + vec2(x, y)) / iChannelResolution[0].xy;
return texture(iChannel0, uv).xyz;
return texture(iChannel0, uv);
}
void sharpen( out vec3 rgb, in vec2 fragCoord )
void sharpen( out vec4 rgb, in vec2 fragCoord )
{
rgb =
texSample(-1,-1, fragCoord) * -1. +
@@ -22,10 +24,10 @@ void sharpen( out vec3 rgb, in vec2 fragCoord )
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// sharpen mask
vec3 sharped;
vec4 sharped;
sharpen(sharped, fragCoord);
fragColor = vec4( sharped, 1.0);
fragColor = mix(texSample( 0, 0, fragCoord), sharped , 1.5 * Amount);;
}

View File

@@ -1,4 +1,4 @@
uniform float Strength;
uniform float Amount;
vec3 blurSample(in vec2 uv, in vec2 xoff, in vec2 yoff)
{
@@ -38,5 +38,5 @@ vec3 edgeStrength(in vec2 uv)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord.xy / iResolution.xy;
fragColor = vec4(texture(iChannel0, uv).xyz - edgeStrength(uv) * Strength * (iResolution.y*0.05), 1.0);
fragColor = vec4(texture(iChannel0, uv).xyz - edgeStrength(uv) * Amount * (iResolution.y*0.05), 1.0);
}

View File

@@ -5,28 +5,21 @@ const mat3 G[2] = mat3[](
mat3( 1.0, 0.0, -1.0, 2.0, 0.0, -2.0, 1.0, 0.0, -1.0 )
);
void sobel( out vec3 rgb, in vec2 fragCoord )
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
mat3 I;
float cnv[2];
vec3 sample;
vec4 s;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++) {
sample = texture(iChannel0, (fragCoord + vec2(i-1,j-1)) / iResolution.xy ).rgb;
I[i][j] = length(sample) * mix(1.0, 3.0, Factor);
s = texture(iChannel0, (fragCoord + vec2(i-1,j-1)) / iResolution.xy );
I[i][j] = length(s) * mix(1.0, 3.0, Factor);
}
for (int i=0; i<2; i++) {
float dp3 = dot(G[i][0], I[0]) + dot(G[i][1], I[1]) + dot(G[i][2], I[2]);
cnv[i] = dp3 * dp3;
float dp3 = dot(G[i][0], I[0]) + dot(G[i][1], I[1]) + dot(G[i][2], I[2]);
cnv[i] = dp3 * dp3;
}
rgb = vec3( sqrt(cnv[0]*cnv[0]+cnv[1]*cnv[1]) );
//rgb = vec3( abs(cnv[0]) + abs(cnv[1]) );
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 v;
sobel(v, fragCoord);
fragColor = vec4(v, 1.0);
fragColor = vec4( vec3( sqrt(cnv[0]*cnv[0]+cnv[1]*cnv[1]) ), 1.0);
}