- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个具有重叠轮廓的图像,当我找到它们时,我一直在尝试使用层次结构过滤掉轮廓。我想做的是过滤掉 parent 不等于-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
最佳答案
当您使用 Mat
表示 findContours
返回的层次结构时,您会得到一个数组,其中包含:
现在,您的问题立即变得显而易见。
hierarchy.get(i, 3)[3]
get
您使用的方法具有以下签名:
public double[] get(int row, int col)
请注意,第一个参数是行号。您将轮廓编号作为行传递,但只有一行。
接下来,第二个参数是列。您始终会获得第 3 列——第三个轮廓的层次结构信息。
你真正应该做的事情是这样的
hierarchy.get(0, i)[3]
最后一个问题是您不必要地将索引转换为 float 。这是浪费的,而且会适得其反,因为要发挥更大作用,您必须将它们转换回整数。只需使用 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));
}
}
}
关于java - OpenCV 层次结构始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45744567/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!