gpt4 book ai didi

android - 无法让 OpenCV 的 warpPerspective 在 Android 上运行

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:34 24 4
gpt4 key购买 nike

我一直在努力在我的 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;
}

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

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

我在这里错过了什么?

最佳答案

找到了,问题出在这几行:

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

应替换为:

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

关于android - 无法让 OpenCV 的 warpPerspective 在 Android 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17361693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com