scatter és gather típusú algoritmusok

32
Scatter és gather típusú algoritmusok

Upload: beate

Post on 17-Jan-2016

57 views

Category:

Documents


6 download

DESCRIPTION

Scatter és gather típusú algoritmusok. Hisztogram. Kvantált kép fényesség értékei: G [ 0, Gmax ] G fényességű pontok száma: P(G). Hisztogram. Hisztogram. Kevés árnyalat Túlexponált. Hisztogram. Hisztogram generálás a GPU-n. Vertex shader. Blending. Fragmens shader. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Scatter  és gather típusú algoritmusok

Scatter és gather típusú algoritmusok

Page 2: Scatter  és gather típusú algoritmusok

Kvantált kép fényesség értékei: G [0, Gmax]

G fényességű pontok száma: P(G)

Hisztogram

ggp )( ggg ,

max

0

1)(g

dggp

Page 3: Scatter  és gather típusú algoritmusok

Hisztogram

Page 4: Scatter  és gather típusú algoritmusok

Kevés árnyalat

Túlexponált

Hisztogram

Page 5: Scatter  és gather típusú algoritmusok

Hisztogram

Hisztogram generálás a GPU-n

Vertex shader

Fragmens shader

Blending 1 2 15 6 4 9

31

Page 6: Scatter  és gather típusú algoritmusok

Fragmensek kompozitálása glBlendEquation(equation)▪ GL_FUNC_ADD

▪ GL_FUNC_SUBTRACT

▪ GL_FUNC_REVERSE_SUBTRACT

▪ GL_MIN

▪ GL_MAX

Hisztogram

rdrs DRSRR adas DASAA

rdrs DRSRR adas DASAA

rsrd SRDRR asad SADAA

),min( ds RRR ),min( ds AAA

),max( ds RRR ),max( ds AAA

Page 7: Scatter  és gather típusú algoritmusok

Hisztogram

glBlendFunc(src, dst) GL_ZERO, GL_ONE

GL_SRC_COLOR, GL_DST_COLOR

GL_SRC_ALPHA, GL_DST_ALPHA

GL_CONSTANT_COLOR, GL_CONSTANT_ALPHA

GL_ONE_MINUS_...

0rS 0A

sr RS sAA

sr AS sAA

cr RS cAA

sr RS 1 sAA 1

Page 8: Scatter  és gather típusú algoritmusok

Hisztogram generálás Grid

SzámításglEnable(GL_BLEND);glBlendFunc(GL_ONE, GL_ONE);histogramBuffer->setRenderTarget();histogramShader->enable();histogramShader->bindUniformTexture(„inputBuffer”, texture->getTextureHandle(0), 0);grid->render(histogramShader);histogramShader->disable();histogramBuffer->disableRenderTarget();glDisable(GL_BLEND);

GLfloat* vertices = new GLfloat[3 * width * height];for(int y = 0; y < height; ++y){ for(int x = 0; x < width; ++x){ vertices[3 * (x + y * width) ] = x / width; vertices[3 * (x + y * width) + 1] = y / height; vertices[3 * (x + y * width) + 2] = 0.0f; }}

Page 9: Scatter  és gather típusú algoritmusok

Hisztogram generálás

Vertex shader

Fragmens shader

uniform sampler2D inputBuffer;in vec4 position;

void main(void){ vec2 resolution = textureSize(inputBuffer, 0); float luminance = texture(inputBuffer, position.xy + (0.5 / resolution)).x; gl_Position = vec4( 2.0 * (luminance - 0.5), 0.0, 0.0, 1.0);}

out vec4 outColor;

void main(void){ outColor = vec4(1.0);}

Page 10: Scatter  és gather típusú algoritmusok

Hisztogram generálás

Vertex shader II.uniform sampler2D inputBuffer;uniform float hLevels;

in vec4 position;

void main(void){ vec2 resolution = textureSize(inputBuffer, 0); float luminance = texture(inputBuffer, position.xy + (0.5 / resolution)).x; gl_Position = vec4( 2.0 * (luminance * (1.0 – 1.0 / hLevels) + 0.5 / hLevels - 0.5),

0.0, 0.0, 1.0);}

Page 11: Scatter  és gather típusú algoritmusok

Kép visszaolvasása

Forrás kiválasztása Buffer kiválasztása

▪ GL_DRAW_FRAMEBUFFER▪ GL_READ_FRAMEBUFFER▪ GL_FRAMEBUFFER

Komponens kiválasztása

▪ GL_COLOR_ATTACHMENTx▪ GL_DEPTH_COMPONENT▪ GL_DEPTH_STENCIL▪ GL_STENCIL_INDEX

glReadBuffer(source);

glBindBuffer(target, buffer);

Page 12: Scatter  és gather típusú algoritmusok

Kép visszaolvasása

Buffer másolása

Format▪ GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA▪ GL_RGB, GL_RGBA▪ GL_LUMINANCE, GL_LUMINANCE_ALPHA

Type▪ GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT▪ GL_UNSIGNED_INT_8_8_8_8▪ ...

glReadPixels(x, y, width, height, format, type, *data);

Page 13: Scatter  és gather típusú algoritmusok

Kép visszaolvasása

float histogram[255] = {0.0f};

glBindFramebuffer(GL_FRAMEBUFFER, histogramBuffer->getHandle());glReadBuffer(GL_COLOR_ATTACHMENT0);glReadPixels(0, 0, 255, 1, GL_RED, GL_FLOAT, histogram);glBindFramebuffer(GL_FRAMEBUFFER, 0);

1 2 15 6 4 9 31

Framebuffer

Page 14: Scatter  és gather típusú algoritmusok

Normálás

Hisztogram

)( 112

max* GGGG

GG

Page 15: Scatter  és gather típusú algoritmusok

Hisztogram

Normálásuniform sampler2D inputMap;uniform float G1;uniform float G2;uniform float Gmax;

in vec2 fTexCoord;out vec4 outColor;

void main(void){ float luminance = texture(inputMap, fTexCoord); outColor = vec4( Gmax / (G2 – G1) * (luminance – G1)); }

Page 16: Scatter  és gather típusú algoritmusok

Hisztogram

Kvantálásuniform sampler2D inputMap;uniform int levels;

in vec2 fTexCoord;out vec4 outColor;

void main(void){ float c = texture(inputMap, fTexCoord); float threshold = 1.0 / levels; while(c > threshold){ threshold += 1.0 / levels; } outColor = vec4(threshold);}

Page 17: Scatter  és gather típusú algoritmusok

Histogram kiegyenlítés

)()()( hpgfhgp

dh

dggphp )()(

)()(1

maxmax

gphdg

dh

dh

dggp

h

g

dphgh0

max )()(

Page 18: Scatter  és gather típusú algoritmusok

Histogram kiegyenlítés

max

0

)(G

Gpix GPN

pixN

GPGP

)()(

G

pix

PN

HH

0

max )(int

Page 19: Scatter  és gather típusú algoritmusok

Histogram kiegyenlítés

Summed area table Mip-map alternatíva Minden elemben egy összeg van Gyorsan számítható

időben generálhatóYT

YB

XL XR

)(log2 n

Page 20: Scatter  és gather típusú algoritmusok

Histogram kiegyenlítés

SAT generálás

DCBA E

C+DB+CA+BA D+E

A+B+C+DA+B+CA+BA B+C+D+E

A+B+C+DA+B+CA+BA A+B+C+D+E

Page 21: Scatter  és gather típusú algoritmusok

Histogram kiegyenlítés

SAT generálástA = input imagen = logr(width)m = logr(height)

// horizontal phasefor(i=0; i<n; i=i+1) tB[x,y] = tA[x,y] + tA[x+1*ri, y] + tA[x+2*ri, y] + ... tA[x+r*ri, y]

swap(tA, tB)

Page 22: Scatter  és gather típusú algoritmusok

Histogram kiegyenlítés

SAT generálás OpenGL

float offset = 1.0f / (float)histogramLevels;int inputBuffer = 0;for(int i=0; i<8; ++i){ sat[(inputBuffer + 1) % 2]->setRenderTarget(0); satShader->enable(); satShader->bindUniformTexture(„inputMap”, sat[inputBuffer]->getColorBuffer(0), 0); satShader->bindUniformFloat(„offset”, -offset); fullscreenQuad->render(satShader); sat[(inputBuffer + 1) % 2]->disableRenderTarget();

inputBuffer = (inputBuffer + 1) % 2; offset *= 2.0f;}

Page 23: Scatter  és gather típusú algoritmusok

Histogram kiegyenlítés

SAT generálás Fragmens shader

uniform sampler2D inputMap;uniform float offset;

in vec2 fTexCoord;out vec4 outColor;

void main(void){ float current = texture(inputMap, fTexCoord).x; float sampleOff = fTexCoord.x + offset; float addValue = 0.0; if(sampleOff >= 0.0){ addValue = texture(inputMap, vec2(sampleOff, 0.0)).x; }

outColor = vec4(current + addValue);}

Page 24: Scatter  és gather típusú algoritmusok

Kiegyenlítő leképzés

Histogram kiegyenlítés

uniform sampler2D inputMap;uniform sampler2D histogram;uniform float level;

in vec2 fTexCoord;out vec4 outColor;

void main(void){ float current = texture(inputMap, fTexCoord).x; float accum = texture(histogram, vec2(current, 0.0)).x;

float bin = floor(accum / level);

outColor = vec4(bin);}

Page 25: Scatter  és gather típusú algoritmusok

Tone mapping

Dinamika tartományok

Leképzés HDR-ről LDR-re Luminancia: Y Átlagos luminancia: Y’

],0[]1,0[

'Y

YYr

r

rr

YWY

YD

1

1 2

r

rr

Y

WYYD

1

)/1( 2

Page 26: Scatter  és gather típusú algoritmusok

Tone mapping

Page 27: Scatter  és gather típusú algoritmusok

Tone mapping

A leképzés lépései

Luminancia leképzés

tA = eredeti kép

tL = luminancia(tA)

látlag = átlagol(tL)

tB = transform(tA, látlag)

float luminance(vec4 color){ return vec4(color.r * 0.2126, color.g * 0.7152, color.b * 0.0722, 1.0);}

Page 28: Scatter  és gather típusú algoritmusok

Tone mapping

Átlag képzés Sat Textúra piramis

Page 29: Scatter  és gather típusú algoritmusok

Tone mapping

Átlag képzésuniform sampler2D inputBuffer;

in vec2 fTexCoord;out vec4 outColor;

void main(void){ vec2 offset = 1.0 / textureSize(inputBuffer, 0); float sum = 0.0; for(int y = -1; y <= 1; ++y){ for(int x = -1; x <= 1; ++x){ sum += texture(inputBuffer, fTexCoord + vec2(x, y) * offset); } } outColor = vec4(sum / 9.0);}

Page 30: Scatter  és gather típusú algoritmusok

Tone mapping

Átlagos luminanciauniform sampler2D inputBuffer;uniform float Yavg;uniform float alpha;

in vec2 fTexCoord;out vec4 outColor;

void main(void){ float Y = texture(inputBuffer, fTexCoord); outColor = vec4(alpha * Y / Yavg);}

'Y

YYr

Page 31: Scatter  és gather típusú algoritmusok

Tone mapping

Végső transzformációuniform sampler2D inputBuffer;uniform float W;

in vec2 fTexCoord;out vec4 outColor;

void main(void){ vec4 Yr = texture(inputBuffer, fTexCoord); outColor = vec4(Yr * (1 + Yr / (W * W)) / (1.0 + Yr));}

r

rr

YWY

YD

1

1 2

Page 32: Scatter  és gather típusú algoritmusok

Tone mapping