【问题标题】:Find bounding box with C# using OpenCVSharp?使用 OpenCVSharp 用 C# 查找边界框?
【发布时间】:2018-08-20 10:54:17
【问题描述】:

我使用https://github.com/shimat/opencvsharp 包装器将opencv 与C# 一起使用。

实际上,我想获得我的计数的最大边界框。 倾斜无关紧要。我想要一个完美的直盒。

我找到轮廓后的实际结果是这样的:

这是我的代码:

  Mat src = new Mat("index.jpg", ImreadModes.GrayScale);
  // Mat src = Cv2.ImRead("lenna.png", ImreadModes.GrayScale);
  Mat dst = new Mat();
  Mat dst2 = new Mat();

  Cv2.Canny(src, dst, hScrollBar1.Value, hScrollBar2.Value);
  //using (new Window("src image", src)) ;
  //using (new Window("dst image", dst)) ;

  // Find contours
  OpenCvSharp.Point[][] contours; //vector<vector<Point>> contours;
  HierarchyIndex[] hierarchyIndexes; //vector<Vec4i> hierarchy;

  Cv2.FindContours(dst, out contours, out hierarchyIndexes, RetrievalModes.External, ContourApproximationModes.ApproxSimple);
  using (new Window("dst image", dst)) ;

我看到有一个函数BoundingRect

 Cv2.BoundingRect()

听起来对我来说是正确的。但是这个函数需要一个名为 curve 的 InputArray。 我有点困惑。

如果有人能给我一个提示,那就太好了。

谢谢

【问题讨论】:

  • 通常你会传递你想要得到矩形的轮廓。以防万一您之后需要旋转的矩形,minAreaRect 将完成这项工作并且输入是相同的,轮廓(点向量,至少在 C++ 中)。
  • 感谢您的评论。 BoundingBox 从类型 OpenCvSharp.InputArray 请求曲线。我的 Contour 来自 OpenCvSharp.Point[][] 类型。那么我应该如何将轮廓转换为 inputArray?
  • 我没有用 C# OpenCV 做太多的工作,但是在 C++ 中 inputArray 有很多东西,比如一个 Mat 对象或一个点向量,它会隐式地将它转换为它需要的东西。 OpenCvSharp.Point[][] 是点向量的向量,或者是轮廓向量,只通过其中一个即可。 FindContours,也接收 inputArrays 或 inputOutputArrays
  • 不幸的是它不会自动转换。

标签: c# opencv bounding-box opencvsharp


【解决方案1】:

看起来轮廓[0] 根本不是最大的轮廓。您需要遍历所有轮廓并将每个区域与临时最大轮廓进行比较。

【讨论】:

    【解决方案2】:

    我自己找到了解决方案。

     var biggestContourRect = Cv2.BoundingRect(contours[0]);
    
            Cv2.Rectangle(dst,
                new OpenCvSharp.Point(biggestContourRect.X, biggestContourRect.Y),
                new OpenCvSharp.Point(biggestContourRect.X + biggestContourRect.Width, biggestContourRect.Y + biggestContourRect.Height),
                new Scalar(255, 255, 255),2);
    

    做好工作:)

    【讨论】:

    • 这就是我试图告诉你的 :) 你传递了一个轮廓,它会自动将其转换为 inputarray。顺便说一句,opencv rects 有tl()br() 给你左上角和右下角
    • 谢谢。对不起,我没有明白你的意思。顺便说一句,我有一张检测到多个轮廓的图像。有没有办法获取所有检测到的轮廓的边界框?
    • 遗憾的是没有直接的方法......只需遍历向量:) 在 C++ 中有一个矩形的重载,您可以直接传递矩形而不是点(矩形(Mat& img,矩形rec, const Scalar& color)) 这可能更容易阅读:)
    猜你喜欢
    • 2012-06-18
    • 2017-07-10
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多