【问题标题】:Perspective correction of imageviewimageview的透视校正
【发布时间】:2012-06-09 21:36:11
【问题描述】:

我正在开发一款应用,该应用需要对使用手机相机拍摄的照片应用透视失真校正。 拍摄照片后,想法是将其显示在图像视图上,并让用户标记文档的四个角(一张卡片、一张纸等),然后根据这些点应用校正。 这是我试图实现的一个例子:

http://1.bp.blogspot.com/-ro9hniPj52E/TkoM0kTlEnI/AAAAAAAAAbQ/c2R5VrgmC_w/s640/s4.jpg

关于如何在 android 上执行此操作的任何想法?

【问题讨论】:

    标签: android image-processing


    【解决方案1】:

    不必为此使用。 您也可以将 Canvas 类的 drawBitmap 函数之一与使用 Matrix 类的 setPolyToPoly 函数初始化的矩阵一起使用。

    public static Bitmap cornerPin(Bitmap b, float[] srcPoints, float[] dstPoints) {
        int w = b.getWidth(), h = b.getHeight();
        Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        Canvas c = new Canvas(result);
        Matrix m = new Matrix();
        m.setPolyToPoly(srcPoints, 0, dstPoints, 0, 4);
        c.drawBitmap(b, m, p);
        return result;
    }
    

    Paint 对象只需要启用抗锯齿。

    用法:

    int w = bitmap.getWidth(), h = bitmap.getHeight();
    float[] src = {
        0, 0, // Coordinate of top left point
        0, h, // Coordinate of bottom left point 
        w, h, // Coordinate of bottom right point
        w, 0  // Coordinate of top right point
    };
    float[] dst = {
        0, 0,        // Desired coordinate of top left point 
        0, h,        // Desired coordinate of bottom left point  
        w, 0.8f * h, // Desired coordinate of bottom right point
        w, 0.2f * h  // Desired coordinate of top right point
    };
    Bitmap transformed = cornerPin(bitmap, src, dst);
    

    其中src 是源点的坐标,dst 是目标点的坐标。结果:

    【讨论】:

    • @ Attila Tanyi ,根据您的示例,左边缘是固定的,右边缘是移动的,我希望右边缘应该固定并移动左边缘,顶部和底部也是如此。我该怎么办 ?有什么建议吗?
    • @GyanSwaroopAwasthi - 我编辑了我的答案,这些数字现在应该更有意义了。您应该更改示例中的 dst 浮点数组。如果它与src 数组具有完全相同的数字,则位图保持不变。除此之外,您只需要更改需要移动的点的坐标即可。坐标是 (x, y) 对,其中 x 是列(从左到右),y 是行(从上到下)。
    【解决方案2】:

    您想做的事情有各种艺术名称,“角针”是视觉效果行业中常用的一种。您需要分两步进行:

    1. 计算从所需的校正图像到原始失真图像的映射
    2. 实际上是根据 (1) 中计算的映射来扭曲原始图像。

    原始图像的 4 个(非共线、透视失真)角和目标(未失真)图像的 4 个角定义映射。此映射称为“homography”- 阅读指向的维基百科页面了解详细信息。一旦知道映射,就可以通过插值计算步骤(2)中的翘曲:对于目标图像中的每个像素,找到原始图像中的对应像素。由于这通常不在整数坐标处,因此您从邻居中插入其颜色。使用了各种插值方案,常见的有最近邻、双线性和双三次(在结果中按平滑度递增的顺序)。

    对于 Android,我建议安装 OpenCV SDK,然后使用 geometry transformation routines(上述两个步骤的 getPerspectiveTransform 和 warpPerspective)。

    【讨论】:

    • 非常感谢您的解释!我将尝试使用 OpenCV。
    猜你喜欢
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多