Image Basics Using Fragment Shaders to Manipulate...

9
1 mjb – December 31, 2019 1 Computer Graphics Using Fragment Shaders to Manipulate Images image.pptx Mike Bailey [email protected] mjb – December 31, 2019 2 Image Basics ResS ResT Treat the image as a texture. Index it using the usual texture indexing (0. s, t 1.) When you get back an RGB from the texture, remember that: (0. r, g, b 1.) Also, f you need to know the resolution of this texture, code this: Thus, to get from the current texel’s (s,t) to a neighboring texel’s (s,t), add ± (1./ResS , 1./ResT) ivec2 ires = textureSize( ImageUnit, 0 ); float ResS = float( ires.s ); float ResT = float( ires.t ); s = 1. s = 0. t = 1. t = 0. mjb – December 31, 2019 3 Computer Graphics Image Negative ( R, G, B ) ( 1.-R, 1.-G, 1.-B ) mjb – December 31, 2019 4 Computer Graphics Image Distortion uniform float uS0, uT0; uniform float uPower; uniform sampler2D uTexUnit; in vec2 vST; void main( ) { vec2 delta = vST - vec2(uS0,uT0); st = vec2(uS0,uT0) + sign(delta) * pow( abs(delta), uPower ); vec3 rgb = texture2D( uTexUnit, vST ).rgb; gl_FragColor= vec4( rgb, 1. ); }

Transcript of Image Basics Using Fragment Shaders to Manipulate...

1

mjb – December 31, 2019

1

Computer Graphics

Using Fragment Shaders to Manipulate Images

image.pptx

Mike [email protected]

mjb – December 31, 2019

2

Computer Graphics

Image Basics

ResS

Res

T

Treat the image as a texture. Index it using the usual texture indexing

(0. ≤ s, t ≤ 1.)

When you get back an RGB from the texture, remember that:

(0. ≤ r, g, b ≤ 1.)

Also, f you need to know the resolution of this texture, code this:

Thus, to get from the current texel’s (s,t) to a neighboring texel’s (s,t), add

± (1./ResS , 1./ResT)

ivec2 ires = textureSize( ImageUnit, 0 );float ResS = float( ires.s );float ResT = float( ires.t );

s = 1.s =

0.

t = 1.

t = 0.

mjb – December 31, 2019

3

Computer Graphics

Image Negative

( R, G, B ) ( 1.-R, 1.-G, 1.-B )

mjb – December 31, 2019

4

Computer Graphics

Image Distortion

uniform float uS0, uT0;uniform float uPower;uniform sampler2D uTexUnit;in vec2 vST;

voidmain( ){

vec2 delta = vST - vec2(uS0,uT0);st = vec2(uS0,uT0) + sign(delta) * pow( abs(delta), uPower );vec3 rgb = texture2D( uTexUnit, vST ).rgb;gl_FragColor= vec4( rgb, 1. );

}

2

mjb – December 31, 2019

5

Computer Graphics

0 1(1 )Q t Q tQ

Image Un-masking:Interpolation can still happen when t < 0. or t > 1.

t = -1.

t = 0.

t = 2.

t = 1.

mjb – December 31, 2019

6

Computer Graphics

Image Un-Masking:Abusing the Linear Blending Equation for a Good Purpose

t

What Idon’t want

More of what I

do want

What I have to start with

0.0 1.0 2.0

Iout = (1 - t)*Idontwant + t*Iin

}}

Blend of what I have and what I don’t want

Blend of what I have and less of what I don’t want

0 1(1 )Q t Q tQ

mjb – December 31, 2019

7

Computer Graphics

Brightness

Idontwant = vec3( 0., 0., 0. );

T = 0. T = 1. T = 2.

mjb – December 31, 2019

8

Computer Graphics

Contrast

Idontwant = vec3( 0.5, 0.5, 0.5 );

T = 0. T = 1. T = 2.

3

mjb – December 31, 2019

9

Computer Graphics

HDTV Luminance Standard

Luminance = 0.2125*Red + 0.7154*Green + 0.0721*Blue

mjb – December 31, 2019

10

Computer Graphics

Idontwant = vec3( luminance, luminance, luminance );

Saturation

T = 0. T = 1. T = 3.

mjb – December 31, 2019

11

Computer Graphics

Difference

Idontwant = Ibefore

Iin = Iafter

T = 0. T = 2.T = 1.

mjb – December 31, 2019

12

Computer Graphics

ChromaKey

Replace the fragment if:

R < T

G < TB > 1.-T

T = 0. T = 0.5 T = 1.

4

mjb – December 31, 2019

13

Computer Graphics

Blur Convolution:

Blur

121242121

.16.1B

mjb – December 31, 2019

14

Computer Graphics

Blur Convolution:

Sharpening

121242121

.16.1B

Idontwant = Iblur

mjb – December 31, 2019

15

Computer Graphics

Sharpening

vec2 stp0 = vec2(1./ResS, 0. );vec2 st0p = vec2(0. , 1./ResT);vec2 stpp = vec2(1./ResS, 1./ResT);vec2 stpm = vec2(1./ResS, -1./ResT);vec3 i00 = texture2D( uImageUnit, vST ).rgb;vec3 im1m1 = texture2D( uImageUnit, vST-stpp ).rgb;vec3 ip1p1 = texture2D( uImageUnit, vST+stpp ).rgb;vec3 im1p1 = texture2D( uImageUnit, vST-stpm ).rgb;vec3 ip1m1 = texture2D( uImageUnit, vST+stpm ).rgb;vec3 im10 = texture2D( uImageUnit, vST-stp0 ).rgb;vec3 ip10 = texture2D( uImageUnit, vST+stp0 ).rgb;vec3 i0m1 = texture2D( uImageUnit, vST-st0p ).rgb;vec3 i0p1 = texture2D( uImageUnit, vST+st0p ).rgb;vec3 target = vec3(0.,0.,0.);target += 1.*(im1m1+ip1m1+ip1p1+im1p1);target += 2.*(im10+ip10+i0m1+i0p1);target += 4.*(i00);target /= 16.;gl_FragColor = vec4( mix( target, irgb, T ), 1. );

mjb – December 31, 2019

16

Computer Graphics

Sharpening

T = 0.

T = 1.

T = 2.

5

mjb – December 31, 2019

17

Computer Graphics

Embossing

vec2 stp0 = vec2( 1./ResS, 0. );vec2 stpp = vec2( 1./ResS, 1./ResT);vec3 c00 = texture2D( uImageUnit, vST ).rgb;vec3 cp1p1 = texture2D( uImageUnit, vST + stpp ).rgb;

vec3 diffs = c00 - cp1p1;float max = diffs.r;if( abs(diffs.g) > abs(max) )

max = diffs.g;if( abs(diffs.b) > abs(max) )

max = diffs.b;

float gray = clamp( max + .5, 0., 1. );vec4 grayVersion = vec4( gray, gray, gray, 1. );vec4 colorVersion = vec4( gray*c00, 1. );gl_FragColor= mix( grayVersion, colorVersion, T );

mjb – December 31, 2019

18

Computer Graphics

Horizontal and Vertical Sobel Convolutions:

Edge Detection

121000121

H

101202101

V

VHS 22 Θ = atan2( V, H )

mjb – December 31, 2019

19

Computer Graphics

Edge Detection

const vec3 LUMCOEFFS = vec3( 0.2125,0.7154,0.0721 );. . .vec2 stp0 = vec2(1./ResS, 0. );vec2 st0p = vec2(0. , 1./ResT);vec2 stpp = vec2(1./ResS, 1./ResT);vec2 stpm = vec2(1./ResS, -1./ResT);float i00 = dot( texture2D( uImageUnit, vST ).rgb , LUMCOEFFS );float im1m1 = dot( texture2D( uImageUnit, vST-stpp ).rgb, LUMCOEFFS );float ip1p1 = dot( texture2D( uImageUnit, vST+stpp ).rgb, LUMCOEFFS );float im1p1 = dot( texture2D( uImageUnit, vST-stpm ).rgb, LUMCOEFFS );float ip1m1 = dot( texture2D( uImageUnit, vST+stpm ).rgb, LUMCOEFFS );float im10 = dot( texture2D( uImageUnit, vST-stp0 ).rgb, LUMCOEFFS );float ip10 = dot( texture2D( uImageUnit, vST+stp0 ).rgb, LUMCOEFFS );float i0m1 = dot( texture2D( uImageUnit, vST-st0p ).rgb, LUMCOEFFS );float i0p1 = dot( texture2D( uImageUnit, vST+st0p ).rgb, LUMCOEFFS) );float h = -1.*im1p1 - 2.*i0p1 - 1.*ip1p1 + 1.*im1m1 + 2.*i0m1 + 1.*ip1m1;float v = -1.*im1m1 - 2.*im10 - 1.*im1p1 + 1.*ip1m1 + 2.*ip10 + 1.*ip1p1;

float mag = sqrt( h*h + v*v );vec3 target = vec3( mag,mag,mag );color = vec4( mix( irgb, target, T ), 1. );

mjb – December 31, 2019

20

Computer Graphics

Edge Detection

T = 0. T = 0.5 T = 1.

6

mjb – December 31, 2019

21

Computer Graphics

float mag = sqrt( h*h + v*v );if( mag > uMagTol ){

gl_FragColor= vec4( 0., 0., 0., 1. );}else{

rgb.rgb *= uQuantize;rgb.rgb += vec3( .5, .5, .5 );ivec3 irgb = ivec3( rgb.rgb );rgb.rgb = vec3( irgb ) / uQuantize;gl_FragColor= vec4( rgb, 1. );

}

Toon Rendering

mjb – December 31, 2019

22

Computer Graphics

Toon RenderingOriginal Image

Colors Quantized

Outlines Added

mjb – December 31, 2019

23

Computer Graphics

Using shaders to enhance scientific, engineering, and

architectural illustration

Toon Rendering for Non-Photorealistic Effects

mjb – December 31, 2019

24

Computer Graphics

Toon Rendering for Non-Photorealistic Effects

Photo by Steve Cunningham

Using shaders to enhance scientific, engineering, and

architectural illustration

7

mjb – December 31, 2019

25

Computer Graphics

Mandelbrot Set

zzz ii 02

1

How fast does itconverge, if ever?

mjb – December 31, 2019

26

Computer Graphics

Julia Set

czz ii

21

How fast does itconverge, if ever?

mjb – December 31, 2019

27

Computer Graphics

Using Double Precision

mjb – December 31, 2019

28

Computer Graphics

We Can Do Image Processing on Dynamic Scenes with a Two-pass Approach

Pass #1

Pass #2

Render a 3D dynamic scene

Texture

Render a quadrilateral Framebuffer

LightingShader

BlurShader

8

mjb – December 31, 2019

29

Computer Graphics

##OpenGL GLIBPerspective 90

Texture2D 6 1024 1024

RenderToTexture 6

Background 0. 0.1 0.Clear

Vertex filter.vertFragment filter.fragProgram Filter1 \

Ad <.01 .2 .5> Bd <.01 .2 .5> \NoiseAmp <0. 0. 1.> NoiseFreq <0. 1. 2.> \Tol <0. 0. 1.>

Teapot

RenderToTexture

Background 0. 0.0 0ClearLookA t 0 0 2.5 0 0 0 0 1 0

Vertex image.vertFragment image.fragProgram Filter2 \

InUnit 6 \EdgeDetect <true> \TEdge <0. 0. 1.> \TSharp <-3. 1. 10.> \ResS 1024 ResT 1024

Translate 0 0 0.QuadXY .2 2.

filter.glib

mjb – December 31, 2019

30

Computer Graphics

#version 330 compatibility

out float vLightIntensity;out vec2 vST;out vec3 vMC;

voidmain( void ){

vST = gl_MultiTexCoord0.st;vMC = gl_Vertex.xyz;

vec3 tnorm = normalize( gl_NormalMatrix * gl_Normal );vec3 LightPos = vec3( 2., 10., 10. ); vec3 ECposition = vec3( gl_ModelViewMatrix * gl_Vertex );vLightIntensity = dot( normalize(LightPos - ECposition), tnorm );vLightIntensity = abs( vLightIntensity );

gl_Position = gl_ModelViewProjectionMatrix * gl_TextureMatrix[0] * gl_Vertex;}

filter.vert

mjb – December 31, 2019

31

Computer Graphics

#version 330 compatibilityin float vLightIntensity; in vec2 vST;in vec3 vMC;

uniform float Ad;uniform float Bd;uniform float NoiseAmp;uniform float NoiseFreq;uniform float Tol;uniform sampler3D Noise3;

const vec3 BEAVER = vec3( 1., .5, 0. );const vec3 OTHER = vec3( 1., .9, .2);

void main( ){

vec4 nv = texture3D( Noise3, NoiseFreq*vMC );float n = nv[0] + nv[1] + nv[2] + nv[3]; // 1. -> 3.n = ( n - 2. ); // -1. -> 1.vec2 st = vST;float Ar = Ad/2.;float Br = Bd/2.;

float numins = floor( st.s / Ad );float numint = floor( st.t / Bd );vec3 TheColor = OTHER;

vec2 center = vec2( numins*Ad + Ar , numint*Bd + Br );vec2 delta = st - center;float oldrad = length( delta );float newrad = oldrad + NoiseAmp*n;delta = delta * newrad / oldrad;float ds = delta.s/Ar;float dt = delta.t/Br;float d = ds*ds + dt*dt;float t = smoothstep( 1.-Tol, 1.+Tol, d );TheColor = mix( BEAVER, OTHER, t );gl_FragColor = vec4( vLightIntensity*TheColor, 1. );

}

filter.frag

mjb – December 31, 2019

32

Computer Graphics

9

mjb – December 31, 2019

33

Computer Graphics