从掩模中检测椭圆 uing CVInvoke.AbsDiff 方法。

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

我有一个随着时间增长的椭圆。

enter image description here

为了检测椭圆,我使用了CvInvoke.AbsDiff方法,得到的图像是这样的。enter image description here

我想把这个椭圆用拟合椭圆的方法,获得它的半径es。

这是我采取的方法。

            CvInvoke.AbsDiff(First, img, grayscale);
        CvInvoke.CvtColor(grayscale, grayscale, ColorConversion.Bgr2Gray);
        CvInvoke.GaussianBlur(grayscale, grayscale, new System.Drawing.Size(11, 11), 15, 15);
        CvInvoke.Threshold(grayscale, grayscale, Convert.ToInt16(Threshold), Convert.ToInt16(Threshold * 2), ThresholdType.Binary );
        Mat element = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new System.Drawing.Size(3, 3), new System.Drawing.Point(-1, -1));

        CvInvoke.Dilate(grayscale, grayscale, element, new System.Drawing.Point(-1, 1), 5, BorderType.Constant, new MCvScalar(255, 255, 255));
        CvInvoke.Canny(grayscale, grayscale, Threshold, MaxThreshold * 2, 3);
        VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
        CvInvoke.FindContours(grayscale, contours, null, RetrType.Ccomp, ChainApproxMethod.ChainApproxTc89Kcos);


        double area = 0;
        double ContourArea = 0;
        int contour = 0;
        int CenterX;
        int CenterY;
        for (int i = 0; i < contours.Size; i++)
        {

            System.Drawing.Rectangle rec = CvInvoke.BoundingRectangle(contours[i]);

            output.Draw(rec, new Bgr(255, 0, 255), 2);
            CenterX = ((rec.Width) / 2) + rec.X;
            CenterY = ((rec.Height) / 2) + rec.Y;
            ContourArea = rec.Width * rec.Height; ;

            if ((HWidth - CenterFactor) < CenterX && CenterX < (HWidth + CenterFactor) && (HHeight - CenterFactor) < CenterY && CenterY< (HHeight + CenterFactor) )
            {
                if (ContourArea < 1000000)
                    if (area < ContourArea)
                    {
                        area = ContourArea;
                        contour = i;

                    }
            }
        }
        //if (contour == 0)
        //{
        //    return arr;
        //}
        System.Drawing.Rectangle rect = CvInvoke.BoundingRectangle(contours[contour]);
        output.Draw(rect, new Bgr(0, 255, 0), 3);

但是我并不是每次都能得到最好的椭圆。这是我得到的轮廓。enter image description here

有其他方法吗?

c# opencv emgucv
1个回答
1
投票

虽然这个方法并不完全完美,但这可能是你可以采取的一个方向。

Mat input = CvInvoke.Imread(@"C:\Users\ajones\Desktop\Images\inputImg.png", ImreadModes.AnyColor);
Mat input2 = input.Clone();

Mat thresh = new Mat();
CvInvoke.GaussianBlur(input, thresh, new System.Drawing.Size(7, 7), 10, 10);

CvInvoke.Threshold(thresh, thresh, 3, 10, ThresholdType.Binary);

CvInvoke.Imshow("The Thresh", thresh);
CvInvoke.WaitKey(0);

Mat output = new Mat();
CvInvoke.CvtColor(thresh, output, ColorConversion.Bgr2Gray);

VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

CvInvoke.FindContours(output, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);
CvInvoke.DrawContours(input, contours, -1, new MCvScalar(0, 255, 0), 3, LineType.FourConnected);

CvInvoke.Imshow("The Image", input);
CvInvoke.WaitKey(0);

int biggest = 0;
int index = 0;
for (int i = 0; i<contours.Size; i++)
{
    if (contours[i].Size > biggest)
    {
        biggest = contours[i].Size;
        index = i;
    }
}

CvInvoke.DrawContours(input2, contours, index, new MCvScalar(0, 255, 0), 3, LineType.FourConnected);

CvInvoke.Imshow("The Image2", input2);
CvInvoke.WaitKey(0);

首先使用高斯滤波器对图像进行模糊处理。enter image description here

然后,使用二进制阈值。enter image description here

之后,找到图像上的所有轮廓。enter image description here

最后,你需要做的只是整理你的轮廓,直到你找到最大的一个。enter image description here

就像我说的,它并不完全完美,但我应该帮助把你推向正确的方向。

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