找到轮廓的边界框

时间:2014-03-18 08:04:51

标签: 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个声誉 如果有人有解决方案请回复! 三江源。

1 个答案:

答案 0 :(得分:3)

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);
         }

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