如何使用emgu cv摆脱不必要的行

问题描述 投票:1回答:1

我正在尝试使用Emgu CV检测椭圆形水滴的轮廓。我写了轮廓检测代码:

    public List<int> GetDiameters()
    {
        string inputFile = @"path.jpg";

        Image<Bgr, byte> imageInput = new Image<Bgr, byte>(inputFile);

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

        Image<Gray, byte> bluredImage = grayImage;
        CvInvoke.MedianBlur(grayImage, bluredImage, 9);

        Image<Gray, byte> edgedImage = bluredImage;
        CvInvoke.Canny(bluredImage, edgedImage, 50, 5);

        Image<Gray, byte> closedImage = edgedImage;           
        Mat kernel = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Ellipse, new System.Drawing.Size { Height = 100, Width = 250}, new System.Drawing.Point(-1, -1)); 
        CvInvoke.MorphologyEx(edgedImage, closedImage, Emgu.CV.CvEnum.MorphOp.Close, kernel, new System.Drawing.Point(-1, -1), 0, Emgu.CV.CvEnum.BorderType.Replicate, new MCvScalar());
       System.Drawing.Point(100, 250), 10000, Emgu.CV.CvEnum.BorderType.Default, new MCvScalar()

        Image<Gray, byte> contoursImage = closedImage;
        Image<Bgr, byte> imageOut = imageInput;
        VectorOfVectorOfPoint rescontours1 = new VectorOfVectorOfPoint();
        using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
        {
            CvInvoke.FindContours(contoursImage, contours, null, Emgu.CV.CvEnum.RetrType.List,
                Emgu.CV.CvEnum.ChainApproxMethod.LinkRuns);
            MCvScalar color = new MCvScalar(0, 0, 255);

            int count = contours.Size;
            for (int i = 0; i < count; i++)
            {
                using (VectorOfPoint contour = contours[i])
                    using (VectorOfPoint approxContour = new VectorOfPoint())
                    {
                        CvInvoke.ApproxPolyDP(contour, approxContour,
                            0.01 * CvInvoke.ArcLength(contour, true), true);

                        var area = CvInvoke.ContourArea(contour);

                    if (area > 0 && approxContour.Size > 10)
                    {
                        rescontours1.Push(approxContour);
                    }

                        CvInvoke.DrawContours(imageOut, rescontours1, -1, color, 2);
                    }                   
            }
        }          
    }

到目前为止的结果:

enter image description here

我认为近似值存在问题。如何消除内部线条并封闭外部轮廓?

c# computer-vision emgucv
1个回答
1
投票

我可能需要更多信息来准确查明您的问题,但这可能与中位数模糊有关。我会看到您是否足够模糊,以至于EmguCV东西的模糊程度足以使您无法进行边缘检测。您可以使用的另一种方法是Dilate。尝试拨打您的Canny边缘检测电话,看看是否有更好的结果。

© www.soinside.com 2019 - 2024. All rights reserved.