程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何将轮廓层次结构从 python openCV 转换为 emgu cv 以找到封闭的轮廓大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何将轮廓层次结构从 python openCV 转换为 emgu cv 以找到封闭的轮廓?

开发过程中遇到如何将轮廓层次结构从 python openCV 转换为 emgu cv 以找到封闭的轮廓的问题如何解决?下面主要结合日常开发的经验,给出你关于如何将轮廓层次结构从 python openCV 转换为 emgu cv 以找到封闭的轮廓的解决方法建议,希望对你解决如何将轮廓层次结构从 python openCV 转换为 emgu cv 以找到封闭的轮廓有所启发或帮助;

我有一个用 python 编写的圆检测,我现在需要将其转换为 emgu cv 以启用绑定到 Xamarin IOS 应用程序。

为此,我在各个部分保存了检测过程的图像,以将其与新的 .net 实现进行比较。然有些部分是直截了当的,但其他部分似乎更棘手。我目前被困在检测到的形状的层次结构上,并且已经检查了示例以及 this answer,然后我将其选为第一个解决方案。

这是我尝试转换的 python 行:

# finding all contours in the image which are enclosed within other contours (child contours)
( cnts,cnts_hs ) = cv2.findContours(img.copy(),cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) 
cnts = [cnt for i,cnt in enumerate(cnts) if cnts_hs[0,i,3] != -1]

目前我使用上述答案进行了尝试,结果如下:

// finding all contours in the image which are enclosed within other contours (child contours)
// NOTE: 
// 'hIEr' is of type 'Mat'
// 'cnts' 'cntsContourOut' are both of type 'VectorOfVectorOfPoint'
CvInvoke.FindContours(img.Clone(),cnts,hIEr,RetrType.Ccomp,ChainApproxMethod.ChainApproxSimplE);
int count = cnts.Size;
for (int i = 0; i < count; i++)
{
    using (VectorOfPoint cntr = cnts[i]) {
        // check if the contour is enclosed in another contour
        if (hIEr.GetDataPointer(i).ToInt32() != -1)
        {
            cntsContourOut.Push(cntr);
        }
    }
}

但结果完全不同......然python线在对给定的样本图像进行过滤后产生49个轮廓,但.net线产生原始的91个轮廓(没有一个被过滤掉)。 我想我对 python 行的作用有一些误解,当涉及到 0 中的 3cnts_hs[0,3] 时,我仍然有这种误解。这就是为什么我在 .net 实现中将它们排除在外。

编辑 1:

我想我应该提到我为 hIEr.GetDataPointer() 尝试了以下参数变体:

hIEr.GetDataPointer(0,3)
// result in an array size exceeded exception

hIEr.GetDataPointer(i,3) // or even 4 instead of 3
// result in the same 91 contours

解决方法

经过更多的挖掘,我找到了解决方案:

var data = hier.GetData();
for (int i = 0; i < count; i++)
{
    using (VectorOfPoint cntr = cnts[i]) {
        var d = (int)data.GetValue(new int[3] { 0,i,3 });
        if(d != -1)
            cntsContourOut.Push(cntr);
    }
}

大佬总结

以上是大佬教程为你收集整理的如何将轮廓层次结构从 python openCV 转换为 emgu cv 以找到封闭的轮廓全部内容,希望文章能够帮你解决如何将轮廓层次结构从 python openCV 转换为 emgu cv 以找到封闭的轮廓所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。