python 批量生成xml标记文件(连通域坐标分割)
#!/usr/bin/python
# -*- coding=utf-8 -*-
# author : Manuel
# date: 2019-05-15 from xml.etree import ElementTree as ET
import numpy as np
from skimage import data,filters,segmentation,measure,morphology,color
from scipy.misc import imread
import os
from os import getcwd IMAGES_LIST=os.listdir('ls')#图片路径 #连通域分割,返回连通域坐标列表
def connected_domain_position_get(image):
coordinates_list=[]#创建坐标列表
thresh = filters.threshold_otsu(image) # 阈值分割,自动返回一个阈值
bw = morphology.closing(image > thresh,
morphology.square(3)) # (image > thresh, morphology.square(3)) #闭运算#将0,1转换成bool
cleared = bw.copy() # 复制
segmentation.clear_border(cleared) # 清除与边界相连的目标物
label_image = measure.label(cleared) # 连通区域标记
borders = np.logical_xor(bw, cleared) # 逻辑异或
label_image[borders] = -1 # ?
# image_label_overlay = color.label2rgb(label_image, image=image) # 不同标记用不同颜色显示
for region in measure.regionprops(label_image): # 循环得到每一个连通区域属性集
# 忽略小区域
if region.area < 1000:
continue
# print(region.bbox)
# 绘制外包矩形
minr, minc, maxr, maxc = region.bbox
# rect = mpatches.Rectangle((minc - 10, minr - 10), maxc - minc + 20, maxr - minr + 20,
# fill=False, edgecolor='red',
# linewidth=2) # mpatches.Rectangle(矩形左上顶点坐标(x,y), width, height)
left = minc - 10
upper = minr - 10
right = maxc + 10
lower = maxr + 10
coordinates_list.append([left,upper,right,lower])#将每组连通域坐标添加进坐标列表
return coordinates_list#返回连通域坐标列表 #创建一级分支object
def create_object(root,xi,yi,xa,ya):#参数依次,树根,xmin,ymin,xmax,ymax
#创建一级分支object
_object=ET.SubElement(root,'object')
#创建二级分支
name=ET.SubElement(_object,'name')
name.text='AreaMissing'
pose=ET.SubElement(_object,'pose')
pose.text='Unspecified'
truncated=ET.SubElement(_object,'truncated')
truncated.text=''
difficult=ET.SubElement(_object,'difficult')
difficult.text=''
#创建bndbox
bndbox=ET.SubElement(_object,'bndbox')
xmin=ET.SubElement(bndbox,'xmin')
xmin.text='%s'%xi
ymin = ET.SubElement(bndbox, 'ymin')
ymin.text = '%s'%yi
xmax = ET.SubElement(bndbox, 'xmax')
xmax.text = '%s'%xa
ymax = ET.SubElement(bndbox, 'ymax')
ymax.text = '%s'%ya #创建xml文件
def create_tree(image_name):
global annotation
# 创建树根annotation
annotation = ET.Element('annotation')
#创建一级分支folder
folder = ET.SubElement(annotation,'folder')
#添加folder标签内容
folder.text=('ls') #创建一级分支filename
filename=ET.SubElement(annotation,'filename')
filename.text=image_name.strip('.jpg') #创建一级分支path
path=ET.SubElement(annotation,'path')
path.text=getcwd()+'/ls/%s'%image_name#用于返回当前工作目录 #创建一级分支source
source=ET.SubElement(annotation,'source')
#创建source下的二级分支database
database=ET.SubElement(source,'database')
database.text='Unknown' #创建一级分支size
size=ET.SubElement(annotation,'size')
#创建size下的二级分支图像的宽、高及depth
width=ET.SubElement(size,'width')
width.text=''
height=ET.SubElement(size,'height')
height.text=''
depth = ET.SubElement(size,'depth')
depth.text = '' #创建一级分支segmented
segmented = ET.SubElement(annotation,'segmented')
segmented.text = '' def main():
for image_name in IMAGES_LIST:
#只处理jpg文件
if image_name.endswith('jpg'):
#将图像通过连通域分割,得到连通域坐标列表,该列表的形式[[a,b,c,d],[e,f,g,h]...,]
image = color.rgb2gray(imread(os.path.join(r'./ls', image_name)))
coordinates_list = connected_domain_position_get(image)
create_tree(image_name) for coordinate_list in coordinates_list:
create_object(annotation, coordinate_list[0], coordinate_list[1], coordinate_list[2], coordinate_list[3])
# if coordinates_list==[]:
# break
# 将树模型写入xml文件
tree = ET.ElementTree(annotation)
tree.write('ls/%s.xml' % image_name.strip('.jpg')) if __name__ == '__main__':
main()
注:xml中所有值必须是字符串,否则报错
python 批量生成xml标记文件(连通域坐标分割)的更多相关文章
- 用python批量生成简单的xml文档
最近生成训练数据时,给一批无效的背景图片生成对应的xml文档,我用python写了一个简单的批量生成xml文档的demo,遇见了意外的小问题,记录一下. 报错问题为:ImportError: No m ...
- php 批量生成html,txt文件的方法(实例代码)
php批量生成html,txt文件的实现代码. 首先,建立一个conn.php 链接数据库. <?php $link = mysql_connect("mysql_host" ...
- dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性
dom4j 为生成 XML 的文件添加 xmlns(命名空间) 属性 分类: Java2011-06-03 16:14 976人阅读 评论(0) 收藏 举报 xml扩展语言 今天在开发sitemap地 ...
- 如何用python批量生成真实的手机号码
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:Python测试社区 1目 标 场 景 平时在工作过程中,偶尔会需要大 ...
- Python批量生成用户名
写在最前 平时在工作中尤其是在做压测的时候难免需要一些用户名和密码,写个简单的Python小脚本批量生成一些 代码示例 import random,string #生成大小字母和数字一起的大字符串 a ...
- python批量生成SQL语句
1,首先写一条能运行成功插入SQL的语句 INSERT INTO sign_guest(realname,phone,email,sign,event_id)VALUES("jack&quo ...
- Python项目生成requirements.txt文件及pip升级问题解决及流程
缘由:新项目使用Python, PC上的python包不全,需要通过requirements.txt文件指定安装所需包 pip安装遇到一些坑 一.直接使用pip包管理工具生成requirements. ...
- java使用jdom生成xml格式文件
本文生成xml使用的工具是jdom.jar,下载地址如下: 链接:https://eyun.baidu.com/s/3slyHgnj 密码:0TXF 生成之后的文档格式类型,就如上面的图片一样,简单吧 ...
- 批量生成xml文件数据C#实现
方法一 // < Records count = "5" > //< Record > // < Contact_ID > 5 - 55W - ...
随机推荐
- 《C程序设计语言》笔记(三)
六:结构 1:结构体声明中,比如: struct point{ int x; int y; }; struct后面的名字是可选的,称为结构标记.结构成员.结构标记和普通变量可以采用相同的名字,它们之间 ...
- 【New Feature】阿里云快照服务技术解析
一.背景 目前上云已经成为行业发展趋势,越来越多的企业级客户将业务系统和数据库迁移到云上.而传统的备份一体机/备份软件方式,并不适合云上ECS.RDS等产品的备份与容灾服务.阿里云块存储服务提供云 ...
- 【NS2】Installing ns-2.29 in Ubuntu 12.04
Installing ns-2.29 in Ubuntu 12.04 Off late, we try to use(install) a old software in a new Oper ...
- LeetCode114 Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. (Medium) For example,Given 1 / \ 2 5 / \ ...
- 初始化Redis密码
在配置文件/etc/redis/redis.conf中有个参数: requirepass 这个就是配置redis访问密码的参数: 比如 requirepass test123: (需重启Redis才能 ...
- C# —— 访问修饰符
1.public 公有的,任何代码均可以访问,应用于所有类或成员. 2.internal 内部的,只能在当前程序集中使用,应用于所有类或成员. 3.protected internal 受保护的内部成 ...
- Mac OS X 常用快捷键一览
- @codeforces - 117C@ Cycle
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个竞赛图(有向完全图),请找出里面的某个三元环,或者判断不 ...
- uniapp点击底部tabbar不跳转页面
一个项目,其设想是这样的,当我进入页面,发现有新版本,提示用户之后,用户点击确定跳转到下载页面. 弹出框要用自己封装的,因为uniapp的弹出框不同的手机上展示的样子不一样,领导的是华为(在这里悄悄吐 ...
- 前端开发之BOM和DOM(转载)
BOM BOM:是指浏览器对象模型,它使JavaScript可以和浏览器进行交互. 1,navigator对象:浏览器对象,通过这个对象可以判定用户所使用的浏览器,包含了浏览器相关信息. naviga ...