使用 OpenCVSharp 使用 C# 查找边界框?

我使用https://github.com/shimat/opencvsharp包装器在 C# 中使用 opencv。

其实我想得到我countours的最大边界框。倾斜无关紧要。我想要一个完美的直盒。

找到轮廓后我的实际结果如下所示:

http://img2.mukewang.com/61ac62160001219e05830455.jpg

这是我的代码:


  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。我有点困惑。


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


噜噜哒
浏览 285回答 2
2回答

智慧大石

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

MYYA

我自己找到了解决方案。&nbsp;var biggestContourRect = Cv2.BoundingRect(contours[0]);&nbsp; &nbsp; &nbsp; &nbsp; Cv2.Rectangle(dst,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new OpenCvSharp.Point(biggestContourRect.X, biggestContourRect.Y),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new OpenCvSharp.Point(biggestContourRect.X + biggestContourRect.Width, biggestContourRect.Y + biggestContourRect.Height),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Scalar(255, 255, 255),2);做这份工作:)
打开App,查看更多内容
随时随地看视频慕课网APP