当前位置: 首页 > article >正文

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


http://www.kler.cn/news/314554.html

相关文章:

  • 深度学习参数管理
  • MySQL-DDL/DML(数据定义/操作语言)
  • GIS开发之如何使用OpenLayers,Leaflet,Mapbox,Cesium,ArcGIS, Turf.js 与 D3.js
  • 【Webpack--00802】配置Babel语法兼容
  • 【图像检索】基于Gabor特征的图像检索,matlab实现
  • Python面试宝典第50题:分割等和子集
  • Vscode、插件历史版本下载
  • [数据结构与算法·C++] 笔记 1.4 算法复杂性分析
  • [附源码]SpringBoot+VUE+Java实现人脸识别系统
  • 实战指南:深度剖析Servlet+JSP+JDBC技术栈下的用户CRUD操作
  • 探秘 Web Bluetooth API:连接蓝牙设备的新利器
  • 828华为云征文|Flexus X实例GitLab部署构建流水线-私人一体化代码仓库~
  • AWS账号可以共用吗?
  • vue 中互相不关联的两个组件怎么进行通信(数据传输)
  • MFC获取网页的html文本
  • 视频V4改进
  • 锐捷 睿易路由器存在RCE漏洞
  • 会声会影2025视频剪辑教学
  • 开源集成开发环境搭建之VSCode安装部署教程
  • MySQL:基本查询操作
  • java计算机毕设课设—土地档案管理系统(附源码、文章、相关截图、部署视频)
  • 基于Java的SSM(Spring、Spring MVC、MyBatis)框架构建的远程诊断系统
  • 论文阅读 - MDFEND: Multi-domain Fake News Detection
  • 探索iPhone一键删除重复照片的方法
  • Kafka 为什么这么快?
  • 某乐指数爬虫逆向分析
  • Qemu开发ARM篇-2、uboot交叉编译
  • Android14 手机蓝牙配对后阻塞问题解决
  • python 自动化测试接口
  • 递归快速获取机构树型图