将目标检测的标注框数据分割出来

该代码定义了一个函数extract_annotations,用于从labelimg生成的txt文件中读取图像的标注信息,然后将图片按标注分割并保存。脚本遍历指定文件夹下的.jpg和.txt文件进行处理。
import os
import cv2

# labelimg生成的txt文件中的格式为(classes x y w h),(类别,锚框中心点x坐标,锚框中心点y坐标,锚框宽w,锚框宽h),小数是归一化后的值

files_path = "/Data/file/imagepath"
output_dir = "/Data/file/resultpath"

def extract_annotations(image_path, annotation_path, output_dir):
    # 读取图像
    image = cv2.imread(image_path)
    image_height, image_width = image.shape[:2]

    with open(annotation_path, 'r') as file:
        lines = file.readlines()
  
    for line in lines:
        parts = line.strip().split()
        class_name = int(parts[0])
        if class_name == 2 or class_name == 0:
            xmax_float = float(parts[1])
            ymax_float = float(parts[2])
            xmin_float = float(parts[3])
            ymin_float = float(parts[4])

            # 将小数值转换为整数坐标
            x1max = int(xmax_float * image_width)
            y1max = int(ymax_float * image_height)
            x1min = int(xmin_float * image_width)
            y1min = int(ymin_float * image_height)
       
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky169

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值