自学内容网 自学内容网

使用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}')

垂直变换

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}')

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

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

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)
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)


原文地址:https://blog.csdn.net/weixin_53895623/article/details/142330534

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!