From 7ff45d78ae1d5fb7dfc998a12cde6387a824bc3d Mon Sep 17 00:00:00 2001 From: ColonelParrot <65585002+ColonelParrot@users.noreply.github.com> Date: Wed, 10 May 2023 11:05:07 -0400 Subject: [PATCH] Account for corner points detection --- test/tests.js | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/test/tests.js b/test/tests.js index 90e8a00..706f35d 100644 --- a/test/tests.js +++ b/test/tests.js @@ -5,7 +5,7 @@ console.log("RUNNING JSCANIFY TESTS"); console.log("Warning: This may take a bit"); -const { loadImage } = require("canvas"); +const { loadImage, createCanvas } = require("canvas"); const { mkdirSync, writeFileSync, unlinkSync, existsSync } = require("fs"); const assert = require("assert"); @@ -15,6 +15,7 @@ const path = require("path"); const outputPaths = { highlight: __dirname + "/output/highlighted.jpg", extracted: __dirname + "/output/extracted.jpg", + cornerPoints: __dirname + "/output/corner_points.jpg", }; const baseFolder = __dirname.replaceAll("\\", "/") + "/output/"; @@ -50,7 +51,7 @@ function test() { scanner.loadOpenCV(function (cv) { console.log("Finished loading OpenCV.js"); console.log("Writing test images to: " + baseFolder); - describe("feature tests", function (done) { + describe("feature tests", function () { it("should highlight paper", function (done) { const highlighted = scanner.highlightPaper(testImage); writeFileSync( @@ -73,6 +74,41 @@ function test() { done(); }); }); + + it("should label corner points", function (done) { + const parsedImage = cv.imread(testImage); + const paperContour = scanner.findPaperContour(parsedImage); + const { + topLeftCorner, + topRightCorner, + bottomLeftCorner, + bottomRightCorner, + } = scanner.getCornerPoints(paperContour, testImage); + + const canvas = createCanvas(); + + cv.imshow(canvas, parsedImage); + const ctx = canvas.getContext("2d"); + const points = [ + { p: topLeftCorner, text: "top left corner" }, + { p: topRightCorner, text: "top right corner" }, + { p: bottomLeftCorner, text: "bottom left corner" }, + { p: bottomRightCorner, text: "bottom right corner" }, + ]; + ctx.fillStyle = "cyan"; + ctx.font = "25px serif"; + points.forEach(({ p: point, text }) => { + ctx.beginPath(); + ctx.arc(point.x, point.y, 15, 0, 2 * Math.PI, false); + ctx.fillText(text, point.x + 30, point.y) + ctx.fill(); + }); + + writeFileSync(outputPaths.cornerPoints, canvas.toBuffer("image/jpeg")); + + assert.ok(existsSync(outputPaths.cornerPoints)); + done(); + }); }); }); }