数据集预处理

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)
相关推荐
红衣小蛇妖6 分钟前
神经网络-Day45
人工智能·深度学习·神经网络
JoannaJuanCV22 分钟前
BEV和OCC学习-5:数据预处理流程
深度学习·目标检测·3d·occ·bev
moxiaoran57532 小时前
uni-app学习笔记三十--request网络请求传参
笔记·学习·uni-app
嘉陵妹妹2 小时前
深度优先算法学习
学习·算法·深度优先
乖乖是干饭王3 小时前
Linux系统编程中的_GNU_SOURCE宏
linux·运维·c语言·学习·gnu
Best_Me073 小时前
深度学习模块缝合
人工智能·深度学习
待什么青丝4 小时前
【TMS570LC4357】之相关驱动开发学习记录2
c语言·arm开发·驱动开发·单片机·学习
行云流水剑4 小时前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
明月醉窗台4 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
虾球xz5 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习