使用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)
相关推荐
WIN赢3 分钟前
【性能测试入门_01性能测试jmeter基础实操场景详解】
ide·python·pycharm
请为小H留灯1 小时前
Python中很常用的100个函数整理
开发语言·python
音视频牛哥2 小时前
SmartMediakit在四足机器人和无人机巡检中的创新应用方案
人工智能·机器学习·计算机视觉
七月初七772 小时前
Excel多级联动下拉菜单设置
python·excel·pandas
Serendipity_Carl3 小时前
Pandas数据清洗实战之清洗猫眼电影
python·pycharm·数据分析·pandas
.昕..3 小时前
(二)seacmsv9注入管理员账号密码+orderby+limit
python·网络安全
音视频牛哥4 小时前
如何在Python下实现摄像头|屏幕|AI视觉算法数据的RTMP直播推送
人工智能·opencv·计算机视觉
HerrFu4 小时前
可狱可囚的爬虫系列课程 17:lxml模块的使用
爬虫·python
SecPulse4 小时前
AI开源竞赛与硬件革命:2025年3月科技热点全景解读——阿里、腾讯领跑开源,英特尔、台积电重塑算力格局
人工智能·科技·opencv·自然语言处理·开源·语音识别
码叔义4 小时前
X509TrustManager信任SSL证书
python·网络协议·ssl