数据集预处理

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)
相关推荐
墨雨听阁40 分钟前
8.1IO进程线程——文件IO函数
学习
使一颗心免于哀伤1 小时前
《设计模式之禅》笔记摘录 - 12.适配器模式
笔记·设计模式
Chef_Chen1 小时前
从0开始学习R语言--Day64--决策树回归
学习·决策树·r语言
无望__wsk2 小时前
ospf笔记
服务器·网络·笔记
Aplis2 小时前
ETCD学习之路
数据库·学习·etcd
玖剹2 小时前
Linux文件系统:从内核到缓冲区的奥秘
linux·c语言·c++·笔记·ubuntu
我不是小upper2 小时前
anaconda、conda、pip、pytorch、torch、tensorflow到底是什么?它们之间有何联系与区别?
人工智能·pytorch·深度学习·conda·tensorflow·pip
知识分享小能手2 小时前
Vue3 学习教程,从入门到精通,Vue3 中使用 Axios 进行 Ajax 请求的语法知识点与案例代码(23)
前端·javascript·vue.js·学习·ajax·vue·vue3
凤年徐2 小时前
【数据结构与算法】21.合并两个有序链表(LeetCode)
c语言·数据结构·c++·笔记·算法·链表
z樾2 小时前
Sum-rate计算
开发语言·python·深度学习