Search code examples
iosswiftcore-graphicscore-imageapple-vision

Warp quadrangle with for corner points to rectangular 2D image with given aspect ratio


I want to build a business card scanner with Vision API. So I'm performing a VNDetectRectanglesRequest with specific rectangle constraints for the business card format (which has a fixed ratio).

As a result of this I'm getting a VNRectangleObservation object back which contains the for corner points for the rectangle. This is mostly a rectangle in 3D space but just a quadrangle in 2D because the physical object can be captured slightly from the side for example.

Now I want to cut out the image with those 4 corner points and warp/distort it into a flat rectangular like the iOS notes app does it when you capture a document.

enter image description here

I don't need any AR like prediction of the document aspect ratio as it is fixed (by the business card format).

How can I do this in Swift using internal libraries e.g. CoreGraphics or CoreImage?


Solution

  • Okay I found this very similar question with a excellent answer pointing to CIPerspectiveCorrection which does exactly what I need: Accepting an image and for points to transform it to a 2D rectangle.

    I won't delete my question because the titles are very different and my question could lead people to the solution :].