数据集预处理

1.目标

将数据集图像通过预处理方法调整为统一大小尺寸,以便于后续模型提取特征。

2.常见的图像数据预处理方法

2.1图像尺度变换

1.图像缩放(使用OpenCV库):

python 复制代码
import cv2

def resize_image(image, width, height):
    resized_image = cv2.resize(image, (width, height))
    return resized_image

# 调整图像大小为300x300
resized_image = resize_image(image, 300, 300)

2.图像裁剪:

随机裁剪(使用PIL库):

python 复制代码
from PIL import Image
import numpy as np

def random_crop(image, crop_size):
    image = np.array(image)
    height, width = image.shape[:2]

    x = np.random.randint(0, width - crop_size[0] + 1)
    y = np.random.randint(0, height - crop_size[1] + 1)

    cropped_image = image[y:y + crop_size[1], x:x + crop_size[0]]
    return Image.fromarray(cropped_image)

# 随机裁剪图像为200x200
cropped_image = random_crop(image, (200, 200))

中心裁剪(使用PIL库):

python 复制代码
from PIL import Image

def center_crop(image, crop_size):
    width, height = image.size
    left = (width - crop_size[0]) // 2
    top = (height - crop_size[1]) // 2
    right = left + crop_size[0]
    bottom = top + crop_size[1]

    cropped_image = image.crop((left, top, right, bottom))
    return cropped_image

# 中心裁剪图像为200x200
cropped_image = center_crop(image, (200, 200))

3.图像翻转(使用OpenCV库):

水平翻转:

python 复制代码
import cv2

def horizontal_flip(image):
    flipped_image = cv2.flip(image, 1)
    return flipped_image

# 水平翻转图像
flipped_image = horizontal_flip(image)

垂直翻转:

python 复制代码
import cv2

def vertical_flip(image):
    flipped_image = cv2.flip(image, 0)
    return flipped_image

# 垂直翻转图像
flipped_image = vertical_flip(image)

4.图像旋转(使用PIL库,最近邻插值):

python 复制代码
from PIL import Image

def rotate_image(image, angle):
    rotated_image = image.rotate(angle, resample=Image.NEAREST)
    return rotated_image

# 将图像逆时针旋转90度
rotated_image = rotate_image(image, -90)

5.图像平移(使用OpenCV库):

python 复制代码
import cv2
import numpy as np

def translate_image(image, shift_x, shift_y):
    rows, cols = image.shape[:2]
    M = np.float32([[1, 0, shift_x], [0, 1, shift_y]])
    translated_image = cv2.warpAffine(image, M, (cols, rows))
    return translated_image

# 将图像水平平移10个像素,垂直平移20个像素
translated_image = translate_image(image, 10, 20)

灰度化(使用OpenCV库):

python 复制代码
import cv2

def convert_to_grayscale(image):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return gray_image

# 将彩色图像转换为灰度图像
gray_image = convert_to_grayscale(image)

图像增强(使用PIL库):

python 复制代码
from PIL import ImageEnhance

def enhance_image(image, factor):
    enhancer = ImageEnhance.Contrast(image)
    enhanced_image = enhancer.enhance(factor)
    return enhanced_image

# 增强图像对比度为1.5倍
enhanced_image = enhance_image(image, 1.5)
  • 数据增强(使用Keras库的ImageDataGenerator):

    python 复制代码
    from keras.preprocessing.image import ImageDataGenerator
    
    # 创建一个ImageDataGenerator对象来进行数据增强
    datagen = ImageDataGenerator(
        rotation_range=20,  # 随机旋转图像的角度范围
        width_shift_range=0.1,  # 随机水平平移图像的比例
        height_shift_range=0.1,  # 随机垂直平移图像的比例
        shear_range=0.2,  # 随机剪切变换的强度
        zoom_range=0.2,  # 随机缩放图像的范围
        horizontal_flip=True,  # 随机水平翻转图像
        fill_mode='nearest'  # 填充图像的方法
    )
    
    # 使用数据增强生成批量的图像数据
    augmented_data = datagen.flow(x_train, y_train, batch_size=32)
相关推荐
91刘仁德21 分钟前
c++类和对象(下)
c语言·jvm·c++·经验分享·笔记·算法
野犬寒鸦35 分钟前
从零起步学习并发编程 || 第一章:初步认识进程与线程
java·服务器·后端·学习
Stream_Silver37 分钟前
【Agent学习笔记3:使用Python开发简单MCP服务】
笔记·python
科技林总39 分钟前
【系统分析师】6.3 企业信息化规划
学习
Stream_Silver1 小时前
【Agent学习笔记2:深入理解Function Calling技术:从原理到实践】
笔记·python
Hacker_Z&Q1 小时前
CSS 笔记2 (属性)
前端·css·笔记
丝斯20112 小时前
AI学习笔记整理(67)——大模型的Benchmark(基准测试)
人工智能·笔记·学习
咚咚王者2 小时前
人工智能之核心技术 深度学习 第七章 扩散模型(Diffusion Models)
人工智能·深度学习
逄逄不是胖胖2 小时前
《动手学深度学习》-60translate实现
人工智能·python·深度学习
whale fall2 小时前
2026 年 1-3 月雅思口语完整话题清单(1-4 月通用最终版)
笔记·学习