Prospective correction of UIImage from points

I am working on an application where I let the user take a picture, such as a business card or photo.

Then the user will mark the four corners of the object (which took the picture). As seen in many document / image / business card scanning applications:

enter image description here

My question is: how do I crop and fix perspective according to these four points? I searched for several days and did not look at several image processing libraries.

Anyone who can point me in the right direction?

+6
source share
4 answers

From iOS8 + there is a filter for the main image called CIPerspectiveCorrection . All you have to do is transfer the image and the four dots. Perspective correction

There is also another filter that supports iOS6 +, called CIPerspectiveTransform , which can be used in a similar way (image skew).

+1
source

If this image was loaded as a texture, it would be very simple to skew it using OpenGL. You literally just draw a full-screen square and use the yellow correction points as the UV coordinate at each point.

+1
source

I'm not sure if you have already tried the Opencv library, but it has a very good way to place an image. I have a small fragment that takes a series of corners, for example, your four corners and the final size to display it.

You can read the warpPerspective man page on the OpenCV site .

cv::Mat deskew(cv::Mat& capturedFrame, cv::Point2f source_points[], cv::Size finalSize) { cv::Point2f dest_points[4]; // Output of deskew operation has same color space as source frame, but // is proportional to the area the document occupied; this is to reduce // blur effects from a scaling component. cv::Mat deskewedMat = cv::Mat(finalSize, capturedFrame.type()); cv::Size s = capturedFrame.size(); // Deskew to full output image corners dest_points[0] = cv::Point2f(0,s.height); // lower left dest_points[1] = cv::Point2f(0,0); // upper left dest_points[2] = cv::Point2f(s.width,0); // upper right dest_points[3] = cv::Point2f(s.width,s.height); // lower right // Build quandrangle "de-skew" transform matrix values cv::Mat transform = cv::getPerspectiveTransform( source_points, dest_points ); // Apply the deskew transform cv::warpPerspective( capturedFrame, deskewedMat, transform, s, cv::INTER_CUBIC ); return deskewedMat; } 
+1
source

I do not know the exact solution to your case, but there is an approach to the trapezoid: http://www.comp.nus.edu.sg/~tants/tsm/TSM_recipe.html - the idea is to continuously build the transformation matrix. Theoretically, you can add a transform that converts your shape to trapecy.

And there are many such questions: https://math.stackexchange.com/questions/13404/mapping-irregular-quadrilateral-to-a-rectangle , but I have not tested the solution.

0
source

Source: https://habr.com/ru/post/902164/


All Articles