您的位置:首页 > 其它

图像数据增强:平移、加噪、旋转、缩放(padding)

2017-11-29 22:26 344 查看
首先是平移
path = '/home/chenjia/HWDB1.1tst_gnt/img/'
#左右、上下平移
#a为平移的尺度,这里设置为10.
def moving(img, a, size, path):
img1 = img
img2 = img
img3 = img
img4 = img
img1 = np.concatenate((img1[:, a:], img1[:, :a]), axis=1)  #左
cv2.imwrite(path + 'mov_zuo.png', img1)
img2 = np.concatenate((img2[:, size[1] - a:], img2[:, :size[1] - a]), axis=1)  # 右
cv2.imwrite(path + 'mov_you.png', img2)
img3 = np.concatenate((img3[a:, :], img3[:a, :]), axis=0)   #上
cv2.imwrite(path + 'mov_shang.png', img3)
img4 = np.concatenate((img4[size[0] - a:, :], img4[:size[0] -a, :]), axis=0)   #下
cv2.imwrite(path + 'mov_xia.png', img4)
moving(img_1, 10, size1, path)


加噪,参考链接:http://blog.csdn.net/myhaspl/article/details/37693429

def noiseing(img):
param = 30
grayscale = 256
w = img.shape[1]
h = img.shape[0]
newimg = np.zeros((h, w), np.uint8)

for x in xrange(0, h):
for y in xrange(0, w, 2):
r1 = np.random.random_sample()
r2 = np.random.random_sample()
z1 = param * np.cos(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1))
z2 = param * np.sin(2 * np.pi * r2) * np.sqrt((-2) * np.log(r1))

fxy = int(img[x, y] + z1)
fxy1 = int(img[x, y + 1] + z2)
if fxy < 0:
fxy_val = 0
elif fxy > grayscale - 1:
fxy_val = grayscale - 1
else:
fxy_val = fxy
if fxy1 < 0:
fxy1_val = 0
elif fxy1 > grayscale - 1:
fxy1_val = grayscale - 1
else:
fxy1_val = fxy1
newimg[x, y] = fxy_val
newimg[x, y + 1] = fxy1_val

cv2.destroyAllWindows()
return newimg


旋转

def rotate(image, angle, center=None, scale=1.0):
(h, w) = image.shape[:2]
# 若未指定旋转中心,则将图像中心设为旋转中心
if center is None:
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
img1 = rotate(img, 5) #5 -5 10 -10等角度均可 正负值表示顺逆时针


img = cv2.imread(path + '1.png', -1)
size = img.shape
#im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)   #转换了灰度化
size1 = (size[0], size[1] + 1)    #把img129,83 补成129,84.
img_1 = 255 * np.ones(size1, np.uint8)
img_1[:, :size[1]] = img
cv2.imshow('preview', img_1)
cv2.waitKey()    #显示图像


缩放  +    以下代码是把图像缩小后,又进行边缘padding的操作。  主要采用边缘镜像padding和边缘点像素值padding两种思想:

#coding=utf-8
import cv2
import numpy as np

def ImageScale(img, scale):
size = img.shape
SIZE1 = size[0]
SIZE2 = size[1]
if scale > 1:
size1 = int(SIZE1 * scale) + 1
size2 = int(SIZE2 * scale)    #是否加1,根据具体图像尺寸的奇偶决定
#这里需要注意,对于w h 不等的图像, w h 的顺序值需要调整好.
img = cv2.resize(img, (size2, size1), interpolation = cv2.INTER_CUBIC)  #双三次线性插值法.
a1 = (size1 - SIZE1) / 2
b1 = size1 - a1
a2 = (size2 - SIZE2) / 2
b2 = size2 - a2
#print a1,b1,a2,b2
img = img[a1:b1, a2:b2]
#print img.shape
else:   #即scale<1
size1 = int(SIZE1 * scale)
size2 = int(SIZE2 * scale) + 1   # 是否加1,根据具体图像尺寸的奇偶决定
img = cv2.resize(img, (size2, size1), interpolation=cv2.INTER_CUBIC)  # 双三次线性插值法.

return img

def mirrpadding(img, s1, s2):
orgsize = img.shape
size1 = orgsize[0]
size2 = orgsize[1]
a1 = (s1 - size1) / 2   #例:(129-103)/2 = 13
b1 = size1 - a1           #103-13
a2 = (s2 - size2) / 2     #例:(84-68)/2 = 8
b2 = size2 - a2           # 68-8
#print a1,b1,a2,b2
img1 = np.rot90((np.rot90(img[:a1, :a2].T)).T, 3)
print img1.shape, '1'
img2 = np.rot90(img[:a1,:].T)
print img2.shape, '2'
img3 = np.rot90((np.rot90(img[:a1, b2:].T)).T, 3)
print img3.shape, '3'
img4 = np.rot90(img[:,:a2].T, 3)
print img4.shape, '4'
img5 = np.rot90(img[:, b2:].T, 3)
print img5.shape, '5'
img6 = np.rot90((np.rot90(img[b1:, :a2].T)).T, 3)
print img6.shape, '6'
img7 = np.rot90(img[b1:, :].T)
print img7.shape, '7'
img8 = np.rot90((np.rot90(img[b1:, b2:].T)).T, 3)
print img8.shape, '8'
img = np.concatenate((img4, img, img5), axis=1)  #concatenate拼接函数,axis=1即在第二个维度上进行拼接.
img1 = np.concatenate((img1, img2, img3), axis=1)
img6 = np.concatenate((img6, img7, img8), axis=1)
img = np.concatenate((img1, img, img6), axis=0)
print img.shape, 'img'
cv2.imwrite('/.../img/mirror.png', img)

#关于填充什么像素值,可以根据图像特点进行修改.
def padding(img, s1, s2):   #s1 s2为原图的w h值
img_1 = img
orgsize = img.shape
size1 = orgsize[0]
size2 = orgsize[1]
a1 = (s1 - size1) / 2   #例:(129-103)/2 = 13
b1 = size1 - a1           #103-13
a2 = (s2 - size2) / 2     #例:(84-68)/2 = 8
b2 = size2 - a2           # 68-8
#print a1,b1,a2,b2

img1 = np.zeros([a1, a2],np.uint)
size = img1.shape
for i in range(size[0]):
for j in range(size[1]):
img1[i, j] = img[0, 0]   #padding为最上角的像素值.
print img1.shape, '1'
img2 = img_1[:a1,:]
size = img2.shape
for i in range(size[0]):
for j in range(size[1]):
img2[i, j] = img[0, 0]   #  得视情况而定...
print img2.shape, '2'
img3 = img_1[:a1, b2:]
size = img3.shape
for i in range(size[0]):
for j in range(size[1]):
img3[i, j] = img[0, 0]  #
print img3.shape, '3'
img4 = img_1[:,:a2]
size = img4.shape
for i in range(size[0]):
for j in range(size[1]):
img4[i, j] = img[0, 0]  #
print img4.shape, '4'
img5 = img_1[:, b2:]
size = img5.shape
for i in range(size[0]):
for j in range(size[1]):
img5[i, j] = img[0, 0]  #
print img5.shape, '5'
img6 = img_1[b1:, :a2]
size = img6.shape
for i in range(size[0]):
for j in range(size[1]):
img6[i, j] = img[0, 0]  #
print img6.shape, '6'
img7 = img_1[b1:, :]
size = img7.shape
for i in range(size[0]):
for j in range(size[1]):
img7[i, j] = img[0, 0]  #
print img7.shape, '7'
img8 = img_1[b1:, b2:]
size = img8.shape
for i in range(size[0]):
for j in range(size[1]):
img8[i, j] = img[0, 0]  #
print img8.shape, '8'
img = np.concatenate((img4, img, img5), axis=1)  #concatenate拼接函数,axis=1即在第二个维度上进行拼接.
img1 = np.concatenate((img1, img2, img3), axis=1)
img6 = np.concatenate((img6, img7, img8), axis=1)
img = np.concatenate((img1, img, img6), axis=0)
cv2.imwrite('.../img/padding.png', img)

img = cv2.imread('.../img/1_1.png', -1)
s1 = img.shape[0]
s2 = img.shape[1]
img1 = ImageScale(img, 0.8)
cv2.imwrite('/home/chenjia/HWDB1.1tst_gnt/img/0.8.png', img1)
# img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
# img = cv2.resize(img, (102,102), interpolation = cv2.INTER_CUBIC)
# cv2.imwrite('/home/lenovo/2Tdisk/face/code/test/gray.jpg', img)
mirrpadding(img1, s1, s2)
padding(img1, s1, s2)


镜像padding的手稿:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: