CV学习笔记6-图像样本增强
图像样本增强(Data Augmentation)是机器学习和深度学习中用于提高模型泛化能力和鲁棒性的一种技术。通过对训练数据进行各种变换,生成新的样本,这可以帮助模型更好地适应不同的输入变化,减少过拟合,提升模型的性能。
常见的图像样本增强技术
-
几何变换
-
旋转:将图像按一定角度旋转,增加模型对图像方向变化的鲁棒性。
import cv2 import numpy as npdef rotate_image(image, angle):height, width = image.shape[:2]center = (width // 2, height // 2)M = cv2.getRotationMatrix2D(center, angle, 1.0)rotated_image = cv2.warpAffine(image, M, (width, height))return rotated_image -
翻转:水平翻转、垂直翻转图像,增加模型对图像对称性的鲁棒性。
flipped_image = cv2.flip(image, 1) # 水平翻转 -
缩放:改变图像的大小,模拟不同的距离或视角。
resized_image = cv2.resize(image, (width, height)) -
平移:将图像在水平或垂直方向上移动,增加模型对物体位置变化的适应能力。
def translate_image(image, tx, ty):M = np.float32([[1, 0, tx], [0, 1, ty]])translated_image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))return translated_image
-
-
颜色变换
-
亮度调整:增加或减少图像的亮度,模拟不同的光照条件。
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) hsv[:, :, 2] = cv2.normalize(hsv[:, :, 2], None, 0, 255, cv2.NORM_MINMAX) bright_image = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) -
对比度调整:调整图像的对比度,增强模型对颜色变化的适应性。
alpha = 1.5 # 对比度控制因子 contrast_image = cv2.convertScaleAbs(image, alpha=alpha) -
色彩抖动:改变图像的颜色空间,使模型对色彩变化具有更好的鲁棒性。
def color_jitter(image, brightness=0, contrast=0, saturation=0, hue=0):image = image.astype(np.float32) / 255.0image = cv2.convertScaleAbs(image, alpha=contrast, beta=brightness)image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)image[:, :, 1] += saturationimage[:, :, 0] += hueimage = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)image = np.clip(image * 255.0, 0, 255).astype(np.uint8)return image
-
-
添加噪声
- 高斯噪声:向图像中添加高斯噪声,模拟图像采集中的噪声。
def add_gaussian_noise(image):row, col, ch = image.shapegauss = np.random.normal(0, 1, (row, col, ch))noisy_image = np.clip(image + gauss * 25, 0, 255).astype(np.uint8)return noisy_image
- 高斯噪声:向图像中添加高斯噪声,模拟图像采集中的噪声。
-
裁剪与填充
-
随机裁剪:从图像中随机裁剪出一个子区域,增强模型对不同视角的鲁棒性。
def random_crop(image, crop_size):h, w = image.shape[:2]ch, cw = crop_sizex = np.random.randint(0, w - cw + 1)y = np.random.randint(0, h - ch + 1)cropped_image = image[y:y+ch, x:x+cw]return cropped_image -
填充:对图像进行边缘填充,模拟不同的背景或边缘情况。
padded_image = cv2.copyMakeBorder(image, top=10, bottom=10, left=10, right=10, borderType=cv2.BORDER_REFLECT)
-
-
图像扭曲
- 透视变换:通过透视变换扭曲图像,模拟不同的视角。
def perspective_transform(image):rows, cols, _ = image.shapepts1 = np.float32([[50, 50], [200, 50], [50, 200], [200, 200]])pts2 = np.float32([[10, 100], [200, 50], [100, 250], [250, 250]])M = cv2.getPerspectiveTransform(pts1, pts2)warped_image = cv2.warpPerspective(image, M, (cols, rows))return warped_image
- 透视变换:通过透视变换扭曲图像,模拟不同的视角。
-
合成变换
- 图像混合:将两张图像合成,生成新的训练样本。
def blend_images(image1, image2, alpha=0.5):blended_image = cv2.addWeighted(image1, alpha, image2, 1 - alpha, 0)return blended_image
- 图像混合:将两张图像合成,生成新的训练样本。
使用库进行数据增强
-
Keras:
ImageDataGenerator提供了许多内置的数据增强功能。from tensorflow.keras.preprocessing.image import ImageDataGeneratordatagen = ImageDataGenerator(rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode='nearest' ) -
Albumentations:一个强大的图像增强库,提供了多种数据增强技术。
from albumentations import Compose, RandomCrop, HorizontalFlip, VerticalFliptransform = Compose([RandomCrop(width=256, height=256),HorizontalFlip(),VerticalFlip() ]) -
OpenCV:可用于实施许多基本的图像增强操作,如旋转、翻转、缩放等。
总结
图像样本增强是提高机器学习模型泛化能力的重要技术。通过对图像进行各种变换,可以生成更多的训练样本,增强模型对不同输入变化的鲁棒性,减少过拟合。常见的增强技术包括几何变换、颜色变换、添加噪声、裁剪与填充、图像扭曲等。结合现代深度学习框架和工具库,可以方便地实现图像增强操作,并在实际应用中提高模型的性能。
