title: Hough with AForge on Kinect. How can i get the right lines?
body: <p>i want to use my kinect to recognise certain objects. One of the methods that i want to use is the Houghtransformation. To do this i am using the AForge library. </p>
  
  <p>But the lines that i find, are totally wrong. Important ones are missing and there are many useless lines in the picture. </p>
  
  <p>To take care of this problem i thought:</p>
  
  <p>First i am detecting the 100 most intense lines via hough to be sure that all the correct lines are detected as well. </p>
  
  <p>I am writing all points from edge detection into a list and them I am checking for each line whether the detected line is at least on X points ( I used 15, 50 and 150) but nontheless my results are bad. </p>
  
  <p>Have you got any idea, how i can find the right lines, before drawing them into the picture and analysing the geometry any further? Or maybe there is just a grave mistake in my code, that i dont see. </p>
  
  <p><a href="https://i.stack.imgur.com/tpT8D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tpT8D.png" alt="result picture"></a></p>
  
  <pre><code>SobelEdgeDetector Kante = new SobelEdgeDetector();
          System.Drawing.Bitmap neu2 = Kante.Apply(neu1);
  
          neu2.Save("test.png");
  
          for (int a = 0; a &lt; 320; a++) //alle mögliche Kantenpunkte in eine Liste
          {
              for (int b = 0; b &lt; 240; b++)
              {
                  color = neu2.GetPixel(a, b);
                  if ((color.R+color.G+color.B)/3 &gt;= 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 &lt; 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&lt;0.0001)
                  {
                      a=a+1;
                  }
              }
  
              if (a &gt; 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); 
              }
  
  
          }
  </code></pre>