【问题标题】:Can't get OpenCV's warpPerspective to work on Android无法让 OpenCV 的 warpPerspective 在 Android 上运行
【发布时间】:2013-06-26 01:10:42
【问题描述】:

我一直在努力在我的 Android 应用程序中实现四到四系统。目的是让用户拍照,添加 4 个角点,然后从图像中提取四边形作为矩形。

我查看了 this methodthis question 来使用 OpenCV。结果代码是这样的:

public static Bitmap warp(Bitmap image, MyPoint p1, MyPoint p2, MyPoint p3, MyPoint p4) {
    int resultWidth = 500;
    int resultHeight = 500;

    Mat inputMat = new Mat(image.getHeight(), image.getHeight(), CvType.CV_8UC4);
    Utils.bitmapToMat(image, inputMat);
    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);

    Point ocvPIn1 = new Point(p1.getX(), p1.getY());
    Point ocvPIn2 = new Point(p2.getX(), p2.getY());
    Point ocvPIn3 = new Point(p3.getX(), p3.getY());
    Point ocvPIn4 = new Point(p4.getX(), p4.getY());
    List<Point> source = new ArrayList<Point>();
    source.add(ocvPIn1);
    source.add(ocvPIn2);
    source.add(ocvPIn3);
    source.add(ocvPIn4);
    Mat startM = Converters.vector_Point2f_to_Mat(source);

    Point ocvPOut1 = new Point(0, 0);
    Point ocvPOut2 = new Point(0, resultHeight);
    Point ocvPOut3 = new Point(resultWidth, resultHeight);
    Point ocvPOut4 = new Point(resultWidth, 0);
    List<Point> dest = new ArrayList<Point>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);
    Mat endM = Converters.vector_Point2f_to_Mat(dest);      

    Mat perspectiveTransform = new Mat(3, 3, CvType.CV_32FC1);
    Core.perspectiveTransform(startM, endM, perspectiveTransform);

    Imgproc.warpPerspective(inputMat, 
                            outputMat,
                            perspectiveTransform,
                            new Size(resultWidth, resultHeight), 
                            Imgproc.INTER_CUBIC);

    Bitmap output = Bitmap.createBitmap(resultWidth, resultHeight, Bitmap.Config.RGB_565);
    Utils.matToBitmap(outputMat, output);
    return output;
}

在测试时,我确保角点的顺序是左上角、左下角、右下角、右上角。

奇怪的是结果并不总是一样的。大多数时候它显示一个单一颜色的正方形,有时是一个黑色的正方形,有时是一条带有不同颜色的对角线。即使尝试使用 startM = endM 也会导致不确定的行为。

我在这里错过了什么?

【问题讨论】:

  • 这有点作用,但有时它会提供最终位图的镜像和/或旋转副本。 ?这是想要的结果吗

标签: android opencv transform perspective


【解决方案1】:

我的代码有一些问题,所以我进行了更改,直到我得到一个工作版本,如果有人对问题代码有问题,这是我的代码:

Bitmap warp(Bitmap image, Point topLeft, Point topRight, Point bottomLeft, Point bottomRight) {
    int resultWidth = (int)(topRight.x - topLeft.x);
    int bottomWidth = (int)(bottomRight.x - bottomLeft.x);
    if(bottomWidth > resultWidth)
        resultWidth = bottomWidth;

    int resultHeight = (int)(bottomLeft.y - topLeft.y);
    int bottomHeight = (int)(bottomRight.y - topRight.y);
    if(bottomHeight > resultHeight)
        resultHeight = bottomHeight;

    Mat inputMat = new Mat(image.getHeight(), image.getHeight(), CvType.CV_8UC1);
    Utils.bitmapToMat(image, inputMat);
    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC1);

    List<Point> source = new ArrayList<>();
    source.add(topLeft);
    source.add(topRight);
    source.add(bottomLeft);
    source.add(bottomRight);
    Mat startM = Converters.vector_Point2f_to_Mat(source);

    Point ocvPOut1 = new Point(0, 0);
    Point ocvPOut2 = new Point(resultWidth, 0);
    Point ocvPOut3 = new Point(0, resultHeight);
    Point ocvPOut4 = new Point(resultWidth, resultHeight);
    List<Point> dest = new ArrayList<>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);
    Mat endM = Converters.vector_Point2f_to_Mat(dest);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

    Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight));

    Bitmap output = Bitmap.createBitmap(resultWidth, resultHeight, Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(outputMat, output);
    return output;
}

【讨论】:

  • @Ata 你的问题是什么?
  • @jonathanrz 您在这一行中也为宽度设置了高度 Mat inputMat = new Mat(image.getHeight(), image.getHeight(), CvType.CV_8UC1);
  • 正是我想要的! 100 万感谢@jonathanrz
【解决方案2】:

找到了,问题出在以下几行:

Mat perspectiveTransform = new Mat(3, 3, CvType.CV_32FC1);
Core.perspectiveTransform(startM, endM, perspectiveTransform);

应该用这个代替:

Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

【讨论】:

  • 感谢@Aegoins,你为我节省了很多精力。
  • 你好,我和你一样关注同样的事情,但我得到了一个奇怪的输出。实际上我的输入是一张名片,使用透视变换我想从图像中提取名片。我得到的输出图像只是一张空白彩色图像,其颜色来自卡片中的一种颜色。
  • @Aegonis 这有点工作原理,但有时它会提供最终位图的镜像副本。 ?这是想要的结果吗
  • @Manpreet 查看我的答案代码并检查它是否解决了您的问题。
猜你喜欢
  • 1970-01-01
  • 2023-02-04
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 2014-03-26
相关资源
最近更新 更多