You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用YOLO标注数据裁剪图片?

基于YOLO格式标注数据裁剪图片的Python实现

标注数据解析

你提供的标注数据 0 0.514583 0.716204 0.136458 0.102778 是YOLO格式的目标标注,各字段对应含义:

  • 0:目标类别ID
  • 0.514583:目标框中心x坐标(相对于图片宽度的归一化值)
  • 0.716204:目标框中心y坐标(相对于图片高度的归一化值)
  • 0.136458:目标框宽度(相对于图片宽度的归一化值)
  • 0.102778:目标框高度(相对于图片高度的归一化值)

实现代码

使用Python的PIL库即可完成图片裁剪,代码如下:

from PIL import Image

# 替换为你的图片路径
img_path = "rUROf.jpg"
img = Image.open(img_path)
img_width, img_height = img.size

# 标注数据
label = [0, 0.514583, 0.716204, 0.136458, 0.102778]
_, x_center, y_center, bbox_w, bbox_h = label

# 把归一化坐标转为像素坐标
x_start = int((x_center - bbox_w / 2) * img_width)
y_start = int((y_center - bbox_h / 2) * img_height)
x_end = int(x_start + bbox_w * img_width)
y_end = int(y_start + bbox_h * img_height)

# 裁剪并保存
cropped_img = img.crop((x_start, y_start, x_end, y_end))
cropped_img.save("cropped_result.jpg")
# 可选:直接查看裁剪结果
# cropped_img.show()

注意事项

  • 先安装依赖库:执行 pip install pillow
  • 如果标注数据存储在txt文件中,可通过文件读取批量处理,示例:
    with open("标注文件.txt", "r") as f:
        for line in f.readlines():
            label = list(map(float, line.strip().split()))
            # 后续裁剪逻辑同上
    

内容的提问来源于stack exchange,提问作者Jony Ghosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 15:12:21