-
Notifications
You must be signed in to change notification settings - Fork 2
/
processing.js
175 lines (154 loc) · 3.9 KB
/
processing.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class Steps {
constructor (el) {
this.el = el
this.root = null
this.last = null
}
addStep (step) {
if ( this.root == null) {
this.root = step
this.last = step
} else {
this.last.next = step
step.prev = this.last
this.last = step
}
this.el.appendChild(step.div)
}
run () {
this.root.run()
}
}
class Step {
constructor (params, src) {
this.next = null
this.prev = null
this.rendered = null
this.alt_src = src
this.div = document.createElement('div')
this.canvas = document.createElement('canvas')
this.div.appendChild(this.canvas)
this.inputs = []
for (var key in params){
var input = Object.assign(document.createElement('input'), params[key])
var label = document.createElement('label')
input.addEventListener('change', this.run.bind(this))
label.innerHTML = key
this.div.appendChild(label)
this.div.appendChild(input)
this.inputs.push(input)
}
this.run = this.run.bind(this)
}
get src () {
return this.alt_src ? this.alt_src : this.prev.canvas
}
run () {
this.process()
this.show()
if (this.next != null){
this.next.run()
}
}
process () {}
show() {
cv.imshow(this.canvas, this.rendered)
this.rendered.delete()
}
get_value (input) {
if (input.type === 'number' || input.type === 'range'){
return parseInt(input.value)
} else if (input.type === 'checkbox' ) {
return input.checked
}
}
}
class BlurStep extends Step {
constructor(src) {
let params = {
'blurDiameter': { value: 8, max:20, min:0, type: 'range' },
'sigmaColor': { value: 250, type:'number' },
'sigmaSpace': { value: 250, type:'number' }
}
super(params, src)
}
process() {
let src = cv.imread(this.src)
this.rendered = new cv.Mat()
cv.cvtColor(src, src, cv.COLOR_RGBA2RGB, 0)
cv.bilateralFilter(
src, this.rendered,
...this.inputs.map(this.get_value),
cv.BORDER_DEFAULT
)
src.delete()
}
}
class MaskStep extends Step {
constructor(src) {
let params = {
'thresh': { value: 190, type: 'number' },
'maxval': { value: 255, type:'number' }
}
super(params, src)
}
process () {
let src = cv.imread(this.src)
this.rendered = new cv.Mat()
cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
cv.threshold(
src, this.rendered,
...this.inputs.map(this.get_value),
cv.THRESH_BINARY)
src.delete()
}
}
class CannyStep extends Step {
constructor(src) {
let params = {
'lowThresh': { value: 100000, type: 'number' },
'highThresh': { value: 200000, type:'number' },
'apertureSize': { value: 7, min: 3, max: 7, step: 2, type:'range' },
'L2gradient': { type:'checkbox' }
}
super(params, src)
}
process () {
let src = cv.imread(this.src)
this.rendered = new cv.Mat()
cv.Canny(
src, this.rendered,
...this.inputs.map(this.get_value)
)
src.delete()
}
}
class HoughPStep extends Step {
constructor(src) {
let params = {
'houghThreshold': { value: 115, type: 'number' },
'minLineLength': { value: 200, type:'number' },
'maxLineGap': { value: 240, type:'number' },
}
super(params, src)
}
process () {
let src = cv.imread(this.src)
this.rendered = cv.imread(this.src)
let lines = new cv.Mat()
let color = new cv.Scalar(255, 255, 0)
cv.cvtColor(src, src, cv.COLOR_RGB2GRAY, 0)
cv.HoughLinesP(
src, lines,
1, Math.PI / 180,
...this.inputs.map(this.get_value)
)
for (let i = 0; i < lines.rows; ++i) {
let startPoint = new cv.Point(lines.data32S[i * 4], lines.data32S[i * 4 + 1]);
let endPoint = new cv.Point(lines.data32S[i * 4 + 2], lines.data32S[i * 4 + 3]);
cv.line(this.rendered, startPoint, endPoint, color, 3);
}
src.delete()
lines.delete()
}
}