-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(image): Implement template matching #99
* Initial template matching logic using openCV * Test using pokemon cheat logo * Test using an "exact" selection from a stock image https://unsplash.com/photos/J2RQT7kJSMM
- Loading branch information
Showing
6 changed files
with
289 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {describe, it} from 'mocha'; | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import express, {Request, Response} from "express"; | ||
import {formatNumber, resolvePath, sleep} from "../src/util"; | ||
import {pathToFileURL, URL} from "url"; | ||
import ImageData from "../src/Common/ImageData"; | ||
import * as cvTypes from '@u4/opencv4nodejs' | ||
import {getCV, TemplateCompare} from "../src/Common/OpenCVService"; | ||
import winston from 'winston'; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
const assert = chai.assert; | ||
|
||
const star = pathToFileURL(resolvePath('./tests/assets/star-transparent.png', './')); | ||
const starInside = pathToFileURL(resolvePath('./tests/assets/star-inside.png', './')); | ||
const tran = pathToFileURL(resolvePath('./tests/assets/tran.jpg', './')); | ||
const tranSel = pathToFileURL(resolvePath('./tests/assets/tran-selection.jpg', './')); | ||
|
||
describe('Template Matching', function () { | ||
|
||
let cv: typeof cvTypes.cv; | ||
|
||
before(async () => { | ||
cv = await getCV(); | ||
}); | ||
|
||
it('matches a standard example', async function () { | ||
|
||
const templateMatch = new TemplateCompare(cv, winston.loggers.get('app')); | ||
|
||
await templateMatch.setTemplate(new ImageData({path: tranSel})); | ||
|
||
const [passed, results] = await templateMatch.matchImage(new ImageData({ | ||
path: tran | ||
}), 'template'); | ||
|
||
if(results.matchRec !== undefined) { | ||
const src = cv.imread(tran.pathname); | ||
src.drawRectangle( | ||
results.matchRec, | ||
new cv.Vec3(0, 255, 0), | ||
2, | ||
cv.LINE_8 | ||
); | ||
// TODO mask is not drawn correctly (its above?) | ||
cv.imwrite(pathToFileURL(resolvePath(`./tests/assets/tran-masked.jpg`, './')).pathname, src); | ||
} | ||
|
||
assert.isTrue(passed); | ||
}); | ||
|
||
it('matches a template using service', async function () { | ||
|
||
const templateMatch = new TemplateCompare(cv, winston.loggers.get('app')); | ||
|
||
await templateMatch.setTemplate(new ImageData({path: star})); | ||
|
||
const [passed, results] = await templateMatch.matchImage(new ImageData({ | ||
path: starInside | ||
}), 'template', 0.2); | ||
|
||
if(results.matchRec !== undefined) { | ||
const src = cv.imread(starInside.pathname); | ||
src.drawRectangle( | ||
results.matchRec, | ||
new cv.Vec3(0, 255, 0), | ||
2, | ||
cv.LINE_8 | ||
); | ||
cv.imwrite(pathToFileURL(resolvePath(`./tests/assets/star-masked.jpg`, './')).pathname, src); | ||
} | ||
}); | ||
}); |