通过FindContours方法发现的轮廓层次结构中导航? [英] Navigate through hierarchy of contours found by FindContours method?

查看:4232
本文介绍了通过FindContours方法发现的轮廓层次结构中导航?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对于直接使用OpenCV的C ++开发人员来说必须很简单。但是我使用的是Emgu(一个用于.NET的OpenCV包装器),在最新版本中我们有方法 CvInvoke.FindContours 返回void,输出结果被传递参数引用,类型为 VectorOfVectorOfPoint

This must be simple for C++ developers using OpenCV directly. However what I'm using is Emgu (an OpenCV wrapper for .NET) and in the latest version we have the method CvInvoke.FindContours returning void, the output result is passed by parameter reference and is of type VectorOfVectorOfPoint.

这是一个简单的调用:

//outputResult is a VectorOfVectorOfPoint
CvInvoke.FindContours(inputImage, outputResult, null, RetrType.Tree, 
                      ChainApproxMethod.ChainApproxSimple);

对于 RetrType.List 只是将结果转换为一些数组的数组,并轻松循环通过所有的轮廓。但是在这里,我想浏览一个树中的所有轮廓。我想我们必须在这里用指针(通过输出结果的 Ptr 属性访问)用native(不安全的)C ++代码做一些事情。但我想知道是否有一个更.NET的友好的解决方案。如果即使使用指针是唯一的解决方案,我仍然不知道如何深入研究 Ptr 以浏览轮廓树。

For RetrType.List mode, we can just convert the result to some array of arrays and loop through all the contours easily. However here I would like to navigate through all the contours in a tree. I guess we must do something with native (unsafe) C++ code here with pointer (accessed via the Ptr property of the output result). But I wonder if there is a more .NET-friendly solution for this. And if even using pointer is the only solution, I still don't know how to delve into that Ptr to navigate through the contours tree.

随着Emgu安装的示例代码使用 CvInvoke.FindContourTree 代码片段(并返回一个 int [ ]

The sample codes accompanied with the Emgu installation have a snippet using CvInvoke.FindContourTree instead (and that returns a int[,]).

推荐答案

要获得轮廓的层次结构,必须首先传递 Mat 对象的函数:

To obtain the hierarchy of the contours, you must first pass a Mat object to the function:

Mat hierarchy = new Mat() ;
CvInvoke.FindContours(inputImage, outputResult, hierarchy, RetrType.Tree, 
                  ChainApproxMethod.ChainApproxSimple);

然后,您可以使用 hierarchy (请参阅此处了解详情在Python OpenCV中):

Then you can use the hierarchy object as follows (see here for more details in Python OpenCV) :

层级将是 Mat 对象大小为1 x大小 outputResult x 4.
所以对于索引 i 的轮廓:

hierarchy will be a Mat object of size 1 x size of outputResult x 4. So for the contour with index i:


  • hierachy [0,i,0] 是下一个轮廓的索引 $ <$> hierachy [0,i,1] 是同一层次结构级别上的先前轮廓的索引,或者 - 如果不存在则为-1。

  • hierachy [0,i,2] code>是轮廓 i 的子项的索引或 - 如果不存在则为-1

  • hierachy [0,i,3] 是轮廓父类的索引 i 或 - 1如果不存在

  • hierachy[0,i,0] is the index of the next contour at the same hierarchy level (with the same parent) or - 1 if it doesn't exist
  • hierachy[0,i,1] is the index of the previous contour at the same hierarchy level or - 1 if it doesn't exist
  • hierachy[0,i,2] is the index of the child of contour i or - 1 if it doesn't exist
  • hierachy[0,i,3] is the index of the parent of contour i or - 1 if it doesn't exist

这是如何使用层次结构对象。

That's how you use the hierarchy object.

轮廓本身通过 outputResult 对象通过使用它们的索引访问。

The contours themselves are accessed through the outputResult object by using their indices.

这篇关于通过FindContours方法发现的轮廓层次结构中导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆