-
Notifications
You must be signed in to change notification settings - Fork 0
/
horizontalEdgeDetected.js
95 lines (85 loc) · 2.9 KB
/
horizontalEdgeDetected.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
// input : image
// output : edge detected
var pixels = require("image-pixels");
var sizeOf = require("image-size");
var fs = require("fs");
const Jimp = require("jimp");
async function edgeDetected(point2pointDistance, edgedetectDistance) {
// load single source
let width;
let height;
sizeOf("./image/result/sample2.png", function (err, dimensions) {
width = dimensions.width;
height = dimensions.height;
});
var { data } = await pixels("./image/result/sample2.png");
let resultdata = [];
let count = 0;
for (let i = 0; i < height; i++) {
resultdata[i] = [];
for (let j = 0; j < width; j++) {
resultdata[i][j] = [];
for (let k = 0; k < 4; k++) {
if (k != 3) {
resultdata[i][j].push(data[count++]);
} else {
count++;
}
}
}
}
//resultdata : 1차원 데이터를 3차원 데이터로 width * height * 4(R, G, B, A)
let edgeData_horizontal = [];
for (let i = 0; i < height - point2pointDistance - 1; i++) {
//width version
edgeData_horizontal[i] = [];
for (let j = 0; j < width - 1; j++) {
//data[i][j] = r g b a
edgeData_horizontal[i][j] = colorPointDistance(resultdata[i][j + point2pointDistance], resultdata[i][j]);
}
}
//edgeData_horizontal : 자기 자신과 바로 오른쪽 점과의 공간좌표 RGB 에서의 거리
edgeData_horizontal = edgeDetectedArray(edgeData_horizontal, edgedetectDistance);
// console.log(edgeDetectedArray(edgeData_horizontal, edgedetectDistance));
// console.log(edgeData_horizontal);
let image = new Jimp(width, height, function (err, image) {
if (err) throw err;
edgeData_horizontal.forEach((row, y) => {
row.forEach((color, x) => {
image.setPixelColor(color, x, y);
});
});
image.write("./image/result/sample2_hor_result.png", (err) => {
if (err) throw err;
});
});
}
//horizontal
function colorPointDistance(a, b) {
let x = (a[0] - b[0]) * (a[0] - b[0]);
let y = (a[1] - b[1]) * (a[1] - b[1]);
let z = (a[2] - b[2]) * (a[2] - b[2]);
let result;
result = Math.sqrt(x + y + z);
return result;
}
function edgeDetectedArray(a, Benchmark) {
//2
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a[0].length; j++) {
if (a[i][j] >= Benchmark) {
//edge 라고 판단하면 빨간색
//a[i][j] = 0xff0000ff;
a[i][j] = 0xff0000ff;
} else {
//edge 가 아니면 파란색
//a[i][j] = 0x0000ffff;
//0x : 16진수라는 의미
//R, G, B, 투명도
a[i][j] = 0xffffff00;
}
}
}
return a;
}
edgeDetected(3, 20);