使用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)
相关推荐
子午几秒前
水果识别系统Python+卷积神经网络算法+人工智能+深度学习+计算机课设项目+TensorFlow+模型训练
人工智能·python·深度学习
卑微的Coder3 分钟前
python画正方形、平行四边形、六边形、五角星、风车(四个半圆)
开发语言·python
一只会敲代码的小灰灰10 分钟前
python学习第十节:爬虫基于requests库的方法
爬虫·python·学习
Adolf_199315 分钟前
Flask-JWT-Extended登录验证
后端·python·flask
William数据分析36 分钟前
[Python]案例驱动最佳入门:Python数据可视化在气候研究中的应用
python·信息可视化·数据
唤醒手腕1 小时前
2024年最新 Python 大数据网络爬虫技术基础案例详细教程(更新中)
开发语言·爬虫·python
zhangbin_2371 小时前
【Python机器学习】NLP信息提取——正则模式
开发语言·人工智能·python·深度学习·机器学习·自然语言处理
小李飞刀李寻欢1 小时前
mongoDB 读取数据python版本实现
数据库·python·mongodb·数据
大脸猫吖1 小时前
Selenium4.0实现自动搜索功能
python·selenium·自动化
你可以自己看1 小时前
python中数据处理库,机器学习库以及自动化与爬虫
python·机器学习·自动化