@@ -6,9 +6,14 @@ import { MatchResult } from "../../match-result.class";
66import { Region } from "../../region.class" ;
77import { ScaledMatchResult } from "../../scaled-match-result.class" ;
88import { DataSource } from "./data-source.interface" ;
9+ import { determineScaledSearchRegion } from "./determine-searchregion.function" ;
10+ import { findEdges } from "./find-edges.function" ;
911import { FinderInterface } from "./finder.interface" ;
1012import { ImageProcessor } from "./image-processor.class" ;
1113import { 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" ;
1217
1318const loadNeedle = async ( image : Image ) : Promise < cv . Mat > => {
1419 if ( image . hasAlphaChannel ) {
@@ -32,36 +37,6 @@ const loadHaystack = async (matchRequest: MatchRequest): Promise<cv.Mat> => {
3237 }
3338} ;
3439
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-
6540const debugImage = ( image : cv . Mat , filename : string , suffix ?: string ) => {
6641 const parsedPath = path . parse ( filename ) ;
6742 let fullFilename = parsedPath . name ;
@@ -81,35 +56,6 @@ const debugImage = (image: cv.Mat, filename: string, suffix?: string) => {
8156// Math.min(result.location.height, image.rows - result.location.top));
8257// debugImage(image.getRegion(roiRect), filename, suffix);
8358// };
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- } ;
11359
11460const isValidSearch = ( needle : cv . Mat , haystack : cv . Mat ) : boolean => {
11561 return ( needle . cols <= haystack . cols ) && ( needle . rows <= haystack . rows ) ;
@@ -140,6 +86,8 @@ export class TemplateMatchingFinder implements FinderInterface {
14086 ) ;
14187 }
14288 const haystack = await loadHaystack ( matchRequest ) ;
89+ const edgeHaystack = await findEdges ( haystack ) ;
90+ const edgeNeedle = await findEdges ( needle ) ;
14391
14492 if ( debug ) {
14593 debugImage ( needle , "input_needle.png" ) ;
@@ -159,7 +107,7 @@ export class TemplateMatchingFinder implements FinderInterface {
159107 )
160108 ) ;
161109 }
162- const matchResult = await matchImages ( haystack , needle ) ;
110+ const matchResult = await matchImages ( edgeHaystack , edgeNeedle ) ;
163111 return new ScaledMatchResult ( matchResult . confidence , currentScale , matchResult . location ) ;
164112 }
165113 ) ;
@@ -178,14 +126,11 @@ export class TemplateMatchingFinder implements FinderInterface {
178126 )
179127 ) ;
180128 }
181- const matchResult = await matchImages ( haystack , scaledNeedle ) ;
129+ const matchResult = await matchImages ( edgeHaystack , await findEdges ( scaledNeedle ) ) ;
182130 return new ScaledMatchResult (
183131 matchResult . confidence ,
184132 currentScale ,
185- scaleSize (
186- matchResult . location ,
187- currentScale
188- )
133+ matchResult . location ,
189134 ) ;
190135 }
191136 ) ;
@@ -203,7 +148,7 @@ export class TemplateMatchingFinder implements FinderInterface {
203148 )
204149 ) ;
205150 }
206- const matchResult = await matchImages ( scaledHaystack , needle ) ;
151+ const matchResult = await matchImages ( await findEdges ( scaledHaystack ) , edgeNeedle ) ;
207152 return new ScaledMatchResult (
208153 matchResult . confidence ,
209154 currentScale ,
@@ -235,16 +180,14 @@ export class TemplateMatchingFinder implements FinderInterface {
235180 try {
236181 const matches = await this . findMatches ( matchRequest , debug ) ;
237182 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 ) ;
240184 if ( potentialMatches . length === 0 ) {
241185 reject ( `Unable to locate ${ matchRequest . pathToNeedle } , no match!` ) ;
242186 }
243- resolve ( potentialMatches . pop ( ) ) ;
187+ resolve ( potentialMatches [ 0 ] ) ;
244188 } catch ( e ) {
245189 reject ( e ) ;
246190 }
247191 } ) ;
248192 }
249-
250193}
0 commit comments