import os
import cv2
# labelimg生成的txt文件中的格式为(classes x y w h),(类别,锚框中心点x坐标,锚框中心点y坐标,锚框宽w,锚框宽h),小数是归一化后的值
files_path = "/Data/file/imagepath"
output_dir = "/Data/file/resultpath"
def extract_annotations(image_path, annotation_path, output_dir):
# 读取图像
image = cv2.imread(image_path)
image_height, image_width = image.shape[:2]
with open(annotation_path, 'r') as file:
lines = file.readlines()
for line in lines:
parts = line.strip().split()
class_name = int(parts[0])
if class_name == 2 or class_name == 0:
xmax_float = float(parts[1])
ymax_float = float(parts[2])
xmin_float = float(parts[3])
ymin_float = float(parts[4])
# 将小数值转换为整数坐标
x1max = int(xmax_float * image_width)
y1max = int(ymax_float * image_height)
x1min = int(xmin_float * image_width)
y1min = int(ymin_float * image_height)
将目标检测的标注框数据分割出来
于 2023-10-31 10:50:38 首次发布
该代码定义了一个函数extract_annotations,用于从labelimg生成的txt文件中读取图像的标注信息,然后将图片按标注分割并保存。脚本遍历指定文件夹下的.jpg和.txt文件进行处理。

最低0.47元/天 解锁文章
1474

被折叠的 条评论
为什么被折叠?



