Rotate frame in cpp

I’m trying to rotate my frame by 90 degrees but it’s not quite working.

My camera is capturing x 640wx360h image and I’m setting the center of the rotation
at point 320,180 but the window isn’t showing the whole image.
What am I missing.

Here’s my rotate function:

Mat rotate(Mat src, double angle) 
    Mat dst;
    int X = src.cols/2.;
    int Y = src.rows/2.;
    char buf[12];
    int n=sprintf (buf, "x,y of rotation: %d, %d", X,Y);
    Point2f pt(X,Y);
    cout << cv::format("%s", buf) << endl; 
    Mat rotatedMat = getRotationMatrix2D(pt, angle, 1.0); 
    warpAffine(src, dst, rotatedMat, Size(src.cols, src.rows));
    return dst;

And here’s what I see when I run the code.

If I do not rotate the frame I see this.

  • there’s a builtin rotate function (for multiples of 90°)
  • if you rotate a 360x240 image 90°, it wont fit into the original shape (things get cut off, that’s what you see there), you have to do something about the dest size, like making it Size(src.rows, src.cols) for 90°

Ah, so it’s simpler to just use the rotate() function.

            rotate(frame, rotatedFrame,ROTATE_90_CLOCKWISE);
            imshow(pzRotatingWindowName, rotatedFrame);

And now I get what I want

1 Like