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

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

其实这也不影响训练模型,可能对模型训练效果的影响也是微乎其微的,于是大多数情况我会选择直接用整张图片训练模型。但是,有的情况下,去掉白边模型训练效果好,不去白边模型某个类别效果就不好。比如图中的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的数值不就改乱了嘛。

相关推荐
陈广亮14 分钟前
构建具有长期记忆的 AI Agent:从设计模式到生产实践
人工智能
会写代码的柯基犬23 分钟前
DeepSeek vs Kimi vs Qwen —— AI 生成俄罗斯方块代码效果横评
人工智能·llm
Mintopia1 小时前
OpenClaw 是什么?为什么节后热度如此之高?
人工智能
爱可生开源社区1 小时前
DBA 的未来?八位行业先锋的年度圆桌讨论
人工智能·dba
叁两4 小时前
用opencode打造全自动公众号写作流水线,AI 代笔太香了!
前端·人工智能·agent
前端付豪4 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
strayCat232554 小时前
Clawdbot 源码解读 7: 扩展机制
人工智能·开源
程序员打怪兽4 小时前
详解Visual Transformer (ViT)网络模型
深度学习
王鑫星4 小时前
SWE-bench 首次突破 80%:Claude Opus 4.5 发布,Anthropic 的野心不止于写代码
人工智能