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

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

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

相关推荐
吕永强11 分钟前
人工智能与环境:守护地球的智能防线
人工智能·科普
兮℡檬,18 分钟前
房价预测|Pytorch
人工智能·pytorch·python
白-胖-子5 小时前
深入剖析大模型在文本生成式 AI 产品架构中的核心地位
人工智能·架构
想要成为计算机高手6 小时前
11. isaacsim4.2教程-Transform 树与Odometry
人工智能·机器人·自动驾驶·ros·rviz·isaac sim·仿真环境
静心问道7 小时前
InstructBLIP:通过指令微调迈向通用视觉-语言模型
人工智能·多模态·ai技术应用
宇称不守恒4.08 小时前
2025暑期—06神经网络-常见网络2
网络·人工智能·神经网络
小楓12018 小时前
醫護行業在未來會被AI淘汰嗎?
人工智能·醫療·護理·職業
数据与人工智能律师8 小时前
数字迷雾中的安全锚点:解码匿名化与假名化的法律边界与商业价值
大数据·网络·人工智能·云计算·区块链
chenchihwen8 小时前
大模型应用班-第2课 DeepSeek使用与提示词工程课程重点 学习ollama 安装 用deepseek-r1:1.5b 分析PDF 内容
人工智能·学习
说私域8 小时前
公域流量向私域流量转化策略研究——基于开源AI智能客服、AI智能名片与S2B2C商城小程序的融合应用
人工智能·小程序