mirror of
https://github.com/ColonelParrot/jscanify.git
synced 2025-12-30 22:31:52 +00:00
jscanify: reorganize
This commit is contained in:
parent
0332acfb90
commit
4439c1a625
119
src/jscanify.js
119
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") {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user