aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--of.frag47
-rw-r--r--pix_opencv_opticalflow.pd150
2 files changed, 197 insertions, 0 deletions
diff --git a/of.frag b/of.frag
new file mode 100644
index 0000000..af7d99e
--- /dev/null
+++ b/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.);
+}
diff --git a/pix_opencv_opticalflow.pd b/pix_opencv_opticalflow.pd
new file mode 100644
index 0000000..9b7fa89
--- /dev/null
+++ b/pix_opencv_opticalflow.pd
@@ -0,0 +1,150 @@
+#N canvas 464 288 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 1047 500 translateXYZ 0 0 -4;
+#X obj 1047 607 square 4;
+#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 4 256;
+#X connect 1 0 2 0;
+#X connect 1 1 8 1;
+#X connect 1 1 23 1;
+#X connect 2 0 3 0;
+#X connect 3 0 16 0;
+#X connect 4 0 21 0;
+#X connect 5 0 36 0;
+#X connect 6 0 7 0;
+#X connect 7 0 8 0;
+#X connect 8 0 17 0;
+#X connect 9 0 11 0;
+#X connect 10 0 37 0;
+#X connect 11 0 8 0;
+#X connect 12 0 23 0;
+#X connect 14 0 15 0;
+#X connect 15 0 23 0;
+#X connect 18 0 27 1;
+#X connect 19 0 27 1;
+#X connect 20 0 27 1;
+#X connect 21 0 3 0;
+#X connect 22 0 6 0;
+#X connect 23 0 13 0;
+#X connect 25 0 24 0;
+#X connect 26 0 24 0;
+#X connect 27 0 12 0;
+#X connect 27 1 18 0;
+#X connect 27 1 44 0;
+#X connect 27 1 45 0;
+#X connect 28 0 32 0;
+#X connect 28 6 29 0;
+#X connect 29 0 33 1;
+#X connect 30 0 31 0;
+#X connect 31 0 28 0;
+#X connect 32 0 39 0;
+#X connect 32 1 3 1;
+#X connect 33 0 38 0;
+#X connect 34 0 35 0;
+#X connect 35 0 40 0;
+#X connect 35 0 41 0;
+#X connect 35 0 42 0;
+#X connect 36 0 1 0;
+#X connect 37 0 6 0;
+#X connect 38 1 41 0;
+#X connect 38 2 40 0;
+#X connect 39 0 42 0;
+#X connect 39 1 33 0;
+#X connect 40 0 6 0;
+#X connect 41 0 1 0;
+#X connect 42 0 27 0;
+#X connect 44 0 19 0;
+#X connect 45 0 20 0;