拆分成mxn的图片

这篇文章介绍了使用Python对图像进行分割的方法,包括计算IOU(IntersectionoverUnion)来确定有效标注区域,以及转换坐标系统(中心坐标到标准坐标和反之)。代码展示了如何将大图分割成子图,并调整大小后保存到指定路径。
摘要由CSDN通过智能技术生成
# name_src_img: 原图的完整读取名称(含路径+文件名)
# name_src_label_txt:  标注文件的完整读取名称(含路径+文件名)
# path_dst_split_im: 子图的图像文件的目标存图路径
# path_dst_split_label_txt:  子图的标注文件的目标存图路径
# size_im_split: 子图从原图中切割的尺寸
# size_im_resize: 子图前缩放的尺寸
# step: 子图在原图中,每次移动的步长
# iou: 子图中被裁剪的标注区域相对于原来标注区域的面积占比小于iou,则为该子图的有效标注区域,否则无效

# 将合并后的图分开并重新保存标签的代码

import os

import cv2


def compute_iou(boxA, boxB):
    # 获取矩形框的坐标
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])

    # 计算交集面积和并集面积
    interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
    unionArea = boxAArea + boxBArea - interArea

    # 计算交集的坐标
    inter_box = (xA, yA, xB, yB)
    # 计算交并比
    iou = interArea / float(unionArea)

    return iou, interArea, inter_box


def convert_centre(size, bbox):
    """
    标准坐标转中心坐标
    将bbox的左上角点、右下角点坐标的格式,转换为bbox中心点 + bbox的w,h的格式,并进行归一化
    size: [weight, height]
    bbox: [Xmin, Ymin, Xmax, Ymax]
    即:xyxy(左上右下) ——> xywh(中心宽高)
    xyxy(左上右下):左上角的xy坐标和右下角的xy坐标
    xywh(中心宽高):边界框中心点的xy坐标和图片的宽度和高度
    """
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (bbox[0] + bbox[2]) / 2.0
    y = (bbox[1] + bbox[3]) / 2.0
    w = bbox[2] - bbox[0]
    h = bbox[3] - bbox[1]
    x = x * dw
    y = y * dh
    w = w * dw
    h = h * dh
    return x, y, w, h


def convert_stand(size, bbox):
    """""
    中心坐标转换成标准坐标
    将bbox中心点 + bbox的w,h的格式转换为,左上角点、右下角点坐标的格式
    size: [weight, height]
    bbox: [x,y,w,h]
    即:xywh(中心宽高) ——> xyxy(左上右下)
    xyxy(左上右下):左上角的xy坐标和右下角的xy坐标
    xywh(中心宽高):边界框中心点的xy坐标和图片的宽度和高度
    """""
    # 标准中心坐标
    x = bbox[0] * size[0]
    y = bbox[1] * size[1]
    w = bbox[2] * size[0]
    h = bbox[3] * size[1]
    # 标准坐标
    x_l = x - w / 2.0
    y_l = y - h / 2.0
    x_r = x + w / 2.0
    y_r = y + h / 2.0
    return x_l, y_l, x_r, y_r


def splitImage(path_src_img, path_dst_img, layout, resize_):
    for k, single_file in enumerate(sorted(os.listdir(path_src_img))):
        print(k)
        image_name = single_file.split('.')[0].split('-')
        # print(image_name)
        name_src_img = os.path.join(path_src_img, single_file)

        m = 0
        n = 0
        image = cv2.imread(name_src_img)
        width, height, channel = image.shape
        for i in range(0, width, int(width/layout[0])+1):
            m += 1
            for j in range(0, height, int(height/layout[1])+1):
                n += 1
                cropped = image[i:int(height/layout[1]) + i, j:int(width/layout[0]) + j]  # 按行和列切分
                path_dst_split_im = os.path.join(path_dst_img, '{}.png'.format(image_name[n-1]))
                cropped = cv2.resize(cropped, resize_)
                cv2.imwrite(path_dst_split_im, cropped)


path_src_img = r'C:\Users\Wilson\Desktop\merge_and_split\valid\merge_images'
path_dst_img = r'C:\Users\Wilson\Desktop\merge_and_split\valid\split_images'

if not os.path.exists(path_dst_img):
    os.mkdir(path_dst_img)

splitImage(path_src_img, path_dst_img, layout=(3, 3), resize_=(640, 213))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值