From 48886dd80cf5a05dad279363e8fc9743dd7770ae Mon Sep 17 00:00:00 2001 From: Antoine Villeret Date: Thu, 27 Dec 2012 14:41:22 +0000 Subject: clean up directory fix help patch dependency improve REAMDE svn path=/trunk/externals/pix_opencv/; revision=16770 --- examples/06_GPU_opticalflow/06_GPU_opticalflow.pd | 169 ++++++++++++++++++++++ examples/06_GPU_opticalflow/of.frag | 47 ++++++ 2 files changed, 216 insertions(+) create mode 100644 examples/06_GPU_opticalflow/06_GPU_opticalflow.pd create mode 100644 examples/06_GPU_opticalflow/of.frag (limited to 'examples') diff --git a/examples/06_GPU_opticalflow/06_GPU_opticalflow.pd b/examples/06_GPU_opticalflow/06_GPU_opticalflow.pd new file mode 100644 index 0000000..c607683 --- /dev/null +++ b/examples/06_GPU_opticalflow/06_GPU_opticalflow.pd @@ -0,0 +1,169 @@ +#N canvas 371 215 1451 649 10; +#X obj 936 450 cnv 15 200 40 empty empty empty 20 12 0 14 -261234 -66577 +0; +#X obj 149 397 gemframebuffer; +#X obj 149 424 translateXYZ 0 0 -4; +#X obj 149 558 pix_texture; +#X obj 194 511 loadbang; +#X obj 162 343 loadbang; +#X obj 568 438 gemframebuffer; +#X obj 568 476 translateXYZ 0 0 -4; +#X obj 568 577 pix_texture; +#X obj 603 521 loadbang; +#X obj 594 359 loadbang; +#X msg 600 542 rectangle 1 \, quality 1; +#X obj 1057 537 loadbang; +#X msg 1057 557 rectangle 1 \, quality 1; +#X obj 149 587 rectangle 4 4; +#X obj 568 603 square 4; +#X msg 1204 461 previousFrame 1 \, currentFrame 2; +#X msg 1224 432 lambda \$1; +#X msg 1288 431 scale \$1 \$1; +#X msg 194 533 rectangle 1 \, quality 1; +#X obj 608 419 r of_dim; +#X obj 1047 587 pix_texture; +#X obj 342 179 gemwin; +#X msg 325 140 create \, 1; +#X msg 398 140 0 \, destroy; +#N canvas 493 216 473 548 glsl_frag 0; +#X obj 104 413 glsl_program; +#X msg 61 190 print; +#X obj 179 255 change; +#X msg 60 360 print; +#X obj 104 232 glsl_fragment; +#X floatatom 179 281 2 0 0 0 ID - -; +#X obj 197 327 print linking; +#X text 123 361 after linking \, click "print" to get some info about +the compiled shader program; +#X obj 41 16 inlet; +#X obj 104 449 outlet; +#X obj 262 57 inlet; +#X obj 41 39 route bang; +#X obj 114 140 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 168 51 gemhead 1; +#X msg 168 73 1; +#X obj 168 97 change; +#X obj 168 121 t b; +#X obj 183 455 t b; +#X obj 183 476 outlet; +#X msg 179 306 link \$1; +#X obj 114 166 t b; +#X msg 113 189 open of.frag; +#X connect 0 0 9 0; +#X connect 0 1 17 0; +#X connect 1 0 4 0; +#X connect 2 0 5 0; +#X connect 3 0 0 0; +#X connect 4 0 0 0; +#X connect 4 1 2 0; +#X connect 5 0 19 0; +#X connect 8 0 11 0; +#X connect 10 0 0 0; +#X connect 11 0 20 0; +#X connect 11 1 4 0; +#X connect 12 0 20 0; +#X connect 13 0 14 0; +#X connect 14 0 15 0; +#X connect 15 0 16 0; +#X connect 16 0 20 0; +#X connect 17 0 18 0; +#X connect 19 0 0 0; +#X connect 19 0 6 0; +#X connect 20 0 21 0; +#X connect 21 0 4 0; +#X restore 1047 464 pd glsl_frag; +#X obj 529 66 pix_info _____________; +#X msg 639 116 \$1; +#X obj 529 5 gemhead; +#X obj 529 45 pix_video; +#X obj 529 113 pix_texture; +#X obj 529 179 spigot; +#X obj 586 250 loadbang; +#X msg 586 271 0; +#X msg 162 362 rectangle 1 \, format RGB32 \, type FLOAT \, texunit +2 \, dimen 640 480; +#X msg 594 386 rectangle 1 \, texunit 1 \, format RGB32 \, dimen 640 +480; +#X obj 529 210 t b b b; +#X obj 529 138 t b b; +#X obj 568 323 gemhead; +#X obj 149 314 gemhead; +#X obj 1047 347 gemhead; +#X text 941 464 shader inside -->; +#X obj 1219 407 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 +10 -262144 -1 -1 0 256; +#X obj 1289 404 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 +10 -262144 -1 -1 1 256; +#X obj 1289 359 loadbang; +#X msg 1289 382 1; +#X text 815 119 this is an example of how to compute optical flow on +GPU; +#X obj 1240 212 loadbang; +#X msg 1240 232 rectangle 1 \, quality 1; +#X obj 1230 262 pix_texture; +#X obj 1230 140 gemhead; +#X text 571 179 <-- only render when a new image arrived; +#X obj 1044 498 translateXYZ 0 -4 -4; +#X obj 1229 174 translateXYZ 0 4 -4; +#X obj 1231 282 rectangle 4 3; +#X obj 1047 607 rectangle 4 3; +#X connect 1 0 2 0; +#X connect 1 1 8 1; +#X connect 1 1 21 1; +#X connect 2 0 3 0; +#X connect 3 0 14 0; +#X connect 4 0 19 0; +#X connect 5 0 34 0; +#X connect 6 0 7 0; +#X connect 7 0 8 0; +#X connect 8 0 15 0; +#X connect 9 0 11 0; +#X connect 10 0 35 0; +#X connect 11 0 8 0; +#X connect 12 0 13 0; +#X connect 13 0 21 0; +#X connect 16 0 25 1; +#X connect 17 0 25 1; +#X connect 18 0 25 1; +#X connect 19 0 3 0; +#X connect 20 0 6 0; +#X connect 21 0 55 0; +#X connect 23 0 22 0; +#X connect 24 0 22 0; +#X connect 25 0 52 0; +#X connect 25 1 16 0; +#X connect 25 1 42 0; +#X connect 25 1 43 0; +#X connect 26 0 30 0; +#X connect 26 6 27 0; +#X connect 27 0 31 1; +#X connect 28 0 29 0; +#X connect 29 0 26 0; +#X connect 30 0 37 0; +#X connect 30 1 3 1; +#X connect 30 1 49 1; +#X connect 31 0 36 0; +#X connect 32 0 33 0; +#X connect 33 0 38 0; +#X connect 33 0 39 0; +#X connect 33 0 40 0; +#X connect 34 0 1 0; +#X connect 35 0 6 0; +#X connect 36 1 39 0; +#X connect 36 2 38 0; +#X connect 37 0 40 0; +#X connect 37 1 31 0; +#X connect 38 0 6 0; +#X connect 39 0 1 0; +#X connect 40 0 25 0; +#X connect 42 0 17 0; +#X connect 43 0 18 0; +#X connect 44 0 45 0; +#X connect 45 0 43 0; +#X connect 47 0 48 0; +#X connect 48 0 49 0; +#X connect 49 0 54 0; +#X connect 50 0 53 0; +#X connect 52 0 21 0; +#X connect 53 0 49 0; diff --git a/examples/06_GPU_opticalflow/of.frag b/examples/06_GPU_opticalflow/of.frag new file mode 100644 index 0000000..af7d99e --- /dev/null +++ b/examples/06_GPU_opticalflow/of.frag @@ -0,0 +1,47 @@ +#extension GL_ARB_texture_rectangle : enable + +uniform sampler2DRect currentFrame, previousFrame; +uniform float lambda; +uniform vec2 scale; + +vec2 offset = vec2(1.,1.); +vec2 texcoord0 = gl_TexCoord[0].st; +vec2 texcoord1 = gl_TexCoord[1].st; + +void main() +{ + vec4 a = texture2DRect(currentFrame, texcoord0); + vec4 b = texture2DRect(previousFrame , texcoord1); + vec2 x1 = vec2(offset.x,0.); + vec2 y1 = vec2(0.,offset.y); + + //get the difference + vec4 curdif = b-a; + + //calculate the gradient + vec4 gradx = texture2DRect(previousFrame, texcoord1+x1)-texture2DRect(previousFrame, texcoord1-x1); + gradx += texture2DRect(currentFrame, texcoord0+x1)-texture2DRect(currentFrame, texcoord0-x1); + vec4 grady = texture2DRect(previousFrame, texcoord1+y1)-texture2DRect(previousFrame, texcoord1-y1); + grady += texture2DRect(currentFrame, texcoord0+y1)-texture2DRect(currentFrame, texcoord0-y1); + vec4 gradmag = sqrt((gradx*gradx)+(grady*grady)+vec4(lambda)); + + if ( gradmag.r == 0. ) { + gl_FragColor = vec4(0.5,0.5,0.5,1); + return; + } + + vec4 vx = curdif*(gradx/gradmag); + float vxd = vx.r;//assumes greyscale + //format output for flowrepos, out(-x,+x,-y,+y) + //~ vec2 xout = vec2(max(vxd,0.),abs(min(vxd,0.)))*scale.x; + float xout = ((vxd/2.)*scale.x+0.5); + + vec4 vy = curdif*(grady/gradmag); + float vyd = vy.r;//assumes greyscale + //format output for flowrepos, out(-x,+x,-y,+y) + //~ vec2 yout = vec2(max(vyd,0.),abs(min(vyd,0.)))*scale.y; + float yout = ((vyd/2.)*scale.y+0.5); + + //~ gl_FragColor = vec4(xout.xy,yout.xy); + gl_FragColor = vec4(xout,yout,0.5,1.); +} -- cgit v1.2.1