【问题标题】:OpenCV Hierarchy is always nullOpenCV 层次结构始终为空
【发布时间】:2017-08-17 20:37:26
【问题描述】:

我有一个轮廓重叠的图像,当我找到轮廓时,我一直在尝试使用层次结构过滤掉轮廓。我想要做的是过滤掉父母不等于-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));
        }
    }

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

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

【问题讨论】:

  • 调用findContourshierarchy 的实际形状和数据类型是什么?打印出来或在调试器中检查它。 | 3 作为get 中的列号似乎有点可疑。
  • 轮廓的形状是正方形。层次结构是一个垫子@DanMašek
  • 不,我的意思是它有多少行、列和通道,以及每个元素的类型(uint8、float 等)。 |在 C++ 中,我从来没有通过 Mat 那里,所以我不确定......让我试试吧。
  • 类型是 CvType 是 Cv8U,每个元素都是一个 int。在像 Mat 这样的数组中是 @DanMašek
  • 我知道 Mat 是什么,我要的是具体的 3 个数字。不过没关系,我已经做了一些测试。

标签: java opencv hierarchy


【解决方案1】:

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

  • 单行
  • 每个检测到的轮廓有一列
  • 4 个通道(next、previous、child 和 parent 轮廓的 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));
        }
    }

}

【讨论】:

  • 谢谢:)。所以总而言之,我应该只使用一个版本的 get 来查找父层次结构,还是你在谈论 MatOfPoint 的东西?
  • @ProgrammingCuber 查看编辑。这不是必需的,只是避免了这些数组的多次分配,以及 int 和 double 之间的转换。
  • 好的,我明白了,但我可以在问题中添加更多内容,因为它正在工作,但我得到的结果与我预期的不同。也许它会澄清得更好。
  • @ProgrammingCuber 好的,将其作为更新添加到您的问题底部。 |实际上我想我明白了问题所在:)层次结构中的位置对应于原始轮廓列表中的位置。由于squareContours 是所有找到的轮廓的子集,因此索引将不再匹配。
  • 更新了@DanMašek
【解决方案2】:

当您从层次结构列表中删除项目时,还需要检查已删除项目的关系。我首先将完整的层次结构 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;
}

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 2016-09-04
    相关资源
    最近更新 更多