I need to rotate an image by either 90, 180 or 270 degrees. In OpenCV4Android I can use:
Imgproc.getRotationMatrix2D(new Point(center, center), degrees, 1);
Here is my Python translation (and thanks to all the posters):
import cv2
def rot90(img, rotflag):
""" rotFlag 1=CW, 2=CCW, 3=180"""
if rotflag == 1:
img = cv2.transpose(img)
img = cv2.flip(img, 1) # transpose+flip(1)=CW
elif rotflag == 2:
img = cv2.transpose(img)
img = cv2.flip(img, 0) # transpose+flip(0)=CCW
elif rotflag ==3:
img = cv2.flip(img, -1) # transpose+flip(-1)=180
elif rotflag != 0: # if not 0,1,2,3
raise Exception("Unknown rotation flag({})".format(rotflag))
return img