-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
116 lines (98 loc) · 2.5 KB
/
index.js
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
const px = require('pixels');
const _ = require('lodash');
const kernel = require('./kernel');
let image = px.read('./images/img1.jpg', Float64Array);
const {
height,
width,
data,
type
} = image;
const arrayData = [];
// 从Float64Array => Arrary
data.forEach(item => {
arrayData.push(item);
});
const pixels = _.chunk(arrayData, 4);
/**
* get width * height matrix
*/
const matrix = _.chunk(pixels, width);
const zeros = [];
for (let i = 0; i < width; i++) {
zeros.push([0, 0, 0, 1]);
}
// 矩阵开头插入一行0像素
matrix.unshift(_.cloneDeep(zeros));
// 矩阵末尾插入一行0像素
matrix.push(_.cloneDeep(zeros));
matrix.forEach(item => {
item.push([0, 0, 0, 1]);
item.unshift([0, 0, 0, 1]);
});
// 获得一个全为0的矩阵
const getMatrix = (...agrs) => {
let matrix = 0;
while (agrs.length) {
const length = agrs.pop();
const temp = [];
for (let i = 0; i < length; i++) {
temp.push(_.cloneDeep(matrix));
}
matrix = temp;
}
return matrix;
};
/**
* 使用一个卷积核来卷积图像
*
* @param {Array} image 待卷积图像
* @param {Arrary} kernel 卷积核
*/
const convolution = (image, kernel) => {
const width = image[0].length - 2;
const height = image.length - 2;
const newImage = getMatrix(height, width, 4);
/**
* 两个矩阵块相乘
* @param {*} kernel 卷积核
* @param {*} matrix 图像矩阵
* @param {*} x 当前位置x
* @param {*} y 当前位置y
*/
const calc = (kernel, matrix, x, y) => {
const length = kernel.length;
for (let k = 0; k < 3; k++) {
let sum = 0;
for (let i = 0; i < length; i++)
for (let j = 0; j < length; j++) {
sum += (kernel[length - i - 1][length - j - 1] * matrix[x + i + -1][y + j + -1][k]);
}
newImage[x - 1][y - 1][k] = Math.abs(sum || 0);
}
newImage[x - 1][y - 1][3] = matrix[x][y][3];
};
// 开始卷积图像
for (let i = 1; i < width - 1; i++) {
for (let j = 1; j < height - 1; j++) {
calc(kernel, image, j, i);
}
}
return newImage;
};
Object.keys(kernel).forEach(item => {
const k = kernel[item];
const newImage = convolution(matrix, k);
const cresult = _.flattenDeep(newImage);
const cimage = new Float64Array(width * height * 4);
for (let i = 0; i < width * height * 4; i++) {
cimage[i] = cresult[i];
}
px.write(`./output/img1_${item}.jpg`, {
height: height,
width: width,
type: type,
data: cimage
});
console.log(`success ==> ./output/img1_${item}.jpg`);
});