Java OpenCV中获取轮廓等级

    

   在Java OpenCV中,Imgproc.findContours函数可以用来提取二值图形的轮廓线,其用法大多数博客都有讲解,但是唯独没有介绍如何获取这些轮廓线的等级。

   参数hierarchy的类型为Mat,使用System.out.println(hierarchy)打印,其值为:Mat [ 1*7*CV_32SC4, isCont=true, isSubmat=false, nativeObj=0x131ce00, dataAddr=0x1319280 ],但是还是不知道里面的轮廓等级关系;  

   不慌,使用如下代码可以打印出hierarchy存储的值,其中sImage为一幅Mat类型二值图像;

List<MatOfPoint> contours = new ArrayList<>();Mat hierarchy = new Mat(); Imgproc.findContours(sImage, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_NONE);
int[] current_hierarchy = new int[4];

for (int i = 0; i < contours.size(); i++) {
System.out.print(i);
System.out.print(" : ");

System.out.println(contours.get(i).size().height);
System.out.println(contours.get(i));

hierarchy.get(0, i, current_hierarchy);

System.out.print(current_hierarchy[0]);
System.out.print(", ");

System.out.print(current_hierarchy[1]);
System.out.print(", ");

System.out.print(current_hierarchy[2]);
System.out.print(", ");

System.out.println(current_hierarchy[3]);
}

hierarchy :4通道矩阵,元素个数为轮廓数量。通道【0】~ 通道【3】对应保存:后一个轮廓下标,前一个轮廓下标,父轮廓下标,内嵌轮廓下标。如果没有后一个,前一个,父轮廓,内嵌轮廓,那么该通道的值为 -1。

本例中总共提取到7条轮廓线,每条轮廓线的等级关系如下:

0 : 1, -1, -1, -1
1 : 3, 0, 2, -1
2 : -1, -1, -1, 1
3 : -1, 1, 4, -1
4 : 5, -1, -1, 3
5 : 6, 4, -1, 3
6 : -1, 5, -1, 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值