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

OpenCV 4.1 Java-contourArea()无法断言船体矩阵的深度==CV_32F | |深度==CV_32S,java,opencv,opencv-contour,Java,Opencv,Opencv Contour,我试着从由生成的外壳点计算外壳面积 convexHull() 我遵循了(因为没有Java教程)的步骤,在代码完成方面做了很多工作 代码如下: Imgproc.findContours(patternEdges, patternContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); MatOfInt patternHull = new MatOfInt(); Imgproc.convexHull(patte

我试着从由生成的外壳点计算外壳面积
convexHull()

我遵循了(因为没有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
但这会引发以下异常:

显然,矩阵内部数据类型是错误的。但是为什么,我怎样才能转换它呢


我使用的是OpenCV 4.1.2。

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

这是(直接来自C++ DOC)所说的:

外壳
输出凸面外壳它是指数的整数向量或 点的向量在第一种情况下,外壳元素是基于0的索引 原始数组中凸包点的数目(自凸包集 点是原始点集的子集)。在第二种情况下,为外壳 元素是凸包点本身

(强调矿山)

没有“或”。只有
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));
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));