-
Notifications
You must be signed in to change notification settings - Fork 1
/
findImage.js
143 lines (134 loc) · 4.77 KB
/
findImage.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
/*
* @Author: moushicheng [email protected]
* @Date: 2022-05-24 00:09:04
* @LastEditors: moushicheng [email protected]
* @LastEditTime: 2022-05-25 16:57:51
* @FilePath: \findImage\findImage.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
const images = require("images");
// const comparer = require("./compareSimilarity");
const {compareSimilarity,comparer} = require("./compareSimilarity");
class imageFinder {
constructor(fatherNode, sonNode, options = {}) {
this.comparer=new comparer()
if(fatherNode){
this.fatherNode = fatherNode
}
if(sonNode){
this.setSon(sonNode)
}
this.options = options;
this.startPoint = options.startPoint ? options.startPoint : [0, 0];
this.endPoint = options.endPoint ? options.endPoint : [this.fatherNode.width, this.fatherNode.height]
this.precision=options.precision?options.precision:0.9
}
find() {
for (let i = this.startPoint[1]; i < this.endPoint[1]; i++) {
for (let j = this.startPoint[0]; j < this.endPoint[0]; j++) {
if (this.isSameCorner(j, i)) {
// const result = compareSimilarity(
// this.getFatherBufferInSonSize(j, i),
// this.sonNode.data
// )
// console.log(result);
const fatherHash=this.comparer.setHash({
data:this.getFatherBufferInSonSize(j, i),
width:this.sonNode.width,
height:this.sonNode.height
});
const sonHash=this.sonNode['hash']
const length=this.sonNode.data.length;
const result=(length-this.comparer.hamming(fatherHash,sonHash))/ length
if (result > this.precision) {
return {
x: j,
y: i
}
}
}
}
}
return 'no find'
}
getFatherBufferInSonSize(x, y) {
const data = this.fatherNode.data
const fatherArea = []
let z = 0;
for (let i = y; i < y + this.sonNode.height; i++) {
for (let j = x * 4; j < (x + this.sonNode.width) * 4; j++) {
fatherArea[z++] = data[i * this.fatherNode.width * 4 + j]
}
}
return fatherArea
}
isSameCorner(x, y) {
//超出边界则退出
if (x > this.fatherNode.width - this.sonNode.width || y > this.fatherNode.height - this.sonNode.height) {
return false;
}
const right = (this.sonNode.width - 1) * 4;
const bottom = ((this.sonNode.height - 1) * this.fatherNode.width) * 4
const middleBottom = Math.floor(this.sonNode.height / 2) * this.fatherNode.width * 4
const offset = x * 4 + (y * this.fatherNode.width) * 4; //offsetX+offsetY
if (this.comparePoint(offset, 0)) return false;
if (this.comparePoint(offset + right, 1)) return false;
if (this.comparePoint(offset + right + bottom, 2)) return false;
if (this.comparePoint(offset + bottom, 3)) return false;
if (this.comparePoint(offset + middleBottom + Math.floor(this.sonNode.width / 2) * 4, 4)) return false;
return true;
}
comparePoint(offset, i) {
const fatherData = this.fatherNode.data;
return fatherData[offset] != this.sonNode.conner[i][0] ||
fatherData[offset + 1] != this.sonNode.conner[i][1] ||
fatherData[offset + 2] != this.sonNode.conner[i][2] ||
fatherData[offset + 3] != this.sonNode.conner[i][3]
}
setSon(node){
this.sonNode = node;
this.setSonConner();
this.comparer.setHash(node);
}
setSonConner() {
const width = this.sonNode.width;
const height = this.sonNode.height;
const data = this.sonNode.data;
const right = (width - 1) * 4;
const bottom = ((height - 1) * width) * 4;
const middleBottom = Math.floor(height / 2) * width * 4
this.sonNode.conner = [
getPoints(0), //左上角
getPoints(right), //右上角
getPoints(bottom + right), //右下角
getPoints(bottom), //左下角
getPoints(middleBottom + Math.floor(width / 2) * 4)//中间点
]
function getPoints(offset) {
return [data[offset], data[offset + 1], data[offset + 2], data[offset + 3]]
}
}
print(data,i,j){
for(let k=i;k<j;k+=4){
console.log(data[k],data[k+1],data[k+2],data[k+3]);
}
}
}
module.exports = {
findImage: function findImage(fatherImageSrc, sonImageSrc, options) {
const fatherImage = images(fatherImageSrc);
const sonImage = images(sonImageSrc)
const fatherData = fatherImage.encode("raw");
const sonData = sonImage.encode("raw");
return new imageFinder({
data: fatherData.slice(12),
width: fatherImage.width(),
height: fatherImage.height(),
}, {
data: sonData.slice(12),
width: sonImage.width(),
height: sonImage.height(),
}, options).find()
},
imageFinder: imageFinder
}