title: How do you get the original coordinates of a point within a zoomed image?
body: <p>I have this school assignment which asks me to be able zoom in and out on a region of interest using opencv and C++. The size of the region of interest is initialized at 80*80 and it augments (left click) and diminishes (right click) by 10px vertically and horizontally on click. I think I have managed to do that as so:</p>
  
  <pre><code>// callback associé à la souris sur la fenêtre "TP1"
  // clic droit -&gt; zoom arrière
  // clic gauche -&gt; zoom avant
  // (x, y) indique la position de la souris sur la fenêtre
  // event est un évenement de la souris (clic, souris déplacé, etc.)
  // flags indique si un bouton de la souris ou du clavier est enfoncé
  void cb_on_mouse(int event, int x, int y, int flags, void*)
  {
      Mat tmp;    // temporary image
      bool update = false;
  
      // copying src to tmp
      src.copyTo(tmp);
  
      // copying src to dst
      src.copyTo(dst);
  
      // when mouse moves
      if (event == EVENT_MOUSEMOVE)
      {
          // showing x and y coordinates of the mouse on the main image
          putText(dst, "x: " + to_string(x), Point(dst.rows + 4, dst.cols - 25), CV_FONT_HERSHEY_COMPLEX, 5, (0,0,0), 8, true);
          putText(dst, "y: " + to_string(y), Point(dst.rows + 4, dst.cols - 5), CV_FONT_HERSHEY_COMPLEX, 5, (0,0,0), 8, true);
          imshow("TP1", dst);
      }
  
      // when a click happens
      else
      {
          // if ctrl + mouse_btn then there's no zoom
          if (flags != EVENT_FLAG_CTRLKEY + EVENT_FLAG_RBUTTON &amp;&amp; flags != EVENT_FLAG_CTRLKEY + EVENT_FLAG_LBUTTON)
          {
              // left click zooms in
              if (event == EVENT_LBUTTONDOWN)
              {
                  // zoom in limit set at 2 for both height and width
                  if (roi.height &gt; 20 &amp;&amp; roi.width &gt; 20)
                  {
                      roi.height -= pas_yzoom;
                      roi.width -= pas_xzoom;
                  }
              }
  
              // right click zooms out
              else if (event == EVENT_RBUTTONDOWN)
              {
                  // zoom out limit is set of original image size (or close)
                  if (roi.height &lt; tmp.rows &amp;&amp; roi.width &lt; tmp.cols)
                  {
                      roi.height += pas_yzoom;
                      roi.width += pas_xzoom;
                  }
              }
  
              // calculating x
              // making sure it doesn't get out of bounds
              if (x - (int)(roi.width / 2) &lt;= 0)
                  roi.x = 0;
  
              else if (x + (int)(roi.width / 2) &gt;= tmp.cols)
                  roi.x = tmp.cols - roi.width;
  
              else
                  roi.x = x - (int)(roi.width / 2);
  
              // calculating y
              // making sure it doesn't go out of bounds
              if (y - (int)(roi.height / 2) &lt;= 0)
                  roi.y = 0;
  
              else if (y + (int)(roi.height / 2) &gt;= tmp.rows)
                  roi.y = tmp.rows - roi.height;
  
              else
                  roi.y = y - (int)(roi.height / 2);
  
              update = true;  
          }
  
          // only updating the image if an update took place
          if (update)
          {
              zoomed_img = tmp(roi);  // creating tmp image
              imshow("Zoom", zoomed_img); // displaying tmp
          }
      }
  }
  </code></pre>
  
  <p>Now I need to gather information about a pixel clicked within the zoom window, but I do not know how to calculate its coordinates on the original image. I really don't know how to deal with scale and all that. I tried this but it isn't working:</p>
  
  <pre><code>Mat tmp_zoom;   // temporary image for zoom
  Mat tmp_src;    // temporary image for source
  float base_x;   // x on original image
  float base_y;   // y on original image
  float scale;
  
  zoomed_img.copyTo(tmp_zoom);
  src.copyTo(tmp_src);
  scale = (float)(tmp_zoom.cols * tmp_zoom.rows) / (float)(roi.width * roi.height);
  
  if (event == EVENT_LBUTTONDOWN)
  {
      base_x = ((float)x / scale);
      base_y = ((float)y / scale);
  
      cout &lt;&lt; "Click on zoom: " &lt;&lt; "\n";
      cout &lt;&lt; "base_x: " &lt;&lt; base_x &lt;&lt; "\n";
      cout &lt;&lt; "base_y: " &lt;&lt; base_y &lt;&lt; "\n";
      circle(tmp_src, Point(base_x, base_y), 5, (0, 0, 0), 1, 8, 0);
      imshow("TP1", tmp_src);
  }
  </code></pre>
  
  <p>Here's what the program is supposed to do:
  <a href="http://i.stack.imgur.com/XvYKC.png" rel="nofollow">image_program</a></p>
  
  <p>I hope that was clear as problem description, thank you in advance for helping.</p>