[自用代码]根据SSDD official的xml文件生成VOC格式mask

感谢官方提出了inshore offshore的sub set, 希望能够规范后续SAR ship detection的论文.
这里结合链接1 链接2 实现对于ssdd official中船只分割VOC格式的mask生成. 具体代码如下:

# -*- coding: utf-8 -*-
import numpy as np
import cv2
import os
import xml.etree.ElementTree as ET

def create_dir_not_exist(path):
    if not os.path.exists(path):
        os.mkdir(path)

 
def xml2mask(image_path, xml, id2class_dict):
    class2id_dict = id2class_dict
    tree = ET.parse(xml)
    root = tree.getroot()
 
    img_size = cv2.imread(image_path).shape
    height = img_size[0]
    weight = img_size[1]
 
    mask = np.zeros([height, weight], dtype=np.uint8)
 
    for instance in root.iter('object'):
        classname = instance.findall('name')[0].text
        cnt_points = []
        for pt in instance.iter('segm'):
            for xy in pt.findall('point'):
                ptx,pty = float(str(xy.text).split(',')[0]),float(str(xy.text).split(',')[1])
                cnt_points.append([ptx, pty])

        pts = np.asarray([cnt_points], dtype=np.int32)
        cv2.fillPoly(img=mask, pts=pts, color=class2id_dict[str(classname)])
 
    return mask

if __name__=='__main__':


    dir = "Annotations"
    img_dir = 'JPEGImages'
    id2class_dict = {'ship':(1,0,0)}

    out_dir = 'SegmentationClass'
    create_dir_not_exist(out_dir)

    files = os.listdir(dir)
    i = 0
    for file in files:
        xml_path = os.path.join(dir,file)
        img_name = str(file.split('.')[0]) + '.jpg'
        img_path = os.path.join(img_dir,img_name)
        if os.path.exists(img_path) and os.path.exists(xml_path):
            # print(xml_path)
            mask = xml2mask(img_path, xml_path, id2class_dict)
            cv2.imwrite(out_dir+ '\\' + os.path.splitext(file)[0] + ".png", mask)
            i+=1
            print('已完成{0}幅图像!'.format(i))
    print("全部完成!")

参考文献:
labelme xml 标注文件输出掩码图片
python实现xml标注文件生成mask
Zhang, T.; Zhang, X.; Li, J.; Xu, X.; Wang, B.; Zhan, X.; Xu, Y.; Ke, X.; Zeng, T.; Su, H.; Ahmad, I.; Pan, D.; Liu, C.; Zhou, Y.; Shi, J.; Wei, S. SAR Ship Detection Dataset (SSDD): Official Release and Comprehensive Data Analysis. Remote Sens. 2021, 13, 3690. https://doi.org/10.3390/rs13183690

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

deyiwang89

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

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

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

打赏作者

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

抵扣说明:

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

余额充值