From 4439c1a62524f949c258aa8e8801493c4511e883 Mon Sep 17 00:00:00 2001 From: ColonelParrot <65585002+ColonelParrot@users.noreply.github.com> Date: Thu, 13 Apr 2023 22:32:45 -0400 Subject: [PATCH] jscanify: reorganize --- src/jscanify.js | 119 ++++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/src/jscanify.js b/src/jscanify.js index a2a4c6d..ef17fe0 100644 --- a/src/jscanify.js +++ b/src/jscanify.js @@ -22,65 +22,6 @@ class jscanify { constructor() { } - /** - * Calculates the corner points of a contour. - * @param {*} contour contour from {@link findPaperContour} - * @returns object with properties `topLeftCorner`, `topRightCorner`, `bottomLeftCorner`, `bottomRightCorner`, each with `x` and `y` property - */ - getCornerPoints(contour) { - let rect = cv.minAreaRect(contour); - const center = rect.center; - - let topLeftCorner; - let topLeftCornerDist = 0; - - let topRightCorner; - let topRightCornerDist = 0; - - let bottomLeftCorner; - let bottomLeftCornerDist = 0; - - let bottomRightCorner; - let bottomRightCornerDist = 0; - - for (let i = 0; i < contour.data32S.length; i += 2) { - const point = { x: contour.data32S[i], y: contour.data32S[i + 1] }; - const dist = distance(point, center); - if (point.x < center.x && point.y > center.y) { - // top left - if (dist > topLeftCornerDist) { - topLeftCorner = point; - topLeftCornerDist = dist; - } - } else if (point.x > center.x && point.y > center.y) { - // top right - if (dist > topRightCornerDist) { - topRightCorner = point; - topRightCornerDist = dist; - } - } else if (point.x < center.x && point.y < center.y) { - // bottom left - if (dist > bottomLeftCornerDist) { - bottomLeftCorner = point; - bottomLeftCornerDist = dist; - } - } else if (point.x > center.x && point.y < center.y) { - // bottom right - if (dist > bottomRightCornerDist) { - bottomRightCorner = point; - bottomRightCornerDist = dist; - } - } - } - - return { - topLeftCorner, - topRightCorner, - bottomLeftCorner, - bottomRightCorner, - }; - } - /** * Finds the contour of the paper within the image * @param {*} img image to process @@ -265,6 +206,66 @@ onComplete(canvas); }; } + + /** + * Calculates the corner points of a contour. + * @param {*} contour contour from {@link findPaperContour} + * @returns object with properties `topLeftCorner`, `topRightCorner`, `bottomLeftCorner`, `bottomRightCorner`, each with `x` and `y` property + */ + getCornerPoints(contour) { + let rect = cv.minAreaRect(contour); + const center = rect.center; + + let topLeftCorner; + let topLeftCornerDist = 0; + + let topRightCorner; + let topRightCornerDist = 0; + + let bottomLeftCorner; + let bottomLeftCornerDist = 0; + + let bottomRightCorner; + let bottomRightCornerDist = 0; + + for (let i = 0; i < contour.data32S.length; i += 2) { + const point = { x: contour.data32S[i], y: contour.data32S[i + 1] }; + const dist = distance(point, center); + if (point.x < center.x && point.y > center.y) { + // top left + if (dist > topLeftCornerDist) { + topLeftCorner = point; + topLeftCornerDist = dist; + } + } else if (point.x > center.x && point.y > center.y) { + // top right + if (dist > topRightCornerDist) { + topRightCorner = point; + topRightCornerDist = dist; + } + } else if (point.x < center.x && point.y < center.y) { + // bottom left + if (dist > bottomLeftCornerDist) { + bottomLeftCorner = point; + bottomLeftCornerDist = dist; + } + } else if (point.x > center.x && point.y < center.y) { + // bottom right + if (dist > bottomRightCornerDist) { + bottomRightCorner = point; + bottomRightCornerDist = dist; + } + } + } + + return { + topLeftCorner, + topRightCorner, + bottomLeftCorner, + bottomRightCorner, + }; + } + } if (typeof module !== "undefined") {