-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW6.js
96 lines (86 loc) · 2.48 KB
/
HW6.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
'use strict'
const cv = require('opencv')
const utils = require('./utils')
const fs = require('fs')
function scaleDown (inputMat) {
let result = new cv.Matrix(64, 64, cv.Constants.CV_8U)
for (let i = 0; i < result.height(); i++) { // row
for (let j = 0; j < result.width(); j++) { // column
let value = inputMat.pixelValueAt(i * 8, j * 8)
result.pixel(i, j, [value, value, value])
}
}
return result
}
function h (b, c, d, e) {
if (b === c && (d !== b || e !== b)) {
return 'q'
} else if (b === c && (d === b || e === b)) {
return 'r'
} else if (b !== c) {
return 's'
}
}
function f (a1, a2, a3, a4) {
let neighbors = [a1, a2, a3, a4]
var rlength = neighbors.filter(function (value) { return value === 'r' }).length
if (neighbors.length === rlength) {
return 5
} else {
return neighbors.filter(function (value) { return value === 'q' }).length
}
}
function yokoiConnect (neighbors) {
return f(
h(neighbors[0], neighbors[1], neighbors[6], neighbors[2]),
h(neighbors[0], neighbors[2], neighbors[7], neighbors[3]),
h(neighbors[0], neighbors[3], neighbors[8], neighbors[4]),
h(neighbors[0], neighbors[4], neighbors[5], neighbors[1])
)
}
/*
[7][2][6]
[3][0][1]
[8][4][5]
*/
function getNeighbors (mat, col, row) {
let neighbors = [
mat.pixelValueAt(row, col),
mat.pixelValueAt(row, col + 1),
mat.pixelValueAt(row - 1, col),
mat.pixelValueAt(row, col - 1),
mat.pixelValueAt(row + 1, col),
mat.pixelValueAt(row + 1, col + 1),
mat.pixelValueAt(row - 1, col + 1),
mat.pixelValueAt(row - 1, col - 1),
mat.pixelValueAt(row + 1, col - 1)
]
return neighbors
}
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
}
inputMat = scaleDown(inputMat)
inputMat = utils.binarized(inputMat)
inputMat.save('./output/HW6/HW6_lena64.bmp')
let stream = fs.createWriteStream('./output/HW6/HW6_Yokoi.txt')
for (let i = 0; i < inputMat.height(); i++) { // row
let out = ''
for (let j = 0; j < inputMat.width(); j++) { // column
let value = yokoiConnect(getNeighbors(inputMat, i, j))
if (inputMat.pixelValueAt(i, j) === 255) {
out += value
} else {
out += ' '
}
}
stream.write(out + '\n')
console.log(out)
}
})
}
main()