Add null check in extractPaper

This commit is contained in:
ColonelParrot 2025-02-01 21:06:30 -05:00
parent e2af210cf2
commit c8e6ab035d
2 changed files with 16 additions and 3 deletions

View File

@ -141,6 +141,9 @@ class jscanify {
/** /**
* Extracts and undistorts the image detected within the frame. * Extracts and undistorts the image detected within the frame.
*
* Returns `null` if no paper is detected.
*
* @param {*} image image to process * @param {*} image image to process
* @param {*} resultWidth desired result paper width * @param {*} resultWidth desired result paper width
* @param {*} resultHeight desired result paper height * @param {*} resultHeight desired result paper height
@ -151,6 +154,11 @@ class jscanify {
const canvas = createCanvas(); const canvas = createCanvas();
const img = cv.imread(image); const img = cv.imread(image);
const maxContour = this.findPaperContour(img); const maxContour = this.findPaperContour(img);
if(maxContour == null){
return null;
}
const { const {
topLeftCorner, topLeftCorner,
topRightCorner, topRightCorner,

View File

@ -132,7 +132,10 @@
/** /**
* Extracts and undistorts the image detected within the frame. * Extracts and undistorts the image detected within the frame.
* @param {*} image image to process *
* Returns `null` if no paper is detected.
*
* @param {*} image image to process
* @param {*} resultWidth desired result paper width * @param {*} resultWidth desired result paper width
* @param {*} resultHeight desired result paper height * @param {*} resultHeight desired result paper height
* @param {*} cornerPoints optional custom corner points, in case automatic corner points are incorrect * @param {*} cornerPoints optional custom corner points, in case automatic corner points are incorrect
@ -140,11 +143,13 @@
*/ */
extractPaper(image, resultWidth, resultHeight, cornerPoints) { extractPaper(image, resultWidth, resultHeight, cornerPoints) {
const canvas = document.createElement("canvas"); const canvas = document.createElement("canvas");
const img = cv.imread(image); const img = cv.imread(image);
const maxContour = this.findPaperContour(img); const maxContour = this.findPaperContour(img);
if(maxContour == null){
return null;
}
const { const {
topLeftCorner, topLeftCorner,
topRightCorner, topRightCorner,