-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2.html
executable file
·186 lines (142 loc) · 4.27 KB
/
m2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<video id="video" src="v.webm" style="width:384px; height:288px;" controls></video>
<canvas id="canvas" style="width:384px; height:288px;"></canvas>
<div></div>
<input type="range" value="10" min="0" max="255" id="tolleranza"><input type="text" id="tolleranza-val">
<script>
var v = document.getElementById('video');
var c = document.getElementById('canvas');
var frame1 = null;
var frame2 = null;
var d;
function getRGB(m, i) {
return [m.data[i], m.data[i+1], m.data[i+2]];
}
function setRGB(m, i, r, g ,b) {
m.data[i] = r;
m.data[i+1] = g;
m.data[i+2] = b;
}
function createRGB() {
if(typeof(colours) == "undefined")
colours = Array();
if(typeof(colours_count) == "undefined")
colours_count = 0;
if(typeof(colours[colours_count]) == "undefined") {
colours[colours_count] = [Math.floor(Math.random()*256), Math.floor(Math.random()*256), Math.floor(Math.random()*256)];
}
colours_count++;
return colours[colours_count-1];
}
function resetRGB() {
if(typeof(colours_count) != "undefined")
colours_count = 0;
}
function createMatrix() {
var matrix = Array();
var m = c.getContext('2d').getImageData(0, 0, 384, 288);
for(var i = 0; i < m.data.length; i = i+4) {
var v = m.data[i] == 0?0:1;
var ii = parseInt(i/4);
var row = parseInt(ii/384);
var col = parseInt(ii%384);
if(!(matrix[row] instanceof Array))
matrix[row] = Array();
//matrix[row][col] = {value:v, visited:false};
matrix[row][col] = v;
}
return matrix;
}
function findExit(matrix, i, j, cc, color) {
matrix[i][j] = 0;
//cc = c.getContext('2d').getImageData(0, 0, 384, 288);
setRGB(cc, ((i*384)+j)*4, color[0], color[1], color[2]);
//cc.data[((i*384)+j)*4] = color;
//c.getContext('2d').putImageData(cc, 0, 0);
try {
if(j >= 1)
if(matrix[i][j-1] == 1)
findExit(matrix, i, j-1, cc, color);
if(j < 383)
if(matrix[i][j+1] == 1)
findExit(matrix, i, j+1, cc, color);
if(i < 287)
if(matrix[i+1][j] == 1)
findExit(matrix, i+1, j, cc, color);
if(i >= 1)
if(matrix[i-1][j] == 1)
findExit(matrix, i-1, j, cc, color);
} catch(e) {
console.log("Troppo movimenti!");
}
return;
}
function visit() {
resetRGB();
var cc = c.getContext('2d').getImageData(0, 0, 384, 288);
var matrix = createMatrix();
for(var i = 0; i < matrix.length; i++) {
for(var j = 0; j < matrix[i].length; j++) {
if(matrix[i][j] == 1) {
findExit(matrix, i, j, cc, createRGB());
//console.log(i+" "+j);
//return;
}
}
}
c.getContext('2d').putImageData(cc, 0, 0);
}
function getFrame(video) {
var tmpc = document.createElement('canvas');
tmpc.getContext('2d').drawImage(v, 0, 0, 384, 288, 0, 0, 300, 150);
var tmpi = tmpc.getContext('2d').getImageData(0, 0, 384, 288);
if(frame1 == null) {
frame1 = tmpi;
return true;
}
if(frame2 == null) {
frame2 = tmpi;
return true;
}
frame1 = frame2;
frame2 = tmpi;
}
function draw() {
getFrame(v);
if(frame1 == null || frame2 == null)
return;
var g1 = toGrayScale(frame1);
var g2 = toGrayScale(frame2);
var t = document.getElementById('tolleranza').value;
var m = getMotionDetect(g1, g2, t);
c.getContext('2d').putImageData(m, 0, 0);
visit();
}
function toGrayScale(image) {
for(var i = 0; i < image.data.length; i = i+4) {
var g = (0.2126*image.data[i]) + (0.7152*image.data[i+1]) + (0.0722*image.data[i+2]);
image.data[i] = image.data[i+1] = image.data[i+2] = g;
}
return image;
}
function getMotionDetect(m1, m2, t) {
var motion = c.getContext('2d').createImageData(384, 288);
for(var i = 0; i < m1.data.length; i = i+4) {
if(Math.abs(m1.data[i] - m2.data[i]) > t)
motion.data[i] = motion.data[i+1] = motion.data[i+2] = 255;
else
motion.data[i] = motion.data[i+1] = motion.data[i+2] = 0;
motion.data[i+3] = 255;
}
return motion;
}
v.addEventListener('play', function() {
d = setInterval(draw, 60);
});
v.addEventListener('pause', function() {
clearInterval(d);
});
document.getElementById('tolleranza').addEventListener('change', function() {
document.getElementById('tolleranza-val').value = this.value;
});
document.getElementById('tolleranza').dispatchEvent(new Event('change'));
</script>