如何检测EmguCv中的五边形和六边形

时间:2016-05-23 04:57:30

标签: c# winforms image-processing emgucv

我正在做一个使用emgucv识别基本几何形状的项目。我使用以下代码来识别五边形形状。但是当我执行它时,它也会显示五边形和圆形。我只想要五边形作为输出。我已经完成了识别圆圈,矩形和三角形。如何使用此代码仅显示五边形?
谢谢!

    public Image<Bgr, Byte> My_Image;
    public Image<Gray, Byte> grayImage;
    public Image<Gray, Byte> cannyEdges;      

    grayImage = My_Image.Convert<Gray, byte>();



public void CannyEdgeDetection()
         {
            try
            {
                double cannyThresholdLinking = 120.0;
                cannyEdges = grayImage.Canny(cannyThreshold, cannyThresholdLinking);
                lines = cannyEdges.HoughLinesBinary(
                    1, //Distance resolution in pixel-related units
                    Math.PI / 45.0, //Angle resolution measured in radians.
                    20, //threshold
                    10, //min Line width
                    10 //gap between lines
                    )[0]; //Get the lines from the first channel
            }
            catch (Exception ex)
            {

                throw ex;
            }

         }

    public void DetectPentagons()
            {

                try
                {

                    CannyEdgeDetection();

                    using (MemStorage storage = new MemStorage())
                        for (
                          Contour<Point> contours = cannyEdges.FindContours(
                              Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
                              Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL,
                              storage);
                           contours != null;
                           contours = contours.HNext)
                        {
                            Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);

                            if (currentContour.Area > 100)
                            {
                                if (currentContour.Total == 5) 
                                {

                                    grayImage.Draw(contours, new Bgr(Color.Red), 10);
                                    pictureBox2.Image = obj.grayImage.Bitmap;
                                }
                            }
                        }

                }

                catch (Exception ex)
                {
                    throw ex;
                }

            } 

1 个答案:

答案 0 :(得分:0)

您不应该依赖轮廓的多个段来检测形状,因为找到的轮廓将是锯齿(锯齿状),任何多边形近似(&#34;平滑&#34;)都可以改变轮廓太多了。

可用于检测形状的一个功能是circularity。圆度是决定形状与圆形的紧密程度的一个因素。

circularity ~= 1, shape is a circle
circularity < 1, shape is convex
circularity > 1, shape is concave

它的公式是:

circularity = (4 * PI * contour_area) / contour_perimeter ^ 2

每个形状都有自己的预期圆度值,例如,正方形应具有0.7853的圆度,并且您可以使用此值来过滤正方形的轮廓。您应该从FindContours方法获得所有可用值。