不能得到的OpenCV的warpPerspective工作在Android [英] Can't get OpenCV's warpPerspective to work on Android

查看:586
本文介绍了不能得到的OpenCV的warpPerspective工作在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力实现我的Andr​​oid应用程序四到四核系统。这样做的目的是让用户拍摄照片,添加4个角点,并有四从图像为矩形提取。

I've been struggling to implement a quad to quad system in my Android application. The aim is to let the user take a picture, add 4 cornerpoints and have that quad extracted from the image as a rectangle.

我看了一下这种方法和<一href="http://stackoverflow.com/questions/9321307/image-perspective-transform-using-android-opencv">this问题使用OpenCV的这一点。由此产生的code是这样的:

I had a look at this method and this question to use OpenCV for this. The resulting code is this:

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

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

While testing, I make sure the order of the corner points is top-left, bottom-left, bottom-right, top-right.

但奇怪的是,该结果并不总是相同的。大多数时候它显示了一个单一的颜色,有时一个黑色的方形,有时用不同的颜色在它的对角线的正方形。即使尝试用 startM = ENDM 导致无法确定的行为。

The strange thing is that the result isn't always the same. Most of the times it shows a square of a single color, sometimes a black square, sometimes a diagonal line with different colors in it. Even experimenting with startM = endM results in non-deterministic behaviour.

我是什么在这里失踪?

推荐答案

找到了,问题是在这些行:

Found it, the problem was in these lines:

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

,应由此来代替:

Which should be replaced by this:

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

这篇关于不能得到的OpenCV的warpPerspective工作在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆