|
5 | 5 | <title></title> |
6 | 6 |
|
7 | 7 | <script type="text/javascript" src="../src/tracking.js"></script> |
| 8 | + <script type="text/javascript" src="../src/tracker/color.js"></script> |
8 | 9 | <script type="text/javascript" src="js/splines.js"></script> |
9 | 10 |
|
10 | 11 | <style type="text/css"> |
11 | 12 | #videoContainer { |
12 | 13 | display: none; |
13 | 14 | } |
14 | 15 |
|
15 | | - .flip-horizontally { |
| 16 | + canvas { |
16 | 17 | -moz-transform: scale(-1, 1); |
17 | 18 | -o-transform: scale(-1, 1); |
18 | 19 | -webkit-transform: scale(-1, 1); |
|
29 | 30 | <br/> |
30 | 31 |
|
31 | 32 | <script> |
32 | | - var videoCamera = new tracking.VideoCamera({ |
33 | | - audio: true |
34 | | - }), |
| 33 | +var videoCamera = new tracking.VideoCamera().render('#videoContainer'); |
35 | 34 |
|
36 | | - canvas = videoCamera.canvas, |
37 | | - canvasContext = canvas.context; |
| 35 | +videoCamera.renderVideoCanvas('#canvasContainer'); |
38 | 36 |
|
39 | | - canvas.domElement.classList.add('flip-horizontally'); |
| 37 | +var plotCanvas = new tracking.Canvas().render(), |
| 38 | + drawPoints = []; |
40 | 39 |
|
41 | | - videoCamera.render('#videoContainer'); |
42 | | - videoCamera.renderVideoCanvas('#canvasContainer'); |
43 | | - |
44 | | - var plotCanvas = new tracking.Canvas(), |
45 | | - drawPoints = []; |
46 | | - |
47 | | - plotCanvas.domElement.classList.add('flip-horizontally'); |
48 | | - plotCanvas.render(); |
49 | | - |
50 | | - var threshold = 50; |
51 | | - |
52 | | - function render() { |
53 | | - var imageData = videoCamera.getVideoCanvasImageData(), |
54 | | - cpx, |
55 | | - cpy, |
56 | | - cpz, |
57 | | - distance, |
58 | | - dx = 0, |
59 | | - dy = 0, |
60 | | - m, |
61 | | - maxx = -1, |
62 | | - maxy = -1, |
63 | | - minx = Infinity, |
64 | | - miny = Infinity, |
65 | | - n, |
66 | | - pixels = [], |
67 | | - total, |
68 | | - totalInliers = 0, |
69 | | - x, |
70 | | - y, |
71 | | - diff1, |
72 | | - diff2; |
73 | | - |
74 | | - canvas.forEach(imageData, function(r, g, b, a, w, i, j) { |
75 | | - diff1 = r - g; |
76 | | - diff2 = b - g; |
77 | | - |
78 | | - if (diff1 >= threshold && diff2 >= threshold) { |
79 | | - pixels.push(j, i); |
80 | | - } |
81 | | - }); |
82 | | - |
83 | | - total = pixels.length; |
84 | | - |
85 | | - if (total < 30) { |
86 | | - drawPoints = []; |
87 | | - return; |
88 | | - } |
89 | | - |
90 | | - // Flag outliers |
91 | | - for (m = 0; m < total; m+=2) { |
92 | | - distance = 0; |
93 | | - |
94 | | - for (n = 2; n < total; n+=2) { |
95 | | - distance += tracking.math.distance(pixels[m], pixels[m+1], pixels[n], pixels[n+1]); |
96 | | - } |
97 | | - |
98 | | - if (distance/total > 20) { |
99 | | - pixels[m] = -1; |
100 | | - pixels[m+1] = -1; |
101 | | - } |
| 40 | +videoCamera.track( |
| 41 | + { |
| 42 | + type: 'color', |
| 43 | + color: 'cyan', |
| 44 | + callback: function(tracking) { |
| 45 | + drawPoints.push(tracking.x, tracking.y); |
| 46 | + drawSpline(videoCamera.canvas.context, drawPoints, 0.5, false); |
102 | 47 | } |
103 | | - |
104 | | - for (m = 0; m < total; m+=2) { |
105 | | - x = pixels[m]; |
106 | | - y = pixels[m+1]; |
107 | | - |
108 | | - if (x > -1 && y > -1) { |
109 | | - dx += x; |
110 | | - dy += y; |
111 | | - totalInliers++; |
112 | | - |
113 | | - if (x < minx) { |
114 | | - minx = x; |
115 | | - } |
116 | | - |
117 | | - if (x > maxx) { |
118 | | - maxx = x; |
119 | | - } |
120 | | - |
121 | | - if (y < miny) { |
122 | | - miny = y; |
123 | | - } |
124 | | - |
125 | | - if (y > maxy) { |
126 | | - maxy = y; |
127 | | - } |
128 | | - |
129 | | - canvasContext.fillStyle = "rgb(255,0,0)"; |
130 | | - canvasContext.fillRect (x, y, 3, 3); |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - cpy = dy/totalInliers; |
135 | | - cpx = dx/totalInliers; |
136 | | - cpz = 60 - ((maxx - minx) + (maxy - miny))/2; |
137 | | - |
138 | | - // plotCanvas.context.fillStyle = "rgb(0, 0,0)"; |
139 | | - // plotCanvas.context.fillRect (cpx, cpy, 3, 3); |
140 | | - drawPoints.push(cpx, cpy); |
141 | | - // plotCanvas.context.clearRect(0,0,320,240); |
142 | | - drawSpline(canvasContext,drawPoints,0.5,false); |
143 | 48 | } |
144 | | - |
145 | | - |
146 | | - |
147 | | - (function loop() { |
148 | | - requestAnimationFrame(loop); |
149 | | - render(); |
150 | | - }()); |
| 49 | +); |
151 | 50 | </script> |
152 | 51 |
|
153 | 52 | </body> |
|
0 commit comments