无法让OpenCV的warpPerspective在Android上运行

gle*_*man 5 android opencv transform perspective

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

我看了一下这个方法这个问题,为此使用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;
}
Run Code Online (Sandbox Code Playgroud)

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

奇怪的是,结果并不总是一样的.大多数时候,它显示单色的正方形,有时是黑色正方形,有时是不同颜色的对角线.甚至试验startM = endM非确定性行为的结果.

我在这里错过了什么?

gle*_*man 5

找到它,问题出在这些方面:

Mat perspectiveTransform = new Mat(3, 3, CvType.CV_32FC1);
Core.perspectiveTransform(startM, endM, perspectiveTransform);
Run Code Online (Sandbox Code Playgroud)

哪个应该被替换为:

Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);
Run Code Online (Sandbox Code Playgroud)