首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Opencv4Android getPerspectiveTransform误差

Opencv4Android getPerspectiveTransform误差
EN

Stack Overflow用户
提问于 2017-06-20 01:31:13
回答 1查看 649关注 0票数 0

我想使用getPerspectiveTransform函数,但它只接受Mat作为参数,并且我在数组中有坐标数据。因此,我将它们转换为点,然后按以下方式转换为Mat:

代码语言:javascript
复制
List<Point> l = new ArrayList<Point>();
for (int i = 0; i < pts_1.length; i++) {
    Point pt = new Point(pts_1[0][i], pts_1[1][i]);
    l.add(i,pt);
}
Mat mat1 = Converters.vector_Point2f_to_Mat(l);
for (int i = 0; i < pts_2.length; i++) {
    Point pt = new Point(pts_2[0][i], pts_2[1][i]);
    l.add(i,pt);
    }
Mat mat2 = Converters.vector_Point2f_to_Mat(l);
Mat perspectiveTransform = Imgproc.getPerspectiveTransform(mat1,mat2);

但是当我运行我的应用程序时,它会给我错误“出了问题”,而在logcat中,我得到了以下错误:

代码语言:javascript
复制
E/cv::error(): OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4) in cv::Mat cv::getPerspectiveTransform(cv::InputArray, cv::InputArray), file /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp, line 6748
E/org.opencv.imgproc: imgproc::getPerspectiveTransform_10() caught cv::Exception: /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp:6748: error: (-215) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function cv::Mat cv::getPerspectiveTransform(cv::InputArray, cv::InputArray)

我是OpenCV4Android的新手,所以我不明白为什么会发生这种情况。怎么修呢?还有其他更好的方法吗?

谢谢你帮忙!

注意:我知道这里遵循了一个类似的过程:Can't get OpenCV's warpPerspective to work on Android,但是他没有得到这个错误,所以我在这里发布它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-20 07:23:59

正如OP的评论讨论中提到的(这都归功于他们),问题在于列表l

代码语言:javascript
复制
List<Point> l = new ArrayList<Point>();
for (int i = 0; i < pts_1.length; i++) {
    Point pt = new Point(pts_1[0][i], pts_1[1][i]);
    l.add(i,pt);
}
Mat mat1 = Converters.vector_Point2f_to_Mat(l);

如果我们看一下List<Point> l的内容

代码语言:javascript
复制
for (Point pt : l)
    System.out.println("(" + pt.x + ", " + p.ty + ")");

(x0, y0)
(x1, y1)
(x2, y2)
(x3, y3)

然后进入下一个矩阵:

代码语言:javascript
复制
for (int i = 0; i < pts_2.length; i++) {
    Point pt = new Point(pts_2[0][i], pts_2[1][i]);
    l.add(i,pt);
    }
Mat mat2 = Converters.vector_Point2f_to_Mat(l);

再看一下List<Point> l的内容

代码语言:javascript
复制
for (Point pt : l)
    System.out.println("(" + pt.x + ", " + p.ty + ")");

(x4, y4)
(x5, y5)
(x6, y6)
(x7, y7)
(x0, y0)
(x1, y1)
(x2, y2)
(x3, y3)

这就是罪魁祸首,你的第二个矩阵会有8分。

来自ArrayList

参数:index - index at which the specified element is to be inserted

使用l.add将插入而不是覆盖旧列表。因此,解决方案是为每个转换矩阵创建一个新的列表。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44642398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档