title: Get position of objects using kinect and C#
body: <p>I got a kinect mount in the ceiling pointing down to a table (that is roughly 2 to 3 meters of the kinect) and my objective is using the kinect depth stream, locate the objects and get their position to late send to unity. So for that i am using c sharp and opencv (emgu wrapper), i first use canny to get the edges and then use BoundingRect to create a box around the object. </p>
  
  <p>The result is the as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/BVf2c.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BVf2c.jpg" alt="enter image description here"></a></p>
  
  <p>Original:</p>
  
  <p><a href="https://i.stack.imgur.com/iNUTf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iNUTf.png" alt="enter image description here"></a></p>
  
  <p>As you can see the stationary objects are no problem but the box around the hands (the first was a flyer on it) is to big and sometimes is even worse. Is there any other way (using opencv ) of getting the positions of the objects?</p>
  
  <p>The objective is then to send the position and dimension (of the object) to unity (trough tcp/ip), so preferably the shapes have to squares or rectangles to easy manipulation on unity.</p>
  
  <p>The code I have so far is:</p>
  
  <pre><code>    Image&lt;Bgr, Byte&gt; grayImage = new Image&lt;Bgr, Byte&gt;("C:\\Users\\Pedro\\Desktop\\imgRva\\KinectSnapshot-03-48-01.png");
      Image&lt;Gray, Byte&gt; gray = grayImage.Convert&lt;Gray, Byte&gt;().PyrDown().PyrUp();
      CvInvoke.cvShowImage("texto", gray);
      CvInvoke.cvWaitKey(0);
      Image&lt;Gray, Byte&gt; bin = gray.ThresholdBinary(new Gray(40), new Gray(255));
      Image&lt;Gray, Byte&gt; cannyEdges = bin.Canny(300, 300);
      CvInvoke.cvShowImage("texto", cannyEdges);
      CvInvoke.cvWaitKey(0);
  
      Image&lt;Gray, Byte&gt; canny = new Image&lt;Gray, byte&gt;(cannyEdges.Size);
      using (MemStorage storage = new MemStorage())
  
          for (Contour&lt;Point&gt; contours = cannyEdges.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, storage); contours != null; contours = contours.HNext)
          {
              CvInvoke.cvDrawContours(canny, contours, new MCvScalar(255), new MCvScalar(255), -1, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));
          }
  
      using (MemStorage store = new MemStorage())
          for (Contour&lt;Point&gt; contours1 = cannyEdges.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, store); contours1 != null; contours1 = contours1.HNext)
          {
              Rectangle r = CvInvoke.cvBoundingRect(contours1, 1);
              canny.Draw(r, new Gray(255), 1);
              Debug.WriteLine(r.Location + " x " + r.Width + " y " + r.Height);
          }
      CvInvoke.cvShowImage("texto", canny);
      CvInvoke.cvWaitKey(0);
  </code></pre>
  
  <p>Ty</p>