c# - 查找轮廓的边界框

标签 c# emgucv

using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
         for (Contour<Point> contours = grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
         {
             Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
            canny.Draw(new Rectangle(currentContour.BoundingRectangle.X, currentContour.BoundingRectangle.Y, currentContour.BoundingRectangle.Width, currentContour.BoundingRectangle.Height), new Gray(1), 1);
             canny.Draw(contours, new Gray(), 2);
         }

这段代码给了我边界框,但不符合标记,在某些情况下轮廓超出了盒子范围。 无法共享示例输出图像,因为它需要 10 个信誉 如果有人有解决方案请回复! 谢谢。

最佳答案

Image<Gray, Byte> canny = new Image<Gray, byte>(grayImage.Size);
     using (MemStorage storage = new MemStorage())
     for (Contour<Point> contours =grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, storage); contours != null; contours = contours.HNext)
     {
         //Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
         CvInvoke.cvDrawContours(canny, contours, new MCvScalar(255), new MCvScalar(255), -1, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));
     }
using (MemStorage store = new MemStorage())
         for (Contour<Point> contours1= grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, store); contours1 != null; contours1 = contours1.HNext)
         {
             Rectangle r = CvInvoke.cvBoundingRect(contours1, 1);
             canny.Draw(r, new Gray(255), 1);
         }

这个方法给了我完美的边界框!

关于c# - 查找轮廓的边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22473528/

相关文章:

c# - 此代码是否被视为递归?

opencv - 如何计算亮度直方图?

c# - 未找到 EmguCV DLL 异常 - 无法找到 cvextern?

c# - 如何使用 PictureBox 打开/关闭相机

c# - EmguCV/OpenCV QueryFrame 缓慢/缓冲

C# 简单程序错误。无法让控制台处理我的 else 语句请求

c# - 在Unity中序列化和反序列化Json和Json数组

c# - 当先行任务处于取消状态时执行 ContinueWith 以及在 ContinueWith 内部使用异步委托(delegate)

c# - 图像的 EMGU/OpenCV FFT 未产生预期结果

c# - kendoui网格全选