title: OpenCV Inconsistent Image Rotation between Python and C++
body: <p>I am working on rotating an image 90 degrees CCW with OpenCV. I started with the Python implementation (<code>rotate.py</code>) and then converted it to C++ (<code>rotate.cpp</code>). Problem is I'm getting inconsistent results.
  I annotated both versions below around the line <code>getRotationMatrix2D(...)</code> on both to describe the inconsistency. If I don't follow the described methods below I get image that are rotated but shifted out of frame.</p>
  
  <p>I'm on OpenCV 2.4 and opencv-python 3.4.0.12. <br>
  EDIT: I've also tried with OpenCV 3.4 now with the same results.</p>
  
  <hr>
  
  <p>This image was rotated with <code>rotate.cpp</code> with the center of rotation at <code>Point(x/2, y/2)</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/c2OfQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c2OfQ.png" alt="Bad rotation"></a></p>
  
  <hr>
  
  <p>rotate.py</p>
  
  <pre><code>import sys
  import os
  import cv2
  
  i = sys.argv[1]
  im = cv2.imread(i)
  y,x = im.shape[:2]  
  
  # x/2, y/2 can be used for the center
  # but then the two lower are required
  # This follows a tutorial I read
  M = cv2.getRotationMatrix2D((x/2,y/2), 90, 1)
  M[0, 2] += (y / 2) - x/2
  M[1, 2] += (x / 2) - y/2
  
  # The center also works as x/2, x/2
  # but only if the two lines after are commented out
  M = cv2.getRotationMatrix2D((x/2,x/2), 90, 1)
  #M[0, 2] += (y / 2) - x/2
  #M[1, 2] += (x / 2) - y/2  
  
  im = cv2.warpAffine(im, M, (y, x))
  
  try:
      cv2.imwrite(i+'~'+'.png', im, params=(cv2.IMWRITE_PNG_COMPRESSION, 5))
  except:
      os.remove(i+'~'+'.png')
      continue
  
  try:
      os.rename(i+'~'+'.png', i)
  except KeyboardInterrupt:
      os.rename(i+'~'+'.png', i)
      raise
  </code></pre>
  
  <hr>
  
  <p>rotate.cpp</p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;iostream&gt;
  
  int main(int argc, char** argv) {
      const auto in_img = cv::imread(argv[1]);
      const auto in_size = in_img.size();
  
      const auto&amp; x = in_size.width;
      const auto&amp; y = in_size.height;
  
      decltype(in_img) out_img(x, y, in_img.type());
      const auto out_size = out_img.size();
  
      //       Only works if these are both x/2      v    v
      cv::Mat M = cv::getRotationMatrix2D(cv::Point(x/2, x/2), 90, 1.0);
      //M.at&lt;int&gt;(0, 2) += ((y/1) - x/2);
      //M.at&lt;int&gt;(1, 2) += ((x/1) - y/2);
      //   ^   Were not having any effect
  
      cv::warpAffine(in_img, out_img, M, out_size);
  
      cv::imwrite(std::string(argv[1]) + "~.png", out_img, {CV_IMWRITE_PNG_COMPRESSION, 5});
  
  }
  </code></pre>