-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW4.js
181 lines (147 loc) · 4.76 KB
/
HW4.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
176
177
178
179
180
181
'use strict'
const cv = require('opencv')
const utils = require('./utils')
const whitePixel = [255, 255, 255]
const blackPixel = [0, 0, 0]
function Kernel (kernel, origin) {
this.kernel = kernel
this.origin = origin
this.points = []
// array[y][x], y is the first array, x is the second.
for (let y = 0; y < kernel.length; y++) {
for (let x = 0; x < kernel[y].length; x++) {
if (kernel[y][x] === 1) {
this.points.push({x: (x - origin.x), y: (y - origin.y)})
}
}
}
return this
}
function applyDilation (inputMat, kernel) {
let result = inputMat.copy()
for (let i = 0; i < inputMat.height(); i++) { // row
for (let j = 0; j < inputMat.width(); j++) { // column
if (inputMat.pixelValueAt(i, j)) {
for (let kernelIndex = 0; kernelIndex < kernel.points.length; kernelIndex++) {
let x = j + kernel.points[kernelIndex].x
let y = i + kernel.points[kernelIndex].y
if (x >= 0 && x <= inputMat.width() && y >= 0 && y <= inputMat.height()) {
result.pixel(y, x, whitePixel)
}
}
}
}
}
return result
}
function applyErosion (inputMat, kernel) {
let result = inputMat.copy()
for (let i = 0; i < inputMat.height(); i++) { // row
for (let j = 0; j < inputMat.width(); j++) { // column
let erosion = true
for (let kernelIndex = 0; kernelIndex < kernel.points.length; kernelIndex++) {
let x = j + kernel.points[kernelIndex].x
let y = i + kernel.points[kernelIndex].y
if (x >= 0 && x <= inputMat.width() && y >= 0 && y <= inputMat.height()) {
// pixelValueAt(col, row)
if (!inputMat.pixelValueAt(y, x)) {
erosion = false
break
}
} else {
erosion = false
break
}
}
if (erosion) {
result.pixel(i, j, whitePixel)
} else {
result.pixel(i, j, blackPixel)
}
}
}
return result
}
function applyOpening (inputMat, kernel) {
return applyDilation(applyErosion(inputMat, kernel), kernel)
}
function applyClosing (inputMat, kernel) {
return applyErosion(applyDilation(inputMat, kernel), kernel)
}
function applyReverse (inputMat) {
let result = inputMat.copy()
for (let i = 0; i < inputMat.height(); i++) { // row
for (let j = 0; j < inputMat.width(); j++) { // column
if (inputMat.pixelValueAt(i, j)) {
result.pixel(i, j, blackPixel)
} else {
result.pixel(i, j, whitePixel)
}
}
}
return result
}
function intersect (matA, matB) {
let result = matA.copy()
for (let i = 0; i < matA.height(); i++) { // row
for (let j = 0; j < matA.width(); j++) { // column
if (matA.pixelValueAt(i, j) & matB.pixelValueAt(i, j)) {
result.pixel(i, j, whitePixel)
} else {
result.pixel(i, j, blackPixel)
}
}
}
return result
}
function applyHitAndMiss (inputMat, J, K) {
return intersect(applyErosion(inputMat, J), applyErosion(applyReverse(inputMat), K))
}
function main () {
// read lena image from the file system using opencv
cv.readImage(process.argv[2], function (err, inputMat) {
if (err || inputMat.height() === 0 || inputMat.width === 0) {
console.log('input image error!')
return
}
let dilationMat = inputMat.copy()
let erosionMat = inputMat.copy()
let openingMat = inputMat.copy()
let closingMat = inputMat.copy()
let hitAndMissMat = inputMat.copy()
let height = inputMat.height()
let width = inputMat.width()
console.log('input image = ', width, ' x ', height)
let octogonalKenel = [
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0]
] // origin is 2,2,
let L = [
[1, 1],
[0, 1]
]
// convert the input image to binary.
inputMat = utils.binarized(inputMat)
// inputMat.save('./output/HW4/HW4_binary.bmp')
dilationMat = applyDilation(inputMat, new Kernel(octogonalKenel, {x: 2, y: 2}))
dilationMat.save('./output/HW4/HW4_dilation.bmp')
erosionMat = applyErosion(inputMat, new Kernel(octogonalKenel, {x: 2, y: 2}))
erosionMat.save('./output/HW4/HW4_erosion.bmp')
openingMat = applyOpening(inputMat, new Kernel(octogonalKenel, {x: 2, y: 2}))
openingMat.save('./output/HW4/HW4_opening.bmp')
closingMat = applyClosing(inputMat, new Kernel(octogonalKenel, {x: 2, y: 2}))
closingMat.save('./output/HW4/HW4_closing.bmp')
let kernelJ = new Kernel(L, {x: 1, y: 0})
let kernelK = new Kernel(L, {x: 0, y: 1})
console.log(kernelJ)
console.log(kernelK)
hitAndMissMat = applyHitAndMiss(inputMat, kernelJ, kernelK)
hitAndMissMat.save('./output/HW4/HW4_hitAndMiss.bmp')
console.log('finished!')
utils.showMatrixOnWindow(hitAndMissMat)
})
}
main()