Rotating an image

In order to rotate the image, we make use of the cv.getRotationMatrix2D() function to build the 2 x 3 transformation matrix. This matrix rotates the image at the desired angle (in degrees), where positive values indicate a counterclockwise rotation. Both the center of rotation and the scale factor can also be adjusted. Using these elements in our example, the following transformation matrix is calculated:

This expression has the following values:

 

The following example builds the M transformation matrix to rotate 180 degrees with respect to the center of the image with a scale factor of 1 (without scaling). Afterwards, this M matrix is applied to the image, as follows:

height, width = image.shape[:2]
M = cv2.getRotationMatrix2D((width / 2.0, height / 2.0), 180, 1)
dst_image = cv2.warpAffine(image, M, (width, height))