You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于imgaug的图像加载与保存问题及数据集增强咨询

解决imgaug图像加载与保存问题的指南

Hey there! Let's work through your imgaug image loading and saving issues since you're diving into CNN training with a small dataset on your HPC cluster. I’ll break this down clearly for your two questions:

问题1:实现图像的加载与保存(转为数组)

The official imgaug examples use pre-defined image arrays, so you’ll need to add code to load your folder of JPGs into arrays first, then save the augmented results back to files. Since you already have OpenCV 2 installed, let’s start with a reliable cv2-based solution that plays nicely with imgaug:

完整代码示例

import os
import cv2
import imgaug as ia
from imgaug import augmenters as iaa

# 1. 加载文件夹中的所有JPG图像为数组
def load_images_from_folder(folder_path):
    images = []
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(".jpg"):  # 兼容大小写的后缀判断
            img_path = os.path.join(folder_path, filename)
            # cv2默认读取为BGR格式,imgaug支持BGR,但转成RGB更符合多数CV库习惯
            img = cv2.imread(img_path)
            if img is not None:
                img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                images.append(img_rgb)
    return images

# 2. 定义图像增强序列(沿用官方示例的增强逻辑)
seq = iaa.Sequential([
    iaa.Fliplr(0.5),  # 50%概率水平翻转
    iaa.GaussianBlur(sigma=(0, 3.0))  # 随机高斯模糊,sigma范围0到3
])

# 3. 执行加载、增强、保存流程
input_folder = "/path/to/your/input/images"  # 替换成你的图像文件夹路径
output_folder = "/path/to/save/augmented/images"  # 替换成保存增强图像的路径

# 创建输出文件夹(如果不存在)
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 加载原始图像
raw_images = load_images_from_folder(input_folder)
# 执行增强
augmented_images = seq(images=raw_images)

# 保存增强后的图像
for idx, img_aug in enumerate(augmented_images):
    # 转回BGR格式,因为cv2.imwrite默认保存为BGR
    img_aug_bgr = cv2.cvtColor(img_aug, cv2.COLOR_RGB2BGR)
    save_path = os.path.join(output_folder, f"augmented_{idx}.jpg")
    cv2.imwrite(save_path, img_aug_bgr)

关于你想用的scipy方案

If you really want to use scipy.ndimage.imread and scipy.misc.imsave, the most likely issue is missing dependencies or version mismatches in your Python 2.7 environment:

  • scipy.ndimage.imread was deprecated in newer scipy versions, but it’s available in scipy 0.19.x and earlier (common in Python 2.7 setups).
  • scipy.misc.imsave relies on PIL/Pillow, so you’ll need to have that installed locally.

Here’s a scipy-based snippet if you want to test it:

import os
from scipy.ndimage import imread
from scipy.misc import imsave
import imgaug as ia
from imgaug import augmenters as iaa

def load_images_scipy(folder_path):
    images = []
    for filename in os.listdir(folder_path):
        if filename.lower().endswith(".jpg"):
            img_path = os.path.join(folder_path, filename)
            img = imread(img_path, mode="RGB")  # 指定RGB格式
            images.append(img)
    return images

# 增强逻辑和保存部分类似,保存时直接用imsave
raw_images = load_images_scipy(input_folder)
augmented_images = seq(images=raw_images)

for idx, img_aug in enumerate(augmented_images):
    save_path = os.path.join(output_folder, f"augmented_scipy_{idx}.jpg")
    imsave(save_path, img_aug)

问题2:选择合适的图像读写接口

Based on your setup (Python 2.7, HPC cluster, no virtual environments), here’s a breakdown of your options:

  • OpenCV (cv2): Top recommendation

    • You already have it installed, so no extra setup needed.
    • Blazing fast for image operations, supports all common image formats.
    • Perfect compatibility with imgaug (imgaug was designed to work seamlessly with cv2 arrays).
    • The only minor gotcha is handling BGR/RGB conversion, which we covered in the code above.
  • PIL/Pillow: Great alternative

    • Intuitive API, excellent format support, and stable in Python 2.7.
    • If you run into cv2 issues (e.g., weird color artifacts), this is a reliable fallback.
    • scipy.misc.imsave actually uses PIL under the hood, so installing Pillow will fix most scipy-related save errors.
  • Scipy: Not recommended for your scenario

    • As mentioned, imread is deprecated in newer scipy versions, and imsave has strict PIL dependencies that are easy to break on HPC clusters.
    • Offers no real advantages over cv2 or Pillow for basic image read/write tasks.

关于Excel存储图像

Storing actual image data in Excel is not recommended—it’s inefficient, has size limits, and makes loading images a huge hassle. If your Excel file contains image file paths instead, you can use the xlrd library to read the paths, then load the images using cv2/PIL as shown above.

内容的提问来源于stack exchange,提问作者Rainer Zufall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:10:34