1.目标

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

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

2.1图像尺度变换

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

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库):

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库):

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库):

水平翻转:

import cv2

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

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

垂直翻转:

import cv2

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

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

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

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库):

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库):

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库):

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):
    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)
    
03-23 00:23