Code Monkey home page Code Monkey logo

Comments (5)

TomAtRichfords avatar TomAtRichfords commented on July 17, 2024

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.

ColonelParrot avatar ColonelParrot commented on July 17, 2024

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.

thenewguy avatar thenewguy commented on July 17, 2024

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.

thenewguy avatar thenewguy commented on July 17, 2024

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.

thenewguy avatar thenewguy commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.