从点对 UIImage 进行透视校正

发布于 2024-12-17 16:00:40 字数 242 浏览 2 评论 0原文

我正在开发一个应用程序,让用户拍照,例如名片或照片。

然后,用户将标记对象的四个角(他们将其拍下来) - 就像在许多文档/图像/名片扫描应用程序中看到的那样:

在此处输入图像描述

我的问题是如何根据这四点裁剪并修复视角?我已经搜索了好几天并查看了几个图像处理库,但没有任何运气。

谁能指出我正确的方向?

I'm working on a app where I'll let the user take a picture e.g of a business card or photograph.

The user will then mark the four corners of the object (which they took a picture off) - Like it is seen in a lot of document/image/business card scanning apps:

enter image description here

My question is how do i crop and fix the perspective according to these four points? I've been searching for days and looked at several image proccessing libraries without any luck.

Any one who can point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情未る 2024-12-24 16:00:40

从 iOS8+ 开始,核心图像过滤器名为 CIPerspectiveCorrection。您所需要做的就是传递图像和四​​个点。Perspective Correction

另外还有一个支持 iOS6+ 的过滤器,称为 CIPerspectiveTransform 可以以类似的方式使用(倾斜图像)。

From iOS8+ there is Filter for Core Image called CIPerspectiveCorrection. All you need to do is pass the image and four points.Perspective Correction

Also there is one more filter supporting iOS6+ called CIPerspectiveTransform which can be used in similar way (skewing image).

落日海湾 2024-12-24 16:00:40

如果将此图像作为纹理加载,则使用 OpenGL 对其进行倾斜将非常简单。实际上,您只需绘制一个全屏四边形并使用黄色校正点作为每个点的 UV 坐标。

If this image were loaded in as a texture, it'd be extremely simple to skew it using OpenGL. You'd literally just draw a full-screen quad and use the yellow correction points as the UV coordinate at each point.

苍风燃霜 2024-12-24 16:00:40

我不确定您是否已经尝试过 Opencv 库,但它有一个非常好的方法来校正图像。我这里有一个小片段,它需要一系列角,例如四个角,以及将其映射到的最终尺寸。

您可以阅读 warpPerspective 的手册页 OpenCV 网站

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;
}

I'm not sure if you've tried the Opencv library yet, but it has a very nice way to deskew an image. I've got here a small snippet that takes an array of corners, your four corners for example, and a final size to map it into.

You can read the man page for warpPerspective 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;
}
绝情姑娘 2024-12-24 16:00:40

我不知道您的情况的确切解决方案,但有梯形的方法: http://www.comp.nus.edu.sg/~tants/tsm/TSM_recipe.html - 这个想法是不断构建变换矩阵。理论上,您可以添加将形状转换为梯形的变换。

还有很多这样的问题: https://math.stackexchange.com/ questions/13404/mapping-irregular-quadriside-to-a-rectangle ,但我没有检查解决方案。

I don't know exact solution of your case, but there is approach for trapezoid: http://www.comp.nus.edu.sg/~tants/tsm/TSM_recipe.html - the idea is to continuously build transformation matrix. Theoretically you can add transformation that converts your shape into trapecy.

And there are many questions like this: https://math.stackexchange.com/questions/13404/mapping-irregular-quadrilateral-to-a-rectangle , but I didn't check solutions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文