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