Account for corner points detection

This commit is contained in:
ColonelParrot 2023-05-10 11:05:07 -04:00
parent 9fe661d4e8
commit 7ff45d78ae

View File

@ -5,7 +5,7 @@
console.log("RUNNING JSCANIFY TESTS"); console.log("RUNNING JSCANIFY TESTS");
console.log("Warning: This may take a bit"); 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 { mkdirSync, writeFileSync, unlinkSync, existsSync } = require("fs");
const assert = require("assert"); const assert = require("assert");
@ -15,6 +15,7 @@ const path = require("path");
const outputPaths = { const outputPaths = {
highlight: __dirname + "/output/highlighted.jpg", highlight: __dirname + "/output/highlighted.jpg",
extracted: __dirname + "/output/extracted.jpg", extracted: __dirname + "/output/extracted.jpg",
cornerPoints: __dirname + "/output/corner_points.jpg",
}; };
const baseFolder = __dirname.replaceAll("\\", "/") + "/output/"; const baseFolder = __dirname.replaceAll("\\", "/") + "/output/";
@ -50,7 +51,7 @@ function test() {
scanner.loadOpenCV(function (cv) { scanner.loadOpenCV(function (cv) {
console.log("Finished loading OpenCV.js"); console.log("Finished loading OpenCV.js");
console.log("Writing test images to: " + baseFolder); console.log("Writing test images to: " + baseFolder);
describe("feature tests", function (done) { describe("feature tests", function () {
it("should highlight paper", function (done) { it("should highlight paper", function (done) {
const highlighted = scanner.highlightPaper(testImage); const highlighted = scanner.highlightPaper(testImage);
writeFileSync( writeFileSync(
@ -73,6 +74,41 @@ function test() {
done(); 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();
});
}); });
}); });
} }