OpenCV 层次结构始终为空

OpenCV Hierarchy is always null

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

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));
        }
    }

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

[-1.0, -1.0, -1.0, 2.0]
null
null
null
null
null
null
null
null
null
null

当您使用 Mat 表示 findContours 返回的层次结构时,您会得到一个数组,其中包含:

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

现在,您的问题就变得很明显了。

hierarchy.get(i, 3)[3]

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

public double[] get(int row, int col)

注意第一个参数是行号。您将等高线编号作为行传递,但只有一行。

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

你真正应该做的是

hierarchy.get(0, i)[3]

最后一个问题是您不必要地将索引转换为浮点数。这是一种浪费,而且会适得其反,因为要发挥更大的作用,您必须将它们转换回整数。只需使用 appropriate overload of 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

我注意到另一个问题。在调用 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));
        }
    }

}

当您从层次结构列表中删除项目时,还需要检查已删除项目的关系。我首先像这样将完整的层次结构 Mat 转移到 List:

List<double[]> listHierarchy = new ArrayList<>();
                for (int i = 0; i< list.size(); i++){
                    listHierarchy.add(hierarchie.get(0, i));
                }

然后,当我需要从这个列表中删除项目时,我调用这个函数:

 List<double[]> deleteHierarchyItem(int position, List<double[]> hierarchy){
    double[] itemHierarchy = hierarchy.get(position);
    double[] workItem;

    //doesnt have children?
    if (itemHierarchy[2]!=-1){
        int nextChild =(int) (itemHierarchy[2]);
        do {
            //nacte dite
            workItem = hierarchy.get(nextChild);
            //zmeni rodice v diteti na rodice puvodniho rodice
            workItem[3] = itemHierarchy[3];
            //nastavi nova data v listu
            hierarchy.set(nextChild, workItem);
            //zmeni ukazatel na nove dite
            nextChild = (int)(workItem[2]);

        } while (nextChild != -1);
    }

    //check for siblings
    boolean hasNextSibling = itemHierarchy[0] != -1;
    boolean hasPreviousSibling = itemHierarchy[1] != -1;

    double idPreviousSibling = itemHierarchy[1];
    double idNextSibling = itemHierarchy[0];

    //has both siblings
    if (hasPreviousSibling && hasNextSibling){
        //change previous sibling
        workItem = hierarchy.get((int)(idPreviousSibling));
        workItem[0] = idNextSibling;
        hierarchy.set((int)(idPreviousSibling), workItem);
        //change next sibling
        workItem = hierarchy.get((int)(idNextSibling));
        workItem[1] = idPreviousSibling;
        hierarchy.set((int)(idNextSibling), workItem);
    }

    //has only previous sibling
    if (hasPreviousSibling && !hasNextSibling){
        workItem = hierarchy.get((int)(idPreviousSibling));
        workItem[0] = -1;
        hierarchy.set((int)(idPreviousSibling), workItem);
    }

    //has only next sibling
    if (!hasPreviousSibling && hasNextSibling){
        workItem = hierarchy.get((int)(idNextSibling));
        workItem[1] = -1;
        hierarchy.set((int)(idNextSibling), workItem);

        //change of child parametres in parent
        if(itemHierarchy[3]>0)
        {
            workItem = hierarchy.get((int)(itemHierarchy[3]));
            workItem[2]=idNextSibling;
            hierarchy.set((int)(itemHierarchy[3]), workItem);
        }
    }

    //check for parent
    if (itemHierarchy[3]!=-1){
        workItem = hierarchy.get((int)(itemHierarchy[3]));
        if (workItem[2]==position){
            workItem[2] = -1;
            hierarchy.set((int)(itemHierarchy[3]), workItem);
        }
    }

    //iterate and decrement values
    for (int i = position; i< hierarchy.size();i++){
        workItem = hierarchy.get(i);
        if (workItem[0]>position){
            workItem[0] = workItem[0] - 1;
        }
        if (workItem[1]>position){
            workItem[1] = workItem[1] - 1;
        }
        if (workItem[2]>position){
            workItem[2] = workItem[2] - 1;
        }
        if (workItem[3]>position){
            workItem[3] = workItem[3] - 1;
        }
        hierarchy.set(i, workItem);
    }
    return hierarchy;
}