OpenCV 层次结构始终为空

Pro*_*ber 0 java opencv hierarchy

我有一个轮廓重叠的图像,当我找到它们时,我一直试图使用层次结构过滤掉轮廓。我想要做的是过滤掉父母不等于 -1 的轮廓。但是,当我尝试获取包含层次结构的信息时,父索引几乎每次都等于 null。我不是在查看获取当前轮廓父级状态的正确信息吗?这是我的代码。

List<MatOfPoint> contours = new ArrayList<>();
    List<MatOfPoint> squareContours = new ArrayList<>();
    Mat hierarchy = new Mat();
    //find all contours
    Imgproc.findContours(dilated, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

    //Remove contours that aren't close to a square shape.
    for(int i = 0; i < contours.size(); i++){
        if(hierarchy != null){
            double area = Imgproc.contourArea(contours.get(i)); 
            MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
            double perimeter = Imgproc.arcLength(contour2f, true);
            //Found squareness equation on wiki... 
            //https://en.wikipedia.org/wiki/Shape_factor_(image_analysis_and_microscopy)
            double squareness = 4 * Math.PI * area / Math.pow(perimeter, 2);

            if(squareness >= 0.7 && squareness <= 0.9 && area >= 2000){
                squareContours.add(contours.get(i));
            }
        }
    }
    //remove contour if it has a parent 
    List<MatOfPoint> finalContours = new ArrayList<>();
    for(int i = 0; i < squareContours.size();i++){
        if(hierarchy.get(i, 3)[3] == -1){ //this should be checking parent index I think.
            finalContours.add(squareContours.get(i));
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我打印包含父信息的层次矩阵时程序的输出 Arrays.toString(hierarchy.get(i,3)))

[-1.0, -1.0, -1.0, 2.0]
null
null
null
null
null
null
null
null
null
null
Run Code Online (Sandbox Code Playgroud)

Dan*_*šek 5

当您使用 aMat来表示由 返回的层次结构时findContours,您会得到一个包含以下内容的数组:

  • 单行
  • 每个检测到的轮廓一列
  • 4 个通道(下一个、上一个、子级和父级轮廓的 id)
  • 数据类型 32 位有符号整数

现在,您的问题立即变得显而易见。

hierarchy.get(i, 3)[3]
Run Code Online (Sandbox Code Playgroud)

get您使用的方法具有以下签名:

public double[] get(int row, int col)
Run Code Online (Sandbox Code Playgroud)

请注意,第一个参数是行号。您将轮廓编号作为行传递,但只有一行。

接下来,第二个参数是列。你总是得到第 3 列——第三个轮廓的层次结构信息。

你真正应该做的是

hierarchy.get(0, i)[3]
Run Code Online (Sandbox Code Playgroud)

最后一个问题是您不必要地将索引转换为浮点数。这是一种浪费,而且适得其反,因为要发挥很大作用,您必须将它们转换回整数。只需使用适当的get.

现在,我的 Java 生锈了,但我认为你可以这样做:

int[] current_hierarchy = new int[4];
for(int i = 0; i < squareContours.size();i++) {
    hierarchy.get(0, i, current_hierarchy);
    if (current_hierarchy[3] == -1) {
        // ... and so on
Run Code Online (Sandbox Code Playgroud)

我注意到另一个问题。在调用 之后findContours, 中的值hierarchy对应于contours列表中的索引(位置)。但是,您首先删除了一些轮廓(通过仅将它们的一个子集插入另一个列表中),而不会对层次结构数据进行任何类似的更改。然后迭代这个子集,最终由于索引不匹配而使用错误的层次结构条目。

为了解决这个问题,我将两个循环合并在一起,可能是这样的:

List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
//find all contours
Imgproc.findContours(dilated, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

// Remove contours that aren't close to a square shape
// and remove contour if it has a parent 
List<MatOfPoint> finalContours = new ArrayList<>();
int[] current_hierarchy = new int[4];
for(int i = 0; i < contours.size(); i++){
    double area = Imgproc.contourArea(contours.get(i)); 
    MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
    double perimeter = Imgproc.arcLength(contour2f, true);
    //Found squareness equation on wiki... 
    //https://en.wikipedia.org/wiki/Shape_factor_(image_analysis_and_microscopy)
    double squareness = 4 * Math.PI * area / Math.pow(perimeter, 2);

    if(squareness >= 0.7 && squareness <= 0.9 && area >= 2000){
        hierarchy.get(0, i, current_hierarchy);
        if (current_hierarchy[3] == -1) {
            finalContours.add(contours.get(i));
        }
    }

}
Run Code Online (Sandbox Code Playgroud)