title: OpenCV: rotating video frames
body: <p>I am trying to rotate video frames captured using <code>libcamera-vid</code> from <a href="https://github.com/raspberrypi/libcamera-apps" rel="nofollow noreferrer">libcamera-apps</a> an arbitrary amount of degrees using OpenCV's <a href="https://docs.opencv.org/4.5.1/da/d54/group__imgproc__transform.html#ga0203d9ee5fcd28d40dbc4a1ea4451983" rel="nofollow noreferrer">warpAffine()</a>.</p>
  <p>The frames are as far as I can understand on yuv420 planar format.</p>
  <p>The gist of what I'm doing is:</p>
  <pre><code>#include &lt;opencv2/imgproc.hpp&gt;
  #include &lt;opencv2/core.hpp&gt;
  
  cv::Point2f Ycenter = cv::Point2f(width / 2.0f, height / 2.0f);
  cv::Point2f UVcenter = cv::Point2f(width / 4.0f, height / 4.0f);
  double rotation = 0;
  cv::Mat Ytransform = cv::getRotationMatrix2D(Ycenter, rotation, 1.0);
  cv::Mat UVtransform = cv::getRotationMatrix2D(UVcenter, rotation, 1.0);
  
  int Uoffset = height*width;
  int Voffset = 5*info.height*info.width/4;
  
  cv::Size Ysize(info.height, info.width);
  cv::Size UVsize(info.height / 2, info.width / 2);
  
  for (unsigned int count = 0; ; count++)
  {
      // ...
      // Wait for frame here
      // ...    
      
      // Acquire buffer in which frame is stored:
      uint8_t* buffer = getFrameBuffer(); // Simplification, but not important
      
      double rot = floor(count / 10);
          
      if (10*rot != rotation)
      {
          rotation = 10*rot;
          Ytransform = cv::getRotationMatrix2D(Ycenter, rotation, 1.0);
          UVtransform = cv::getRotationMatrix2D(UVcenter, rotation, 1.0);
      }
  
          
      cv::Mat Y(Ysize.height, Ysize.width, CV_8UC1, buffer);
      cv::Mat U(UVsize.height, UVsize.width, CV_8UC1, buffer + Uoffset);
      cv::Mat V(UVsize.height, UVsize.width, CV_8UC1, buffer + Voffset);
          
  
      cv::warpAffine(Y, Y, Ytransform, Ysize);
      cv::warpAffine(U, U, UVtransform, UVsize);
      cv::warpAffine(V, V, UVtransform, UVsize);
  
      sendFrameToEncoder(buffer); // Also a simplification, also not important as far as i know
  }
  </code></pre>
  <p>Where <code>height</code>, <code>width</code> are the height and width of the video frame.</p>
  <p>However, this produces wierd, warped images. Something is clearly rotated, but not the correctly.</p>
  <p>rotation = 0 produces:
  <a href="https://i.stack.imgur.com/7ujyI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7ujyI.png" alt="Non-rotated image" /></a></p>
  <p>rotation = 10 produces:
  <a href="https://i.stack.imgur.com/vccJF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vccJF.jpg" alt="&quot;Rotated&quot; 10 degrees" /></a></p>
  <p>rotation = 20 produces:
  <a href="https://i.stack.imgur.com/GNmG9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GNmG9.jpg" alt="&quot;Rotated&quot; 20 degrees" /></a></p>
  <p>rotation = 30 produces:
  <a href="https://i.stack.imgur.com/OAnqK.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OAnqK.jpg" alt="&quot;Rotated&quot; 30 degrees" /></a></p>
  <p>So, it clearly isn't working correctly. Does anyone know what's going wrong here?</p>
  <p>I'm using OpenCV version 4.5.1
  On a Raspberry Pi Zero W running Raspberry Pi OS Bullseye</p>