Data Augmentation
Posted leebxo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Data Augmentation相关的知识,希望对你有一定的参考价值。
## 基本图像处理
### 存储图片
import numpy as np
import cv2
import matplotlib.pyplot as plt
import os
# 6.2.1图像的表示
img = np.array(
[
[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 255, 0], [255, 0, 255], [0, 255, 255]],
[[255, 255, 255], [128, 128, 128], [0, 0, 0]]
], dtype = np.uint8)
save = ‘/home/lee/Documents/opencv‘
plt.imsave(os.path.join(save, ‘img_pyplot.jpg‘), img)
cv2.imwrite(os.path.join(save, ‘img_cv2.jpg‘), img)
# 6.2.2基本图像处理
color_img = cv2.imread(‘Images/dog.jpg‘)
print(color_img.shape)
gray_img = cv2.imread(‘dog.jpg‘, cv2.IMREAD_GRAYSCALE)
print(gray_img.shape)
cv2.imwrite(‘test_grayscale.jpg‘, gray_img)
reload_grayscale = cv2.imread(‘test_grayscale.jpg‘)
print(reload_grayscale.shape)
cv2.imwrite(‘test_imwrite.jpg‘, color_img, (cv2.IMWRITE_JPEG_QUALITY, 10))
cv2.imwrite(‘test_imwrite.png‘, color_img, (cv2.IMWRITE_PNG_COMPRESSION, 5))
img = cv2.imread(‘dog.jpg‘)
img_200_200 = cv2.resize(img, (200, 200))
print(img_200_200.shape)
cv2.imwrite(‘test_resize_200_200.jpg‘, img_200_200)
img_fx_fy = cv2.resize(img, (0, 0), fx=0.5, fy=0.8,
interpolation=cv2.INTER_NEAREST)
print(img_fx_fy.shape)
cv2.imwrite(‘test_resize_fx_fy.jpg‘, img_fx_fy)
img_white_border = cv2.copyMakeBorder(img, 50, 50, 0, 0, cv2.BORDER_CONSTANT,
value=(0, 0, 0))
print(img_white_border.shape)
cv2.imwrite(‘test_resize_border.jpg‘, img_white_border)
############################################################
img = cv2.imread(‘leaf.jpg‘)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# H空间中,绿色比黄色的值高一点,所以给每个像素+15,黄色的树叶会变绿
turn_green_hsv = img_hsv.copy()
turn_green_hsv[:, :, 0] = (turn_green_hsv[:, :, 0]+40) % 180
turn_green_img = cv2.cvtColor(turn_green_hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(‘turn_green.jpg‘, turn_green_img)
# 减小饱和度会让图像损失鲜艳,变得更灰
colorless_hsv = img_hsv.copy()
colorless_hsv[:, :, 1] = 0.5*colorless_hsv[:, :, 1]
colorless_img = cv2.cvtColor(colorless_hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(‘colorless.jpg‘, colorless_img)
# 减小亮度
darker_hsv = img_hsv.copy()
darker_hsv[:, :, 2] = 0.5*darker_hsv[:, :, 2]
darder_img = cv2.cvtColor(darker_hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(‘darker.jpg‘, darder_img)
###########################################################
img = cv2.imread(‘deer.jpg‘)
# 原图1024*695经过仿射变换后横纵坐标放大1.6倍,图片扩大为1638*1112
M_crop_deer = np.array([
[1.6, 0., 0],
[0., 1.6, 0]
], dtype=np.float32)
img_deer = cv2.warpAffine(img, M_crop_deer, (1638, 1112))
cv2.imwrite(‘deer_affine.jpg‘, img_deer)
# x轴的剪切变换,角度为15°
theta = 15 * np.pi / 180
M_shear = np.array([
[1, np.tan(theta), 0],
[0, 1, 0]], dtype=np.float32)
img_sheared = cv2.warpAffine(img, M_shear, (1024, 695))
cv2.imwrite(‘deer_affine_sheard.jpg‘, img_sheared)
# 顺时针旋转,角度为15°
M_rotate = np.array([
[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0]
], dtype=np.float32)
img_rotated = cv2.warpAffine(img, M_rotate, (1024, 695))
cv2.imwrite(‘deer_affine_rotated.jpg‘, img_rotated)
#某种变换,具体旋转+缩放+旋转组合可以通过SVD分解理解
M = np.array([
[1, 1.5, -400],
[0.5, 2, -100]
], dtype=np.float32)
img_transformed = cv2.warpAffine(img, M, (1024, 695))
cv2.imwrite(‘deer_affine_transformed.jpg‘, img_transformed)
以上是关于Data Augmentation的主要内容,如果未能解决你的问题,请参考以下文章