python对标注的坐标进行扩展

本文介绍了一个使用Python脚本批量处理XML文件中坐标的方法,通过读取XML文件中的宽度和高度信息,然后对xmin和xmax坐标进行相应的数值转换,适用于图像处理和标注数据修正的场景。
#coding=utf-8
##此文件放在要写入文件的文件夹里
import glob
import re

aa=[]
aa=glob.glob(r'D:\sales\all_label\*.xml')#读入所有的xml文件,得到a列表
print(aa) #aa列表存储所有xml文件名

for a in aa:
	with open(a, 'r', encoding='UTF-8') as fo:
		contents = fo.readlines() #把所有行,存进contents列表里

	for content in contents:#找宽和高
		
		width=re.search('<width>(.*)</width>',content) 
		if width:#如果非空
			w=int(width.group(1))  #宽度
			
		height=re.search('<height>(.*)</height>',content) 
		if height:
			h=int(height.group(1))  #高度
			break #找完高度跳出循环
			
	for content in contents:	#content是一行的字符串
		
		xmin=re.search('<xmin>(.*)</xmin>', content) 
		if xmin:#如果非空
			content = re.sub('\d+',str(w-int(xmin.group(1))), content)#\d+代表替换掉数字 
		
		xmax=re.search('<xmax>(.*)</xmax>', content) 
		if xmax:#如果非空
			content = re.sub('\d+',str(w-int(xmax.group(1))), content) 
			
		#下面是逐行写入
		newname=a.lstrip("D:\\sales\\all_label\\") #去掉前缀,不会改变a自身
		with open('11_'+newname, 'a', encoding='UTF-8') as fs:
			fs.write(content)



### Python 中实现棋盘格坐标 为了创建一个带有坐标的棋盘格图像,在给定的代码基础上可以进一步扩展功能来显示或保存具有坐标的图片。下面是一个完整的例子,它不仅会生成黑白相间的棋盘图案而且会在每个方格内标注行列编号。 #### 创建带坐标的棋盘格图像 ```python import cv2 import numpy as np def create_chessboard_with_coordinates(width, height, square_size=30): """ 创建指定大小和方格尺寸的棋盘图,并添加坐标标签 参数: width (int): 棋盘宽度方向上的方格数量 height (int): 棋盘高度方向上的方格数量 square_size (int): 单个方格的实际像素长度,默认为30px 返回: ndarray: 包含文字标记的棋盘图像数据数组 """ # 初始化空白画布 board_width = width * square_size board_height = height * square_size canvas = np.zeros((board_height, board_width, 3), dtype=np.uint8) font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 thickness = 1 text_color = (0, 255, 0) # Green color BGR format for row in range(height): y_start = row * square_size for col in range(width): x_start = col * square_size # 绘制交替颜色背景 if (row + col) % 2 == 0: cv2.rectangle(canvas, pt1=(x_start, y_start), pt2=(x_start + square_size, y_start + square_size), color=(255, 255, 255), thickness=-1) # 添加坐标文本 coord_text = f'({col},{row})' ((text_w, text_h), _) = cv2.getTextSize(coord_text, font, font_scale, thickness) org_x = max(x_start + int(square_size / 2 - text_w / 2), 0) org_y = min(y_start + int(square_size / 2 + text_h / 2), board_height - 1) cv2.putText(canvas, text=coord_text, org=(org_x, org_y), fontFace=font, fontScale=font_scale, color=text_color, thickness=thickness) return canvas if __name__ == '__main__': img = create_chessboard_with_coordinates(12, 9)[^1] cv2.imshow('Chess Board with Coordinates', img) key = cv2.waitKey(0) if key & 0xFF == ord('s'): cv2.imwrite('./chessboard_with_coords.png', img) cv2.destroyAllWindows() ``` 此脚本定义了一个函数`create_chessboard_with_coordinates()`用于构建所需的棋盘图形并为其加上坐标说明。通过调整输入参数中的`width`, `height`以及`square_size`变量可改变最终输出图像的具体规格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值