gpt4 book ai didi

java - OpenCV 4.1 Java -contourArea() 断言深度 == CV_32F || 失败船体矩阵的深度 == CV_32S

转载 作者:行者123 更新时间:2023-12-01 20:13:07 25 4
gpt4 key购买 nike

我尝试根据生成的船体点计算船体面积凸包()

我关注了OpenCV Python tutorial (因为没有 Java 教程)并摆弄代码完成。

这是代码:

Imgproc.findContours(patternEdges, patternContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
MatOfInt patternHull = new MatOfInt();
Imgproc.convexHull(patternContours.get(0), patternHull);
Imgproc.contourArea(pickPoints(patternContours.get(0), patternHull)); // fails here

但这会引发以下异常:

Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.1.2) /home/build/git/opencv/modules/imgproc/src/shapedescr.cpp:274: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'contourArea']
at org.opencv.imgproc.Imgproc.contourArea_1(Native Method)
at org.opencv.imgproc.Imgproc.contourArea(Imgproc.java:1607)
at com.acme.opencv.Test.main(Test.java:94)

显然矩阵内部数据类型是错误的。但为什么,以及如何转换它?

我使用的是 OpenCV 4.1.2。

最佳答案

问题是,在Java中,API只实现了填充MatOfInt原始点矩阵的索引

这就是Javadoc (直接从 C++ 文档生成)说:

hull Output convex hull. It is either an integer vector of indices or vector of points. In the first case, the hull elements are 0-based indices of the convex hull points in the original array (since the set of convex hull points is a subset of the original point set). In the second case, hull elements are the convex hull points themselves.

(Emphasis mine)

没有“或”。只有 MatOfInt,它是原始矩阵。

您可以通过这样的辅助函数使用选取的点创建一个新矩阵:

private static MatOfPoint2f pickPoints(MatOfPoint points, MatOfInt indices) {
Point[] pickedPoints = new Point[indices.rows()];
int newRow = 0;
for (int index : indices.toArray()) {
pickedPoints[newRow++] = new Point(points.get(index, 0));
}
return new MatOfPoint2f(pickedPoints);
}

然后使用它:

Imgproc.findContours(patternEdges, patternContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
MatOfInt patternHullIndices = new MatOfInt();
Imgproc.convexHull(patternContours.get(0), patternHullIndices);
Imgproc.contourArea(pickPoints(patternContours.get(0), patternHull));

关于java - OpenCV 4.1 Java -contourArea() 断言深度 == CV_32F || 失败船体矩阵的深度 == CV_32S,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963442/

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