数据增强——图像旋转

目标:利用CV2通过对图像旋转,实现图像扩增

功能1:图像旋转

函数: 构建旋转矩阵:M=cv2.getRotationMatrix2D((w // 2, h // 2),-90, 1)

                        第一个参数为旋转中心,第二个为旋转角度(负数:顺时针旋转,正数:逆时针旋转),第三个为旋转后的缩放因子

                                    img=cv2.warpAffine(img,M,(2*cols,2*rows))    # 第三个参数是输出图像的尺寸中心

   参考文档 : https://cloud.tencent.com/developer/article/1347695

旋转90度目标框坐标

原目标框:左上角(w0,h0) 右下角(w1,h1)

图像旋转后:(w_0=H-h0-h,h_0=w0),(w_1=H-h1+h,h_1=w1)

代码功能:

扫描二维码关注公众号,回复: 9472649 查看本文章
def img_rot(image_path,annotation_path,image_save_path,annotation_save_path):
    list_img = os.listdir(image_path)
    path_img = [os.path.join(image_path, path) for path in list_img]
    num_i = 20000000
    for path in path_img:
        num_i += 1
        txt = os.path.basename(path).replace('.jpg', '')  # 返回文件名
        txt_path = txt + '.txt'
        annot_path = os.path.join(annotation_path, txt_path)
        with open(annot_path, encoding='utf-8') as fp:
            text = fp.read()
            data = text.split(' ')
        if len(data) == 6:
            img = cv2.imread(path)
            img_h, img_w, _ = img.shape
            # cv2.flip(img, 1)
            # 图像旋转
            if img_w > img_h:
                padding = (img_w - img_h) // 2
                center = (img_w // 2, img_w // 2)
                img_padded = np.zeros(shape=(img_w, img_w, 3), dtype=np.uint8)
                img_padded[padding:padding + img_h, :, :] = img

                M = cv2.getRotationMatrix2D(center, -90, 1)
                rotated_padded = cv2.warpAffine(img_padded, M, (img_w, img_w))
                img = rotated_padded[:, padding:padding + img_h, :]

            elif img_w < img_h:
                padding = (img_h - img_w) // 2
                center = (img_h // 2, img_h // 2)
                # plt_1 = (img_h - 546 - 116, 422)
                # plt_2 = (img_h - 662 + 116, 496)
                img_padded = np.zeros(shape=(img_h, img_h, 3), dtype=np.uint8)
                # img_padded[padding:padding + h, :, :] = img
                img_padded[:, padding:padding + img_w, :] = img
                M = cv2.getRotationMatrix2D(center, -90, 1)
                rotated_padded = cv2.warpAffine(img_padded, M, (img_h, img_h))
                img = rotated_padded[padding:padding + img_w, :, :]
            img_name = 'core_battery' + str(num_i) + '.jpg'
            cv2.imwrite(os.path.join(image_save_path, img_name), img)
            # w_0,h_0,w_1,h_1=int(data[2]),int(data[3]),int(data[4]),int(data[5])
            w_0, h_0, w_1, h_1 = int(data[2]), int(data[3]), int(data[4]), int(data[5])
            annot_w = w_1 - w_0
            annot_h = h_1 - h_0
            w_0_0 = img_h - h_0 - annot_h
            h_0_0 = w_0
            w_1_0 = img_h - h_1 + annot_h
            h_1_0 = w_1
            string = data[0] + ' ' + data[1] + ' ' + str(w_0_0) + ' ' + str(h_0_0) + ' ' + str(w_1_0) + ' ' + str(h_1_0)
            txt_name = 'core_battery' + str(num_i) + '.txt'

            with open(os.path.join(annotation_save_path, txt_name), 'w') as fp:
                fp.write(string)
if __name__ == '__main__':
    image_save_path = 'C:\data\core_500\image_new'             #处理后的图像保存位置
    annotation_save_path = 'C:\data\core_500/annotation_new'   #处理后的目标框保存位置
    annotation_path = 'C:\data\core_500\Annotation'            #原图像保存位置
    image_path = 'C:\data\core_500\Image'                      #原目标框保存位置
    # 图像翻转
    img_flip(image_path,annotation_path,image_save_path,annotation_save_path)
    # 图像旋转
    img_rot(image_path, annotation_path, image_save_path, annotation_save_path)
发布了109 篇原创文章 · 获赞 22 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42233538/article/details/103501785