使用python对图像批量水平变换和垂直变换

水平变换

python 复制代码
import os
import cv2

# 设置源文件夹路径
src_folder = r'C:\Users\TJ\Desktop\tmp\origin'

# 设置目标文件夹路径
dst_folder = r'C:\Users\TJ\Desktop\tmp\enhanced'

# 确保目标文件夹存在
if not os.path.exists(dst_folder):
    os.makedirs(dst_folder)

# 遍历源文件夹中的图像文件
for filename in os.listdir(src_folder):
    # 检查是否为图像文件
    if filename.endswith('.jpg') or filename.endswith('.png'):
        # 构造源图像路径
        src_path = os.path.join(src_folder, filename)

        # 读取图像
        img = cv2.imread(src_path)

        # 对图像进行水平翻转
        flipped_img = cv2.flip(img, 1)

        # 构造目标图像路径
        dst_path = os.path.join(dst_folder, 'horizontalflip_' + filename)

        # 保存翻转后的图像
        cv2.imwrite(dst_path, flipped_img)

        print(f'Saved flipped image: {dst_path}')

垂直变换

python 复制代码
import os
import cv2

# 设置源文件夹和目标文件夹路径
src_folder = r'C:\Users\TJ\Desktop\tmp\origin'
dst_folder = r'C:\Users\TJ\Desktop\tmp\enhanced'

# 创建目标文件夹(如果不存在)
os.makedirs(dst_folder, exist_ok=True)

# 遍历源文件夹中的图像文件
for filename in os.listdir(src_folder):
    # 检查是否为图像文件
    if filename.endswith(('.jpg', '.png', '.bmp')):
        # 构建源文件路径和目标文件路径
        src_path = os.path.join(src_folder, filename)

        # 读取图像
        img = cv2.imread(src_path)

        # 执行垂直变换
        flipped_img = cv2.flip(img, 0)  # 0表示垂直翻转
        dst_path = os.path.join(dst_folder, 'verticalflip_' + filename)

        # 将变换后的图像保存到目标文件夹
        cv2.imwrite(dst_path, flipped_img)
        print(f'Saved flipped image: {dst_path}')

需要根据实际情况替换路径

对于深度学习,若将图像进行变换,变换后的图像的标注文件也需要进行调整

python 复制代码
import os

# 指定原始标注文件所在目录
orig_annot_dir = r''
# 指定翻转后图像所在目录
flipped_img_dir = r''

for annot_file in os.listdir(orig_annot_dir):
    annot_path = os.path.join(orig_annot_dir, annot_file)

    # 读取原始标注文件
    with open(annot_path, 'r') as f:
        lines = f.readlines()

    new_lines = []
    for line in lines:
        components = line.strip().split()
        print(f"Line: {line}")
        print(f"Components: {components}")

        class_id = components[0]
        x_center = float(components[1])
        y_center = float(components[2])
        width = float(components[3])
        height = float(components[4])

        # 水平翻转 x 坐标
        x_center_new = 1 - x_center

        # 将新坐标写入新的标注行
        new_line = f"{class_id} {x_center_new:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n"
        new_lines.append(new_line)

    # 将新标注写入文件
    img_name = os.path.splitext(annot_file)[0]
    new_annot_path = os.path.join(flipped_img_dir, f"{img_name}.txt")
    with open(new_annot_path, 'w') as f:
        f.writelines(new_lines)
python 复制代码
import os

# 指定原始标注文件所在目录
orig_annot_dir = r''
# 指定垂直翻转后图像所在目录
flipped_img_dir = r''

for annot_file in os.listdir(orig_annot_dir):
    annot_path = os.path.join(orig_annot_dir, annot_file)

    # 读取原始标注文件
    with open(annot_path, 'r') as f:
        lines = f.readlines()

    new_lines = []
    for line in lines:
        components = line.strip().split()
        class_id = components[0]
        x_center = float(components[1])
        y_center = float(components[2])
        width = float(components[3])
        height = float(components[4])

        # 垂直翻转 y 坐标
        y_center_new = 1 - y_center

        # 将新坐标写入新的标注行
        new_line = f"{class_id} {x_center:.6f} {y_center_new:.6f} {width:.6f} {height:.6f}\n"
        new_lines.append(new_line)

    # 将新标注写入文件
    img_name = os.path.splitext(annot_file)[0]
    new_annot_path = os.path.join(flipped_img_dir, f"{img_name}.txt")
    with open(new_annot_path, 'w') as f:
        f.writelines(new_lines)
相关推荐
小王子10241 小时前
设计模式Python版 组合模式
python·设计模式·组合模式
Mason Lin3 小时前
2025年1月22日(网络编程 udp)
网络·python·udp
清弦墨客3 小时前
【蓝桥杯】43697.机器人塔
python·蓝桥杯·程序算法
RZer5 小时前
Hypium+python鸿蒙原生自动化安装配置
python·自动化·harmonyos
CM莫问6 小时前
什么是门控循环单元?
人工智能·pytorch·python·rnn·深度学习·算法·gru
查理零世6 小时前
【算法】回溯算法专题① ——子集型回溯 python
python·算法
struggle20257 小时前
2025开源DouyinLiveRecorder全平台直播间录制工具整合包,多直播同时录制、教学直播录制、教学视频推送、简单易用不占内存
图像处理·计算机视觉·开源·音视频·语音识别
圆圆滚滚小企鹅。7 小时前
刷题记录 HOT100回溯算法-6:79. 单词搜索
笔记·python·算法·leetcode
纠结哥_Shrek7 小时前
pytorch实现文本摘要
人工智能·pytorch·python
李建军8 小时前
TensorFlow 示例摄氏度到华氏度的转换(二)
人工智能·python·tensorflow