Comments (5)
This is how I did it (and it seems to be working ok).
In jscanify.js...
Change this:
extractPaper(image, resultWidth, resultHeight, cornerPoints) {
To this:
extractPaper(image, maxSize, cornerPoints) {
Then after this:
let warpedDst = new cv.Mat();
Insert this:
// Calculate "width" and "height" of the quadrilateral
const widthTop = Math.sqrt(Math.pow(topRightCorner.x - topLeftCorner.x, 2) + Math.pow(topRightCorner.y - topLeftCorner.y, 2));
const widthBottom = Math.sqrt(Math.pow(bottomRightCorner.x - bottomLeftCorner.x, 2) + Math.pow(bottomRightCorner.y - bottomLeftCorner.y, 2));
const heightLeft = Math.sqrt(Math.pow(topLeftCorner.x - bottomLeftCorner.x, 2) + Math.pow(topLeftCorner.y - bottomLeftCorner.y, 2));
const heightRight = Math.sqrt(Math.pow(topRightCorner.x - bottomRightCorner.x, 2) + Math.pow(topRightCorner.y - bottomRightCorner.y, 2));
// Average the widths and heights to account for potential skew
const avgWidth = (widthTop + widthBottom) / 2;
const avgHeight = (heightLeft + heightRight) / 2;
// Determine aspect ratio
const aspectRatio = avgWidth / avgHeight;
// Calculate output dimensions based on aspect ratio and max size
let resultWidth, resultHeight;
if (aspectRatio > 1) { // Landscape
resultWidth = maxSize;
resultHeight = Math.round(maxSize / aspectRatio);
} else { // Portrait or square
resultWidth = Math.round(maxSize * aspectRatio);
resultHeight = maxSize;
}
Before this:
let dsize = new cv.Size(resultWidth, resultHeight);
You would then use scanner.extractPaper(image, maxSize)
, where maxSize
is the largest side of your desired scanned image.
ps. ChatGPT generated it 🤣
from jscanify.
Yep, this solution looks like it might work too. If you want to avoid unnecessary complexity though, you could simply show your users a dialog, asking them "what paper size would you like the scan to be?"
from jscanify.
This would be very useful when scanning a variety of papers with unknown size - such as notes a customer gives you as a stack of random paper
from jscanify.
Really it would be most useful to me if the image was extracted and returned at its real dimensions. It can be scaled to a different size later as long as the result could be queried for true dimensions.
from jscanify.
Adding how you can achieve it since I ended up here in my initial search.
Here is how you determine capture the width & height to use for your ratio:
const img = cv.imread(imageEl);
const contour = scanner.findPaperContour(img);
console.log(contour);
const cornerPoints = scanner.getCornerPoints(contour);
console.log(cornerPoints);
const topWidth = cornerPoints.topRightCorner.x - cornerPoints.topLeftCorner.x;
const bottomWidth = cornerPoints.bottomRightCorner.x - cornerPoints.bottomLeftCorner.x;
const leftHeight = cornerPoints.bottomLeftCorner.y - cornerPoints.topLeftCorner.y;
const rightHeight = cornerPoints.bottomRightCorner.y - cornerPoints.topRightCorner.y;
console.log(topWidth);
console.log(bottomWidth);
console.log(leftHeight);
console.log(rightHeight);
const width = Math.max(topWidth, bottomWidth);
const height = Math.max(leftHeight, rightHeight);
console.log(width);
console.log(height);
const resultCanvas = scanner.extractPaper(imageEl, width, height);
The final width and height is at the ratio to scale
from jscanify.
Related Issues (20)
- Documentation implies findPaperContour takes HTML image input HOT 1
- There is no support for Typescript :( HOT 3
- Do you have the document detection in real time while using the camera to get the photo through MediaCapture API ? HOT 5
- WebRTC is very complicated for photos HOT 5
- Any way to use this library in Angular? HOT 3
- [ Demo Vison ] Update to work with User Input image HOT 1
- IOS Compatibility HOT 27
- Improvements to the recognition algorithm
- Poor Image Quality HOT 7
- Fails to install with Python 3.12 HOT 1
- extractPaper not working with cluttered background HOT 2
- Highlighted image does not show HOT 12
- cv.approxPolyDP maxContour. HOT 2
- Error importing jscanify library in React application HOT 2
- Passing a number "-1" from JS side to C/C++ side to an argument of type "unsigned long" HOT 1
- Calling getCornerPoints throws an Uncaught (in promise) 23302432 HOT 2
- React js import support? HOT 3
- Doesn't work in web (React) HOT 3
- Does not work inside vue.js or nuxt project HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jscanify.