【数据转换】对mask图像数据转为xml标签数据

关键词: python 、mask 、 xml 、opencv 、IRSTD-1K、NUDT-SIRST

背景

  Mask图像数据通常是通过某种分割算法或工具生成的,其中每个像素都被赋予了一个标签,表示其所属的目标或区域。最近我图省事情,在网上找了一些红外小目标的开源数据集,但是自己的任务是做目标检测,所以需要对mask图像数据转为xml格式数据。

xml与mask分析

  XML格式的标注数据内容在深度学习、计算机视觉等领域中扮演着重要的角色。在图像标注领域,XML文件通常用于存储图像中各个目标或区域的标签信息。 XML格式的标注数据内容通常包含以下几个关键部分:

  1. 文件头部
  2. 图像信息
  3. 目标或区域列表
    • 目标或区域属性
  4. 其他辅助信息

样例:

shell 复制代码
<annotation>
    <folder>aa</folder>
    <filename>XDU1.png</filename>
    <path>C:/Users/kiven/Desktop/aa/XDU1.png</path>
    <source>
       <database>Unknown</database>
    </source>
    <size>
       <width>512</width>
       <height>512</height>
       <depth>1</depth>
    <segmented>0</segmented>
    <object>
       <name>1</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
          <xmin>113</xmin>
          <ymin>251</ymin>
          <xmax>125</xmax>
          <ymax>265</ymax>
       </bndbox>
    </object>
    <object>
       <name>1</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
          <xmin>107</xmin>
          <ymin>290</ymin>
          <xmax>110</xmax>
          <ymax>296</ymax>
       </bndbox>
    </object>
</annotation>

  对于IRSTD-1K和NUDT-SIRST数据集在这里查看后得到:mask图像为二值图,黑底白色表示目标区域。

转换

  通过分析mask图像我们可以很清晰得到目标区域为白色区域(二值图),因此我们可以利用opencv中的 cv2.findContours函数得到白色目标,然后计算外接最小矩形即可得到目标区域坐标。然后根据xml信息进行逐步填充信息进行调试即可:

shell 复制代码
import os
import cv2

def put_xml(xml_name, img_name, img_dir, width, height, lab_objects):
    # write in xml file
    xml_file = open("C:/Users/kiven/Desktop/VOC2007/" + xml_name, 'w')
    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>VOC2007</folder>\n')
    xml_file.write('    <filename>' + img_name + '</filename>\n')
    xml_file.write('    <path>' + img_dir + '</path>\n')
    xml_file.write('    <size>\n')
    xml_file.write('        <width>' + str(width) + '</width>\n')
    xml_file.write('        <height>' + str(height) + '</height>\n')
    xml_file.write('        <depth>3</depth>\n')
    xml_file.write('    </size>\n')

    for lab_object in lab_objects:

        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + "0" + '</name>\n')
        xml_file.write('        <pose>Unspecified</pose>\n')
        xml_file.write('        <truncated>0</truncated>\n')
        xml_file.write('        <difficult>0</difficult>\n')
        xml_file.write('        <bndbox>\n')
        xml_file.write('            <xmin>' + str(lab_object[0]) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(lab_object[1]) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(lab_object[2]) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(lab_object[3]) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')

    xml_file.write('</annotation>')
    xml_file.close()


def make_xml(img_path, img_name):
    img_dir = img_path + "/" + img_name
    # 读取图像
    image = cv2.imread(img_dir, cv2.IMREAD_GRAYSCALE)
    height, width = image.shape[:2]
    # 查找所有的连通区域
    contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # 遍历所有的连通区域
    boxx = []
    for contour in contours:
        # 计算每个连通区域的最小外接矩形
        x, y, w, h = cv2.boundingRect(contour)
        # 打印最小外接矩形的坐标和大小
        print(f"白色区域的最小外接矩形坐标: ({x}, {y}),宽度: {w},高度: {h}")
        boxx.append([x, y, x + w, y + h])
    return img_dir, boxx, height, width


if __name__ == '__main__':
    img_path = "C:/Users/kiven/Desktop/VOC2007"
    for img_name in os.listdir(img_path):
        img_dir, boxx, height, width = make_xml(img_path, img_name)
        xml_name = img_name[:-4] + ".xml"
        put_xml(xml_name, img_name, img_dir, width, height, boxx)

结语

  行!有了这个代码生成是xml标注信息,后续的实验数据又新增了不少

相关推荐
BK贝壳4 小时前
亿级用户签到位图选型复盘:用 bool-hybrid-array 扛冷启动打卡,也把它和 Roaring/Redis 的边界测清楚了
数据结构·python·性能优化·numpy
2601_966949656 小时前
如何根据研究的起止时间截取分钟级行情?Python 实战与 QuantDash 数据处理指南
开发语言·python·pandas·量化交易·1024程序员节·股票数据·quantdash
在世修行10 小时前
智学文枢:页面切换与跨页数据预填
python·桌面应用·signal·1024程序员节·pyside·跨页跳转·状态预填
2601_9628857210 小时前
如何用 Python 把 K 线重采样成任意周期?
开发语言·python
打工仔折腾 AI10 小时前
在 Ubuntu ARM64 部署 Q1 V3.0 MuJoCo 模型:加载、单步推理与执行器响应解析
linux·运维·人工智能·python·深度学习·ubuntu·性能优化
信誓旦旦的程序猿11 小时前
【Python量化实战宝典 #21】北向资金跟随手册:聪明钱择时信号(Python 可跑)
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
严谨的麻辣烫11 小时前
静态 ISP 代理请求超时怎么排查?Python 与 curl 实战
python·http·requests·1024程序员节·网络排障
SelectDB12 小时前
从 StarRocks 走向 Apache Doris:如何实现引擎平滑替换与架构升级?
大数据·数据库·数据分析
KeepRobots13 小时前
11-01 温哥华不回拨:用 60 行 Python 查你的工单系统会不会差一小时(KeepRobots 工程笔记)
python
xiao77mwq13 小时前
你以为 OCR 慢?实测 81.3 s 里全是故障代价:如何把「定格态」消掉,让耗时从 6.5 s 到 13 倍差距显出原形
python