-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·102 lines (80 loc) · 2.54 KB
/
script.js
File metadata and controls
executable file
·102 lines (80 loc) · 2.54 KB
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
let canvas = document.getElementById('canvas');
let slider = document.getElementById('Rank')
let value = document.getElementById('value')
value.textContent = slider.value;
slider.onchange = function() {
value.textContent = this.value;
k = parseInt(this.value)
lowerResolution(k)
}
let img = new Image();
img.crossOrigin = 'anonymous';
img.src = 'Tiger.png';
let ctx = canvas.getContext('2d');
img.onload = function() {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
};
function reduceMatrix(A, k, bool) {
// removes the last k columns of A
// if bool is true then it also removes the last k rows
if (bool) {
A.splice(k, A.length -1);
return A;
}
else {
B = [];
let newRow
for (row of A){
newRow = row.slice(0,k)
B.push(newRow);
}
return B
}
}
let lowerResolution = function(k) {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
let colorsArray = [];
let newLine = [];
let j =0;
for (let i = 0; i < data.length; i += 4) {
j++
let avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
if (j<100) {
newLine.push(avg)
}
else {
j=0
newLine.push(avg)
colorsArray.push(newLine)
newLine = [];
}
}
// SVD calculation:
const {u, v, q} = SVDJS.SVD(colorsArray)
Uk = reduceMatrix(u, k, false);
Vk = reduceMatrix(v, k, false);
Sk = reduceMatrix(q, k, true)
const matrixUk = math.matrix(Uk)
const matrixVk = math.matrix(Vk)
const matrixVkTransp = math.transpose(matrixVk)
const matrixSk = math.diag(Sk)
USk = math.multiply(matrixUk, matrixSk);
const Ak = math.multiply(USk, matrixVkTransp)
// Convert into image
const [height, width] = Ak._size;
let buffer = new Uint8ClampedArray(width*height*4)
const oneDimensional = [];
for(var y = 0; y < height; y++) {
for(var x = 0; x < width; x++) {
var pos = (y * width + x) * 4;
buffer[pos ] = Ak._data[y][x]; // R
buffer[pos+1] = Ak._data[y][x]; // G
buffer[pos+2] = Ak._data[y][x]; // B
buffer[pos+3] = 255; // alpha }
}
}
imageData.data.set(buffer);
ctx.putImageData(imageData, 0, 0);
}