title: Trouble with Points in OpenCV
body: <p>I have a small bug in some OpenCV code that attempts to do the follow:</p>
  
  <ol>
  <li><p>Stitch two webcam images together in real time.</p></li>
  <li><p>Perform background subtraction and calculate the typical vector of contours from the resulting foreground Mat.</p></li>
  <li><p>From the contours vector, calculate points that represent where the object is in the image. It could just be the centroid, but I'm using the center point on the bottom edge of all of the bounding boxes. This is done in the function "lowestPoints".</p></li>
  <li><p>Transfer these points into a new coordinate system, which roughly approximates to just scaling them down or up to the new images size, but in the future it could be anything. This is done in the function "tf".</p></li>
  </ol>
  
  <p>I can confirm from testing and seeing the results that #'s 1-3 work just fine. For some reason, #4 doesn't work correctly at all. The new calculated coordinates always have a zero x component, and the y component doesn't scale at all. When I plot them or cout the coordinates, it's always zero, and always plotted on the left hand side of the new image. The relevant code is below:</p>
  
  <pre><code>//from transfunc.cpp
  
  #include &lt;opencv2/opencv.hpp&gt;
  #include &lt;vector&gt;
  #include "defines.h"
  #include "protos.h"
  
  vector&lt;Point&gt; lowestPoints(vector&lt;vector&lt;Point&gt;&gt; contours)
  {
      // Returns the midpoints of the bottom edge of contour bounding boxes
      Point p;
      Rect box;
      vector&lt;Point&gt; lowPoints;
  
      for (unsigned int i = 0; i &lt; contours.size(); i++)
      {
          box = boundingRect(contours[i]);
          p.x = box.x + box.width/2;
          p.y = box.y + box.height;
          lowPoints.push_back(p);
      }
  
      return lowPoints;
  }
  
  vector&lt;Point&gt; tf(vector&lt;Point&gt; in, vector&lt;Point&gt; grid, Mat camImg, Mat layoutImg)
  {
      int a, b;
      Point temp;
      vector&lt;Point&gt; out;
  
      std::cout &lt;&lt; "Points set in to TF..." &lt;&lt; std::endl &lt;&lt; std::endl;
      printPoints(in);
  
      a = layoutImg.cols / camImg.cols;
      b = layoutImg.rows / camImg.rows;
  
      for (unsigned int i = 0; i &lt; in.size(); i++)
      {
          temp.x = in[i].x * a;
          temp.y = in[i].y * b;
          out.push_back(temp);
      }
  
      std::cout &lt;&lt; "Points calculated in TF..." &lt;&lt; std::endl &lt;&lt; std::endl;
      printPoints(out);
  
      return out;
  }
  
  // from main.cpp
  
  #include &lt;opencv2/opencv.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "defines.h"
  #include "protos.h"
  
  int main()
  {
      //variable declarations and initializations
  
      while(true) //main webcam feed loop
      {
          // First time through calculate necessary camera parameters for stitching
          // Grab frames from videoCaptures
          // Stitch the frames together (stored in Mat pano)
          // Background subtraction + calculate vector&lt;vector&lt;Point&gt;&gt; contours
  
          if (contours.size() &gt; 0)
          {
              drawBoundingBoxes(contours, pano);
              lowPoints = lowestPoints(contours);
              objPoints = tf(lowPoint, pano, topDown);
              // Draw everything
  
              std::cout &lt;&lt; "Printing 'Lowest' Points in main..." &lt;&lt; std::endl;
              printPoints(lowestPoints(contours));
              std::cout &lt;&lt; "Printing 'Object' Points in main..." &lt;&lt; std::endl;
              printPoints(objPoints);
          }
  
          // Show images
  
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The output for a sample point looks as follows:</p>
  
  <p>Points sent in to TF...</p>
  
  <p>Point 0: (509, 340)
  Point 1: (477, 261)</p>
  
  <p>Points calculated in TF...</p>
  
  <p>Point 0: (0, 340)
  Point 1: (0, 261)</p>
  
  <p>Printing 'Lowest' points in main...</p>
  
  <p>Point 0: (509, 340)
  Point 1: (477, 261)</p>
  
  <p>Printing 'Object' points in main...</p>
  
  <p>Point 0: (0, 340)
  Point 1: (0, 261)</p>
  
  <p>Why is the x-coordinate always zero, and why is the y-coordinate not being scaled by b?</p>
  
  <p>Thanks,</p>
  
  <p>-Tony</p>