@@ -6,9 +6,14 @@ import { MatchResult } from "../../match-result.class";
6
6
import { Region } from "../../region.class" ;
7
7
import { ScaledMatchResult } from "../../scaled-match-result.class" ;
8
8
import { DataSource } from "./data-source.interface" ;
9
+ import { determineScaledSearchRegion } from "./determine-searchregion.function" ;
10
+ import { findEdges } from "./find-edges.function" ;
9
11
import { FinderInterface } from "./finder.interface" ;
10
12
import { ImageProcessor } from "./image-processor.class" ;
11
13
import { ImageReader } from "./image-reader.class" ;
14
+ import { matchImages } from "./match-image.function" ;
15
+ import { scaleImage } from "./scale-image.function" ;
16
+ import { scaleLocation } from "./scale-location.function" ;
12
17
13
18
const loadNeedle = async ( image : Image ) : Promise < cv . Mat > => {
14
19
if ( image . hasAlphaChannel ) {
@@ -32,36 +37,6 @@ const loadHaystack = async (matchRequest: MatchRequest): Promise<cv.Mat> => {
32
37
}
33
38
} ;
34
39
35
- const matchImages = async ( haystack : cv . Mat , needle : cv . Mat ) : Promise < MatchResult > => {
36
- const match = await haystack . matchTemplateAsync (
37
- needle ,
38
- cv . TM_SQDIFF_NORMED ,
39
- ) ;
40
- const minMax = await match . minMaxLocAsync ( ) ;
41
- return new MatchResult (
42
- 1.0 - minMax . minVal ,
43
- new Region (
44
- minMax . minLoc . x ,
45
- minMax . minLoc . y ,
46
- Math . min ( needle . cols , haystack . cols ) ,
47
- Math . min ( needle . rows , haystack . rows ) ,
48
- ) ,
49
- ) ;
50
- } ;
51
-
52
- const scaleImage = async ( image : cv . Mat , scaleFactor : number ) : Promise < cv . Mat > => {
53
- const scaledRows = Math . max ( Math . floor ( image . rows * scaleFactor ) , 1.0 ) ;
54
- const scaledCols = Math . max ( Math . floor ( image . cols * scaleFactor ) , 1.0 ) ;
55
- return image . resizeAsync ( scaledRows , scaledCols , 0 , 0 , cv . INTER_AREA ) ;
56
- } ;
57
-
58
- const determineScaledSearchRegion = ( matchRequest : MatchRequest ) : Region => {
59
- const searchRegion = matchRequest . searchRegion ;
60
- searchRegion . width *= matchRequest . haystack . pixelDensity . scaleX ;
61
- searchRegion . height *= matchRequest . haystack . pixelDensity . scaleY ;
62
- return searchRegion ;
63
- } ;
64
-
65
40
const debugImage = ( image : cv . Mat , filename : string , suffix ?: string ) => {
66
41
const parsedPath = path . parse ( filename ) ;
67
42
let fullFilename = parsedPath . name ;
@@ -81,35 +56,6 @@ const debugImage = (image: cv.Mat, filename: string, suffix?: string) => {
81
56
// Math.min(result.location.height, image.rows - result.location.top));
82
57
// debugImage(image.getRegion(roiRect), filename, suffix);
83
58
// };
84
- //
85
- // const findEdges = async (image: cv.Mat): Promise<cv.Mat> => {
86
- // const gray = await image.cvtColorAsync(cv.COLOR_BGR2GRAY);
87
- // return gray.cannyAsync(50, 200);
88
- // };
89
-
90
- const scaleSize = (
91
- result : Region ,
92
- scaleFactor : number ,
93
- ) : Region => {
94
- return new Region (
95
- result . left ,
96
- result . top ,
97
- result . width / scaleFactor ,
98
- result . height / scaleFactor ,
99
- ) ;
100
- } ;
101
-
102
- const scaleLocation = (
103
- result : Region ,
104
- scaleFactor : number ,
105
- ) : Region => {
106
- return new Region (
107
- result . left / scaleFactor ,
108
- result . top / scaleFactor ,
109
- result . width ,
110
- result . height ,
111
- ) ;
112
- } ;
113
59
114
60
const isValidSearch = ( needle : cv . Mat , haystack : cv . Mat ) : boolean => {
115
61
return ( needle . cols <= haystack . cols ) && ( needle . rows <= haystack . rows ) ;
@@ -140,6 +86,8 @@ export class TemplateMatchingFinder implements FinderInterface {
140
86
) ;
141
87
}
142
88
const haystack = await loadHaystack ( matchRequest ) ;
89
+ const edgeHaystack = await findEdges ( haystack ) ;
90
+ const edgeNeedle = await findEdges ( needle ) ;
143
91
144
92
if ( debug ) {
145
93
debugImage ( needle , "input_needle.png" ) ;
@@ -159,7 +107,7 @@ export class TemplateMatchingFinder implements FinderInterface {
159
107
)
160
108
) ;
161
109
}
162
- const matchResult = await matchImages ( haystack , needle ) ;
110
+ const matchResult = await matchImages ( edgeHaystack , edgeNeedle ) ;
163
111
return new ScaledMatchResult ( matchResult . confidence , currentScale , matchResult . location ) ;
164
112
}
165
113
) ;
@@ -178,14 +126,11 @@ export class TemplateMatchingFinder implements FinderInterface {
178
126
)
179
127
) ;
180
128
}
181
- const matchResult = await matchImages ( haystack , scaledNeedle ) ;
129
+ const matchResult = await matchImages ( edgeHaystack , await findEdges ( scaledNeedle ) ) ;
182
130
return new ScaledMatchResult (
183
131
matchResult . confidence ,
184
132
currentScale ,
185
- scaleSize (
186
- matchResult . location ,
187
- currentScale
188
- )
133
+ matchResult . location ,
189
134
) ;
190
135
}
191
136
) ;
@@ -203,7 +148,7 @@ export class TemplateMatchingFinder implements FinderInterface {
203
148
)
204
149
) ;
205
150
}
206
- const matchResult = await matchImages ( scaledHaystack , needle ) ;
151
+ const matchResult = await matchImages ( await findEdges ( scaledHaystack ) , edgeNeedle ) ;
207
152
return new ScaledMatchResult (
208
153
matchResult . confidence ,
209
154
currentScale ,
@@ -235,16 +180,14 @@ export class TemplateMatchingFinder implements FinderInterface {
235
180
try {
236
181
const matches = await this . findMatches ( matchRequest , debug ) ;
237
182
const potentialMatches = matches
238
- . filter ( match => match . confidence >= matchRequest . confidence )
239
- . sort ( ( first , second ) => first . scale - second . scale ) ;
183
+ . filter ( match => match . confidence >= matchRequest . confidence ) ;
240
184
if ( potentialMatches . length === 0 ) {
241
185
reject ( `Unable to locate ${ matchRequest . pathToNeedle } , no match!` ) ;
242
186
}
243
- resolve ( potentialMatches . pop ( ) ) ;
187
+ resolve ( potentialMatches [ 0 ] ) ;
244
188
} catch ( e ) {
245
189
reject ( e ) ;
246
190
}
247
191
} ) ;
248
192
}
249
-
250
193
}
0 commit comments