改变图片的大小,标签中box也跟着改变大小(附代码)

import cv2
import os

picture_file_name_list = os.listdir('./pic/')  # 图片路径, 返回图片文件名字列表
picture_path = './pic/'  # 图片路径
label_txt_path = './xml/'  # 标签路径
save_picture_path = './new_pic/'  # 保存新的图片路径
save_label_txt_path = './new_txt/'  # 保存新的标签路径


def get_box(file_path):
    """
        :param file_path: 文件路径
        :return: box_list 4-7
    """
    with open(file_path, 'r') as f:
        line = f.readlines()
    box_list = list()
    for i in line:
        label_split = i.split(" ")
        box_list.append([label_split[4], label_split[5], label_split[6], label_split[7]])

    return box_list


def write_new_box_to_kitti_txt(file_path, box_list, save_file_path=save_label_txt_path):
    """
    :param file_path: 文件名称
    :param box_list: 新 box 的位置 type:str
    :param save_file_path: 修改文件之后保存的位置
    :return: None
    """
    with open(file_path, 'r') as f:
        line = f.readlines()

    changed_box_label = list()
    object_index = 0
    for i in line:
        label_split = i.split(" ")
        label_split[4] = box_list[object_index][0]
        label_split[5] = box_list[object_index][1]
        label_split[6] = box_list[object_index][2]
        label_split[7] = box_list[object_index][3]
        changed_box_label.append(label_split)
        object_index += 1

    file_name = os.path.basename(file_path)  # 获取文件名
    with open(save_file_path + file_name, 'w') as f:
        for i in changed_box_label:
            str_list = [str(l) for l in i]
            j = " ".join(str_list)
            f.write(j)


def save_picture(file_name):
    """
    :param picture_path: 要保存的图片名称
    :return: None
    """
    img = cv2.imread(picture_path + file_name)
    img = cv2.resize(img, (1024, 1024))
    print(file_name)
    cv2.imwrite(save_picture_path + file_name, img)


def show_new_box(input_image):
    img_ori = cv2.imread(input_image)
    height_old, width_old = img_ori.shape[:2]  # 原始图片的尺寸
    img_ori = cv2.resize(img_ori, tuple([1024, 1024]))
    height_new, width_new = img_ori.shape[:2]  # 新图片的尺寸

    file_name = os.path.basename(input_image)
    box_list = get_box(label_txt_path + file_name[:-4] + '.txt')
    for i in range(len(box_list)):
        box_list[i][0] = round((float(box_list[i][0]) * (float(width_new) / float(width_old))), 1)
        box_list[i][2] = round((float(box_list[i][2]) * (float(width_new) / float(width_old))), 1)
        box_list[i][1] = round((float(box_list[i][1]) * (float(height_new) / float(height_old))), 1)
        box_list[i][3] = round((float(box_list[i][3]) * (float(height_new) / float(height_old))), 1)

    for i in range(len(box_list)):
        img = cv2.rectangle(img_ori, (int(box_list[i][0]), int(box_list[i][1])),
                            (int(box_list[i][2]), int(box_list[i][3])),
                            (255, 0, 0), 2)
    # 保存新的label文件中
    file_base_name = os.path.basename(input_image)
    write_new_box_to_kitti_txt(label_txt_path + file_base_name[:-4] + ".txt", box_list)
    # 保存新的图片
    save_picture(file_base_name)

    cv2.imshow("new_picture_and_new_box_position", img)
    cv2.waitKey(0)


def show_box(input_image):
    """
    0a0b3620e96151d1.jpg     4 个物体
    /m/03fp41 0 0 0 10.5 650.1 133.6 983.6 0 0 0 0 0 0 0
    /m/03fp41 0 0 0 99.5 669.9 388.9 937.2 0 0 0 0 0 0 0
    /m/03fp41 0 0 0 353.6 682.6 722.8 921.4 0 0 0 0 0 0 0
    /m/03fp41 1 0 0 758.1 685.6 1022.7 970.7 0 0 0 0 0 0 0
    """
    box_list = [[10.5, 650.1, 133.6, 983.6, 4]]
    # box_list = [[99.5, 669.9, 388.9, 937.2, 4]]
    # box_list = [[353.6, 682.6, 722.8, 921.4, 4]]
    # box_list = [[758.1, 685.6, 1022.7, 970.7, 4]]

    image = cv2.imread(input_image)
    image = cv2.resize(image, (1024, 1024))
    for bbox in box_list:
        image = cv2.rectangle(image, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2)

    cv2.imshow("old_picture_and_old_box_position", image)
    cv2.waitKey(0)


if __name__ == '__main__':
    for i in picture_file_name_list:
        show_new_box(picture_path + i)

文件格式
在这里插入图片描述

发布了74 篇原创文章 · 获赞 47 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_40639095/article/details/103522310