用旋转的矩形近似轮廓

时间:2013-02-09 23:24:19

标签: emgucv contour

经过一些颜色检测和二进制阈值处理后,我使用以下代码查找轮廓并将其绘制到图像上:

 using (MemStorage stor = new MemStorage())
        {
           Contour<Point> contours = img.FindContours(
              Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
              Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST,
              stor);

           for (; contours != null; contours = contours.HNext)
           {
              Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * poly, stor);

              img.Draw(currentContour,new Bgr(255,255,255),1);

              Rectangle currentrect = currentContour.BoundingRectangle;

              img.Draw(currentrect,new Bgr(255,255,255),2);
            }
        }

我的问题是,正如我所料,如果轮廓是一个矩形但在图像中旋转,则边界矩形不会改变其方向以适应旋转。他们是完成这个功能的另一种方式吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

是的,还有另一种方法可以实现这一目标。你可以使用

contour.GetConvexHull(ORIENTATION.CV_CLOCKWISE);

使用Moments,您可以轻松获得方向并相应地调整矩形。

答案 1 :(得分:0)

您正在寻找的方法是:

PointCollection.MinAreaRect(points);

工作示例在这里: http://www.emgu.com/wiki/index.php/Minimum_Area_Rectangle_in_CSharp

完整的文档(上面没有上述内容)位于: http://www.emgu.com/wiki/files/2.4.0/document/html/0d5fd148-0afb-fdbf-e995-6dace8c8848d.htm

相关问题