时频图数据集更正程序,去除坐标轴白边及调整对应的标签值

当数据集是时频图时可能有一个尴尬的问题,就是数据集制作好后,发现有白边。

其实这也不影响训练模型,可能对模型训练效果的影响也是微乎其微的,于是大多数情况我会选择直接用整张图片训练模型。但是,有的情况下,去掉白边模型训练效果好,不去白边模型某个类别效果就不好。比如图中的BPSK和Frank信号。

一开始我设置416*416的神经网络输入大小,甚至BPSK,Frank信号检测出来的概率特别低,10个中有2个的样子?上面的检测结果是640*640大小训练出来的,虽然信号检测出来的,BPSK,Frank这种特别窄的信号置信度很低,见下图。

这种情况白边就不是可留可不留的了,是必须要去掉。白边占的面积还是挺大的。

裁剪图片并不难,难的是还要对应修正labels中的数值。于是,我写了一个更正程序,既可以裁剪图片,也可以修正labels。

python 复制代码
#作者:zhouzhichao
#时间:25年7月4日
#功能:裁剪数据集的图片和调整对应标签数值

import os
from PIL import Image, ImageDraw

# origin_img_path = ""
# target_img_path = ""
# origin_label_path = ""
# target_label_path = ""

origin_width = 875
origin_height = 656

x1 = 114
x2 = 791
y1 = 49
y2 = 583

after_cut_width = x2 - x1
after_cut_height = y2 - y1

def cut_img(target_path):


    # 设置文件夹路径
    folder_path = target_path
    output_folder = target_path

    # 如果输出文件夹不存在,则创建
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 获取文件夹中的所有图片
    for filename in os.listdir(folder_path):
        if filename.endswith(('.png', '.jpg', '.jpeg')):  # 根据需要修改图片格式
            img_path = os.path.join(folder_path, filename)
            img = Image.open(img_path)

            # 裁剪区域:左上角(114, 49)到右下角(791, 583)
            cropped_img = img.crop((x1, y1, x2, y2))

            # 保存裁剪后的图片到输出文件夹
            output_path = os.path.join(output_folder, filename)
            cropped_img.save(output_path)

    print("裁剪完成!")

def modify_label(labels_folder):
    output_folder = labels_folder
    for filename in os.listdir(labels_folder):
        if filename.endswith('.txt'):
            txt_path = os.path.join(labels_folder, filename)
            with open(txt_path, 'r') as file:
                lines = file.readlines()

            # 修改后的数据将存储在这里
            modified_lines = []

            for line in lines:
                parts = line.strip().split()  # 拆分每一行
                class_id = parts[0]
                x_center = float(parts[1])
                y_center = float(parts[2])
                width = float(parts[3])
                height = float(parts[4])

                # 修改第2列为0.5,第4列为1
                modified_y_center = (y_center*origin_height-y1)/after_cut_height
                modified_y_height = height*origin_height/after_cut_height


                # 拼接修改后的行
                modified_line = f"{class_id} {0.5:.3f} {modified_y_center:.3f} {1:.3f} {modified_y_height:.3f}\n"
                modified_lines.append(modified_line)

            # 保存修改后的文件
            output_txt_path = os.path.join(output_folder, filename)
            with open(output_txt_path, 'w') as output_file:
                output_file.writelines(modified_lines)

def watch(images_folder,labels_folder,output_folder):
    for filename in os.listdir(labels_folder):
        if filename.endswith('.txt'):
            txt_path = os.path.join(labels_folder, filename)

            # 获取对应的图片文件名
            img_filename = filename.replace('.txt', '.jpg')
            img_path = os.path.join(images_folder, img_filename)

            # 打开图片
            img = Image.open(img_path)
            draw = ImageDraw.Draw(img)

            # 读取标签文件
            with open(txt_path, 'r') as file:
                lines = file.readlines()

            # 遍历每一行标签,绘制矩形框
            for line in lines:
                parts = line.strip().split()  # 拆分每一行
                class_id = int(parts[0])
                x_center = float(parts[1]) * img.width
                y_center = float(parts[2]) * img.height
                width = float(parts[3]) * img.width
                height = float(parts[4]) * img.height

                # 计算矩形框的左上角和右下角坐标
                x1 = x_center - width / 2
                y1 = y_center - height / 2
                x2 = x_center + width / 2
                y2 = y_center + height / 2

                # 绘制矩形框,使用红色边框
                draw.rectangle([x1, y1, x2, y2], outline="white", width=2)

            # 保存带框的图片
            output_img_path = os.path.join(output_folder, img_filename)
            img.save(output_img_path)

if __name__ == "__main__":
    img_path = "D:\english\yolov7\datasets_higher_cut\images\\train"
    label_path = "D:\english\yolov7\datasets_higher_cut\labels\\train"
    output_img_path = "D:\english\yolov7\datasets_higher_cut\watch"
    # cut_img(img_path)
    # modify_label(label_path)
    watch(img_path, label_path, output_img_path)

其中,cut_img是裁剪图片的,modify_label是更正标签的,watch是检测更正结果的。

①裁剪效果:

每张图片原本大小是875*656,把左上角114,49到右下角的791,583,覆盖原图。

②label更正效果,原标签:

更正后标签:

主要是把这些数字中的第2列都改成0.5,第4列改成1,第3列改成原来的数值称原图片高度减去y1后除以裁剪后的图片高度。最后一列改成原值乘以原图高度除以裁剪后的图片高度。

③检查效果

最后,用这个程序需要注意,对一个文件架不能使用两次,那图片不就被裁了2次嘛,label的数值不就改乱了嘛。

相关推荐
回眸&啤酒鸭1 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智1 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅1 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein1 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu1 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb1 天前
智能网联汽车安全能力框架
人工智能
龙亘川1 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI1 天前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai