【转载】 python实现xml标注文件生成mask

python实现xml标注文件生成mask

数据集Medical Images for Nucleus Segmentation(TCGA,一个细胞核分割公共数据库,原图.tif,标注文件.xml)

xml文件结构
若干个(X,Y)坐标点所围成的区域(region)表示一个细胞核的mask;若干个region组成的regions即这幅图所有细胞核的mask。

<Annotations MicronsPerPixel="0.252000">
	<Annotation ...>
		<Attributes>
			...
		</Attributes>
		<Regions>
			<RegionAttributeHeaders>
				...
			</RegionAttributeHeaders>
			<Region Id="2" Type="0" Zoom="5" Selected="0" ImageLocation="" ImageFocus="0" Length="126.0" Area="1049.3" LengthMicrons="31.8" AreaMicrons="66.6" Text="" NegativeROA="0" InputRegionId="0" Analyze="1" DisplayId="2">
				<Attributes/>
				<Vertices>
					<Vertex X="96.400000" Y="70.800000"/>
					<Vertex X="96.600000" Y="71"/>
					...
				</Vertices>
			</Region>
			<Region Id="3" ..>
				...
			</Region>
			...
		</Regions>
		<Plots/>
	</Annotation>
</Annotations>

代码:

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

def xml2mask(filename, dir):

    mask = np.zeros([1000, 1000], dtype=np.uint8)
    
    xml = str(dir + "\\" + file)
    tree = ET.parse(xml)
    root = tree.getroot()
    regions = root.findall('Annotation/Regions/Region')
    for region in regions:
        points = []
        for point in region.findall('Vertices/Vertex'):
            x = float(point.attrib['X'])
            y = float(point.attrib['Y'])
            points.append([x, y])

        pts = np.asarray([points], dtype=np.int32)
        cv2.fillPoly(img=mask, pts=pts, color=255)
    cv2.imwrite("the\\dir\\you\\want\\to\\save\\to\\" + os.path.splitext(filename)[0] + ".png", mask)

dir = "the\\path\\to\\your\\xml\\files"
files = os.listdir(dir)
i = 0
for file in files:
    xml2mask(file, dir)
    i+=1
    print('已完成{0}幅图像!'.format(i))
print("全部完成!")

转换结果:
在这里插入图片描述

版权声明:本文为CSDN博主Grimpeur的原创文章。
原文链接:https://blog.csdn.net/qq_44823829/article/details/109054033

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值