数据集预处理

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)
相关推荐
CoovallyAIHub27 分钟前
超越YOLOv8/v11!自研RKM-YOLO为输电线路巡检精度、速度双提升
深度学习·算法·计算机视觉
BagMM1 小时前
FC-CLIP 论文阅读 开放词汇的检测与分割的统一
人工智能·深度学习·计算机视觉
q***44811 小时前
java进阶--多线程学习
java·开发语言·学习
断剑zou天涯6 小时前
【算法笔记】窗口内最大值或最小值的更新结构
java·笔记·算法
Naiva8 小时前
【小技巧】Microchip 把 MPLAB X IDE工程编码改成 UTF-8
笔记
金融小师妹8 小时前
基于NLP语义解析的联储政策信号:强化学习框架下的12月降息概率回升动态建模
大数据·人工智能·深度学习·1024程序员节
山顶夕景9 小时前
【RL】Does RLVR enable LLMs to self-improve?
深度学习·llm·强化学习·rlvr
cg501710 小时前
基于 Bert 基本模型进行 Fine-tuned
人工智能·深度学习·bert
ndjnddjxn10 小时前
Rust学习
开发语言·学习·rust
菜鸟‍11 小时前
【后端学习】MySQL数据库
数据库·后端·学习·mysql