2

我想用我的 kinect 来识别某些物体。我想使用的方法之一是霍夫变换。为此,我正在使用 AForge 库。

但我发现的线条是完全错误的。重要的丢失了,图片中有很多无用的线条。

为了解决这个问题,我想:

首先,我通过 hough 检测 100 条最强烈的线条,以确保也检测到所有正确的线条。

我正在将来自边缘检测的所有点写入一个列表,并且我正在检查每条线是否检测到的线至少在 X 点上(我使用了 15、50 和 150),但是我的结果很糟糕。

在将它们绘制到图片中并进一步分析几何图形之前,您有什么想法吗?或者我的代码中可能只是一个严重的错误,我没有看到。

结果图片

SobelEdgeDetector Kante = new SobelEdgeDetector();
        System.Drawing.Bitmap neu2 = Kante.Apply(neu1);

        neu2.Save("test.png");

        for (int a = 0; a < 320; a++) //alle mögliche Kantenpunkte in eine Liste
        {
            for (int b = 0; b < 240; b++)
            {
                color = neu2.GetPixel(a, b);
                if ((color.R+color.G+color.B)/3 >= 50)
                {
                    Kantenpunkte.Add(new System.Drawing.Point(a, b));
                }
            }
        }

        Bitmap Hough = new Bitmap(320, 240);

        Hough.Save("C:\\Users\\Nikolas Rieble\\Desktop\\Hough.png");

        //houghtrans            
        HoughLineTransformation lineTransform = new HoughLineTransformation();

        // apply Hough line transofrm
        lineTransform.ProcessImage(neu2);
        Bitmap houghLineImage = lineTransform.ToBitmap();

        houghLineImage.Save("1.png");


        // get most intensive lines
        HoughLine[] lines = lineTransform.GetMostIntensiveLines(100);
        UnmanagedImage fertig = UnmanagedImage.FromManagedImage(neu2);

        foreach (HoughLine line in lines)
        {
            // get line's radius and theta values
            int r = line.Radius;
            double t = line.Theta;

            // check if line is in lower part of the image
            if (r < 0)
            {
                t += 180;
                r = -r;
            }

            // convert degrees to radians
            t = (t / 180) * Math.PI;

            // get image centers (all coordinate are measured relative
            // to center)
            int w2 = neu2.Width / 2;
            int h2 = neu2.Height / 2;

            double x0 = 0, x1 = 0, y0 = 0, y1 = 0;

            if (line.Theta != 0)
            {
                // none-vertical line
                x0 = -w2; // most left point
                x1 = w2;  // most right point

                // calculate corresponding y values
                y0 = (-Math.Cos(t) * x0 + r) / Math.Sin(t);
                y1 = (-Math.Cos(t) * x1 + r) / Math.Sin(t);
            }
            else
            {
                // vertical line
                x0 = line.Radius;
                x1 = line.Radius;

                y0 = h2;
                y1 = -h2;
            }

            // draw line on the image
            int a = 0;

            foreach (System.Drawing.Point p in Kantenpunkte) //count number of detected edge points that are on this line
            {
                double m1 = ((double)p.Y - y0)/((double)p.X - x0);       
                double m2 = ((y0 - y1)) / (x0 - x1);

                if (m1-m2<0.0001)
                {
                    a=a+1;
                }
            }

            if (a > 150) //only draw lines, which cover at least A points
            {
                AForge.Imaging.Drawing.Line(fertig,
                   new IntPoint((int)x0 + w2, h2 - (int)y0),
                   new IntPoint((int)x1 + w2, h2 - (int)y1),
                   System.Drawing.Color.Red); 
            }


        }
4

0 回答 0