标注txt格式是这样的(序号 长半轴 短半轴 角度 中心坐标x y)
import cv2
# import numpy as np
# 定义画椭圆的函数
def draw_ellipse(major_axis, minor_axis, angle, x, y, file, file1):
# # 创建一个黑色背景图像
# img = np.zeros((512, 512, 3), np.uint8)
# 指定一个背景图像
img = cv2.imread(file1)
# 定义椭圆中心、长轴长度和短轴长度
center = (round(x), round(y))
axes = (round(major_axis), round(minor_axis))
# 定义椭圆的旋转角度(逆时针方向)(我这里数据为百分比)
angle = angle*90
# 定义椭圆的颜色和线宽
color = (255, 255, 255)
thickness = 1 #-1可以将椭圆变实心
# 在图像上绘制椭圆
cv2.ellipse(img, center, axes, angle, 0, 360, color, thickness)
# 保存图像
cv2.imwrite(file, img)
# 显示图像
# cv2.imshow('image', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# 读取txt文件
with open('D:\Desktop\DAGM\Class1_def\Class1_def/label.txt', 'r') as f:
line = f.readline()
# 循环读取每一行数据
while line:
# 删掉空格
line = line.strip(" ").split()
# 背景图片路径
filename1 = 'Class1_def/' + line[0] + '.png'
# 以图像名作为文件名
filename = 'Mask/'+line[0] + '.jpg'
# 读取椭圆数据
major_axis = float(line[1])
minor_axis = float(line[2])
angle = float(line[3])
x = float(line[4])
y = float(line[5])
# (创建黑色图像)并画椭圆
draw_ellipse(major_axis, minor_axis, angle, x, y, filename, filename1)
line = f.readline()