欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图片旋转90,180,270

程序员文章站 2022-05-17 10:06:13
...
import cv2
import numpy as np
import os
def rotation(img,i,save_path,angle):
    width = img.shape[0]  # 长度
    height = img.shape[1]  # 宽度
    # 这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子
    # 可以通过设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题
    M = cv2.getRotationMatrix2D((width/2, height/2), angle, 1)
    # 第三个参数是输出图像的尺寸中心
    dst = cv2.warpAffine(img, M, (width, height))
    img_rotate_name = str(angle)+'_'+i
    img_rotate_path = os.path.join(save_path,img_rotate_name)
    cv2.imwrite(img_rotate_path, dst)
if __name__ =="__main__":
    paths = ('F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Fovea_Illustration_512/',
          'F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Glaucoma_512_012/',
          'F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Non-Glaucoma_512_012')

    # save_paths = ('F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Fovea_Illustration_512/rotate/',
    #       'F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Glaucoma_512_012/rotate/',
    #       'F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Non-Glaucoma_512_012/rotate')

    for path in paths:
        save_path = path+'rotate'
        name = os.listdir(path)
        for i in name:
            rotation_angle = [90, 180, 270]
            for angle in rotation_angle:
                image_path = os.path.join(path, i)
                image = cv2.imread(image_path, cv2.IMREAD_ANYCOLOR)
                img = rotation(image,i,save_path,angle)