自制数据集处理

1. 视频抽帧

每90帧抽一帧

import cv2
import os
# 处理视频为图片

def extract_frames(video_path, output_folder, frame_interval=30):
    # 打开视频文件
    video_capture = cv2.VideoCapture(video_path)
    # 确保视频文件已成功打开
    if not video_capture.isOpened():
        print("Error: Unable to open video file.")
        return

    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    frame_count = 0
    while True:
        # 读取一帧
        ret, frame = video_capture.read()
        if not ret:
            break

        # 在每个指定的帧间隔保存帧
        if frame_count % frame_interval == 0:
            # 保存当前帧为图像文件
            frame_filename = os.path.join(output_folder, f"frame_{frame_count}.jpg")
            cv2.imwrite(frame_filename, frame)

        frame_count += 1

    # 释放视频捕获对象
    video_capture.release()


# 视频文件路径
video_path = "maize_tassel.mp4"
# 输出文件夹路径
output_folder = ("maize_tassel")
# 每30帧抽取一张图片
extract_frames(video_path, output_folder, frame_interval=90)

2. 图像裁剪

裁剪5120*2700图像为8份1280*1280图像

import os
from PIL import Image

def crop_images(input_folder, output_folder, crop_width, crop_height):
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 获取输入文件夹中的所有图像文件
    images = [f for f in os.listdir(input_folder) if f.endswith(('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'png'))]

    for image_file in images:
        # 打开图像
        with Image.open(os.path.join(input_folder, image_file)) as img:
            img_width, img_height = img.size

            # 确保图像尺寸为5120x2700
            if img_width != 5120 or img_height != 2700:
                print(f"图像 {image_file} 尺寸不符合要求,跳过裁剪。")
                continue

            # 裁剪并保存图像
            for i in range(2):  # 高度方向裁剪2个块
                for j in range(4):  # 宽度方向裁剪4个块
                    left = j * crop_width
                    top = i * crop_height
                    right = left + crop_width
                    bottom = top + crop_height

                    cropped_img = img.crop((left, top, right, bottom))
                    cropped_img_name = f"{os.path.splitext(image_file)[0]}_crop_{i}_{j}.png"
                    cropped_img.save(os.path.join(output_folder, cropped_img_name))
                    print(f"保存裁剪图像: {cropped_img_name}")

# 示例用法
input_folder = './maize1'  # 输入文件夹路径
output_folder = './maize_split_8'  # 输出文件夹路径
crop_width = 1280  # 裁剪宽度
crop_height = 1280  # 裁剪高度

crop_images(input_folder, output_folder, crop_width, crop_height)

3. 图像画框

将一个voc格式的xml文件中的框画在一个图像中。

import os
import cv2
import xml.etree.ElementTree as ET


def draw_bounding_boxes(image_path, xml_path, output_path):
    # 读取图像
    image = cv2.imread(image_path)
    if image is None:
        print(f"Error: Unable to open image file {image_path}")
        return

    # 解析XML文件
    tree = ET.parse(xml_path)
    root = tree.getroot()

    # 获取所有object标签
    for obj in root.findall('object'):
        # 获取bndbox标签
        bndbox = obj.find('bndbox')
        if bndbox is None:
            continue

        # 提取边框坐标
        xmin = int(bndbox.find('xmin').text)
        ymin = int(bndbox.find('ymin').text)
        xmax = int(bndbox.find('xmax').text)
        ymax = int(bndbox.find('ymax').text)

        # 在图像上绘制红色边框,线条宽度为2
        cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 0, 255), 10)

    # 保存结果图像
    cv2.imwrite(output_path, image)
    print(f"Output saved to {output_path}")


def process_directories(xml_dir, img_dir, output_dir):
    # 确保输出目录存在
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # 遍历XML目录中的所有文件
    for xml_file in os.listdir(xml_dir):
        if xml_file.endswith('.xml'):
            xml_path = os.path.join(xml_dir, xml_file)
            img_filename = xml_file.replace('.xml', '.jpg')
            image_path = os.path.join(img_dir, img_filename)

            if not os.path.exists(image_path):
                img_filename = xml_file.replace('.xml', '.png')
                image_path = os.path.join(img_dir, img_filename)

            if os.path.exists(image_path):
                output_path = os.path.join(output_dir, img_filename)
                draw_bounding_boxes(image_path, xml_path, output_path)
            else:
                print(f"Warning: No image file found for {xml_file}")


# 示例用法
xml_dir = './outputs'  # 替换为包含XML标签文件的目录路径
img_dir = './images'  # 替换为包含图像文件的目录路径
output_dir = './end'  # 替换为输出图像的保存目录路径

process_directories(xml_dir, img_dir, output_dir)

相关推荐

  1. 自制数据处理

    2024-07-19 04:36:02       20 阅读
  2. 自然语言处理-用于预训练词嵌入的数据

    2024-07-19 04:36:02       60 阅读
  3. 自动驾驶数据waymo

    2024-07-19 04:36:02       63 阅读
  4. 数据视频编码(自用)

    2024-07-19 04:36:02       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 04:36:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 04:36:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 04:36:02       58 阅读
  4. Python语言-面向对象

    2024-07-19 04:36:02       69 阅读

热门阅读

  1. layui前端开发-记录一次弹窗嵌套表格功能的开发

    2024-07-19 04:36:02       20 阅读
  2. oracle 查询锁 && 解锁

    2024-07-19 04:36:02       21 阅读
  3. 初识Redis

    2024-07-19 04:36:02       18 阅读
  4. redis setnx使用方法

    2024-07-19 04:36:02       18 阅读
  5. 微服务

    微服务

    2024-07-19 04:36:02      17 阅读
  6. perf工具学习材料

    2024-07-19 04:36:02       17 阅读
  7. opencv—常用函数学习_“干货“_3

    2024-07-19 04:36:02       17 阅读
  8. k8s学习——升级后的k8s使用私有harbor仓库

    2024-07-19 04:36:02       26 阅读
  9. LVS的DR模式

    2024-07-19 04:36:02       20 阅读
  10. 前端常用工具库

    2024-07-19 04:36:02       19 阅读
  11. 智能灯光的工作原理

    2024-07-19 04:36:02       20 阅读
  12. 安全防御:防火墙基本模块

    2024-07-19 04:36:02       21 阅读
  13. Qt区分鼠标按下时移动的是哪个多边形

    2024-07-19 04:36:02       19 阅读