Rotate image by 90, 180 or 270 degrees

后端 未结 11 2287
一个人的身影
一个人的身影 2020-12-01 07:48

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);         


        
11条回答
  •  广开言路
    2020-12-01 08:16

    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
    

提交回复
热议问题