-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanny.js
156 lines (132 loc) · 3.92 KB
/
canny.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
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
const { Worker } = require('worker_threads')
/**
* Some boilerplate for running threads
* @param {Any} workerData
*/
function runWorker(workerData) {
return new Promise((resolve, reject) => {
const worker = new Worker('./worker.js', { workerData });
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
})
})
}
const defaultOptions = {
lowThreshold: 10,
highThreshold: 30,
gaussianBlur: 1.1
};
const Gx = [
[-1, 0, +1],
[-2, 0, +2],
[-1, 0, +1]
];
const Gy = [
[-1, -2, -1],
[0, 0, 0],
[+1, +2, +1]
];
const convOptions = {
bitDepth: 32,
mode: 'periodic'
};
function cannyEdgeDetector(image, options) {
image.checkProcessable('Canny edge detector', {
bitDepth: 8,
channels: 1,
components: 1
});
options = Object.assign({}, defaultOptions, options);
const width = image.width;
const height = image.height;
const brightness = image.maxValue;
const gfOptions = {
sigma: options.gaussianBlur,
radius: 3
};
const gf = image.gaussianFilter(gfOptions);
const gradientX = gf.convolution(Gy, convOptions);
const gradientY = gf.convolution(Gx, convOptions);
const G = gradientY.hypotenuse(gradientX);
const Image = image.constructor;
const nms = new Image(width, height, {
kind: 'GREY',
bitDepth: 32
});
const edges = new Image(width, height, {
kind: 'GREY',
bitDepth: 32
});
const finalImage = new Image(width, height, {
kind: 'GREY'
});
// Non-Maximum supression
for (var i = 1; i < width - 1; i++) {
for (var j = 1; j < height - 1; j++) {
var dir = (Math.round(Math.atan2(gradientY.getValueXY(i, j, 0), gradientX.getValueXY(i, j, 0)) * (5.0 / Math.PI)) + 5) % 5;
if (
!((dir === 0 && (G.getValueXY(i, j, 0) <= G.getValueXY(i, j - 1, 0) || G.getValueXY(i, j, 0) <= G.getValueXY(i, j + 1, 0)))
|| (dir === 1 && (G.getValueXY(i, j, 0) <= G.getValueXY(i - 1, j + 1, 0) || G.getValueXY(i, j, 0) <= G.getValueXY(i + 1, j - 1, 0)))
|| (dir === 2 && (G.getValueXY(i, j, 0) <= G.getValueXY(i - 1, j, 0) || G.getValueXY(i, j, 0) <= G.getValueXY(i + 1, j, 0)))
|| (dir === 3 && (G.getValueXY(i, j, 0) <= G.getValueXY(i - 1, j - 1, 0) || G.getValueXY(i, j, 0) <= G.getValueXY(i + 1, j + 1, 0))))
) {
nms.setValueXY(i, j, 0, G.getValueXY(i, j, 0));
}
}
}
for (i = 0; i < width * height; ++i) {
var currentNms = nms.data[i];
var currentEdge = 0;
if (currentNms > options.highThreshold) {
currentEdge++;
finalImage.data[i] = brightness;
}
if (currentNms > options.lowThreshold) {
currentEdge++;
}
edges.data[i] = currentEdge;
}
// Hysteresis: first pass
var currentPixels = [];
for (i = 1; i < width - 1; ++i) {
for (j = 1; j < height - 1; ++j) {
if (edges.getValueXY(i, j, 0) !== 1) {
continue;
}
outer: for (var k = i - 1; k < i + 2; ++k) {
for (var l = j - 1; l < j + 2; ++l) {
if (edges.getValueXY(k, l, 0) === 2) {
currentPixels.push([i, j]);
finalImage.setValueXY(i, j, 0, brightness);
break outer;
}
}
}
}
}
// Hysteresis: second pass
while (currentPixels.length > 0) {
var newPixels = [];
for (i = 0; i < currentPixels.length; ++i) {
for (j = -1; j < 2; ++j) {
for (k = -1; k < 2; ++k) {
if (j === 0 && k === 0) {
continue;
}
var row = currentPixels[i][0] + j;
var col = currentPixels[i][1] + k;
if (edges.getValueXY(row, col, 0) === 1 && finalImage.getValueXY(row, col, 0) === 0) {
newPixels.push([row, col]);
finalImage.setValueXY(row, col, 0, brightness);
}
}
}
}
currentPixels = newPixels;
}
return finalImage;
}
module.exports = cannyEdgeDetector;