|
| 1 | +# @Time : 2020/4/14 11:32 |
| 2 | +# @Author : Libuda |
| 3 | +# @FileName: mian.py |
| 4 | +# @Software: PyCharm |
| 5 | +import numpy as np |
| 6 | +import imgaug.augmenters as iaa |
| 7 | +import cv2 |
| 8 | +import imageio |
| 9 | +import imgaug as ia |
| 10 | +import matplotlib.pyplot as plt |
| 11 | + |
| 12 | + |
| 13 | +def test1(): |
| 14 | + images = np.zeros((2, 128, 128, 3), dtype=np.uint8) # two example images |
| 15 | + images[:, 64, 64, :] = 255 |
| 16 | + points = [ |
| 17 | + [(10.5, 20.5)], # points on first image |
| 18 | + [(50.5, 50.5), (60.5, 60.5), (70.5, 70.5)] # points on second image |
| 19 | + ] |
| 20 | + |
| 21 | + seq = iaa.Sequential([ |
| 22 | + iaa.AdditiveGaussianNoise(scale=0.05 * 255), |
| 23 | + iaa.Affine(translate_px={"x": (1, 5)}) |
| 24 | + ]) |
| 25 | + |
| 26 | + # augment keypoints and images |
| 27 | + images_aug, points_aug = seq(images=images, keypoints=points) |
| 28 | + |
| 29 | + print("Image 1 center", np.argmax(images_aug[0, 64, 64:64 + 6, 0])) |
| 30 | + print("Image 2 center", np.argmax(images_aug[1, 64, 64:64 + 6, 0])) |
| 31 | + print("Points 1", points_aug[0]) |
| 32 | + print("Points 2", points_aug[1]) |
| 33 | + |
| 34 | + |
| 35 | +def test2(): |
| 36 | + image = imageio.imread("http://wx4.sinaimg.cn/large/006HcH9cgy1g2ywu5fooxj30oy0gimz1.jpg") # 读取图片数据 |
| 37 | + |
| 38 | + seq = iaa.Sequential([ # 定义一个sequential,把要进行的图片操作(3个操作)放在里面 |
| 39 | + iaa.Affine(rotate=(-25, 25)), |
| 40 | + iaa.AdditiveGaussianNoise(scale=(10, 30)), |
| 41 | + iaa.Crop(percent=(0, 0.4)) |
| 42 | + ], random_order=True) # 这3个图片操作以随机顺序作用在图片上 |
| 43 | + |
| 44 | + images_aug = [seq.augment_image(image) for _ in range(8)] # 应用data augmentation |
| 45 | + ia.imshow(ia.draw_grid(images_aug, cols=4, rows=2)) # 显示图片操作效果 |
| 46 | + |
| 47 | + # for index, one in enumerate(images_aug): |
| 48 | + # cv2.imwrite(str(index) + ".jpg", one) |
| 49 | + |
| 50 | + |
| 51 | +def test3(): |
| 52 | + file_list = "list" |
| 53 | + with open(file_list, 'r') as f: |
| 54 | + lines = f.readlines()[:1] |
| 55 | + for line in lines: |
| 56 | + line = line.strip().split() |
| 57 | + path = line[0] |
| 58 | + landmark = line[1:197] |
| 59 | + attribute = line[197:203] |
| 60 | + euler_angle = line[203:206] |
| 61 | + |
| 62 | + image = imageio.imread("http://wx4.sinaimg.cn/large/006HcH9cgy1g2ywu5fooxj30oy0gimz1.jpg") # 读取图片数据 |
| 63 | + # ia.imshow(image) |
| 64 | + |
| 65 | + keypoints = ia.KeypointsOnImage([ |
| 66 | + ia.Keypoint(x=65, y=100), |
| 67 | + ia.Keypoint(x=75, y=200), |
| 68 | + ia.Keypoint(x=100, y=100), |
| 69 | + ia.Keypoint(x=200, y=80) |
| 70 | + ], shape=image.shape) |
| 71 | + |
| 72 | + # landmark = np.asarray(landmark, dtype=np.float32) |
| 73 | + |
| 74 | + seq = iaa.Sequential([ # 定义一个sequential,把要进行的图片操作(3个操作)放在里面 |
| 75 | + iaa.Affine(rotate=(-20, 20)), |
| 76 | + # iaa.AdditiveGaussianNoise(scale=(10, 30)), |
| 77 | + # iaa.Crop(percent=(0, 0.4)) |
| 78 | + ], random_order=True) # 这3个图片操作以随机顺序作用在图片上 |
| 79 | + |
| 80 | + # augment keypoints and images |
| 81 | + images_aug, points_aug = seq(images=image, keypoints=keypoints) |
| 82 | + |
| 83 | + # images_aug = [seq.augment_image(image) for _ in range(8)] # 应用data augmentation |
| 84 | + |
| 85 | + # for index,one in enumerate(images_aug): |
| 86 | + # cv2.imwrite(str(index)+".jpg",one) |
| 87 | + # images_aug,points_aug = [seq(images=image, keypoints=keypoints) for _ in range(8)] # 应用data augmentation |
| 88 | + |
| 89 | + ia.imshow(images_aug) # 显示图片操作效果 |
| 90 | + |
| 91 | + |
| 92 | +def test4(): |
| 93 | + # image = ia.quokka(size=(256, 256)) |
| 94 | + file_list = "list" |
| 95 | + with open(file_list, 'r') as f: |
| 96 | + lines = f.readlines()[:1] |
| 97 | + landmarks = [] |
| 98 | + for line in lines: |
| 99 | + line = line.strip().split() |
| 100 | + path = line[0] |
| 101 | + landmark = line[1:197] |
| 102 | + attribute = line[197:203] |
| 103 | + euler_angle = line[203:206] |
| 104 | + |
| 105 | + image = imageio.imread(path) # 读取图片数据 |
| 106 | + |
| 107 | + h, w, _ = image.shape |
| 108 | + |
| 109 | + # 定义关键点 |
| 110 | + landmark = np.asarray(landmark, dtype=np.float32) |
| 111 | + |
| 112 | + lad_ls = [] |
| 113 | + |
| 114 | + # 变成绝对位置 |
| 115 | + for x, y in landmark.reshape(-1, 2) * [h, w]: |
| 116 | + lad_ls.append(ia.Keypoint(x, y)) |
| 117 | + |
| 118 | + # 相对位置 |
| 119 | + # for x,y in landmark.reshape(-1, 2): |
| 120 | + # lad_ls.append(ia.Keypoint(x,y)) |
| 121 | + |
| 122 | + keypoints = ia.KeypointsOnImage(lad_ls, shape=image.shape) |
| 123 | + |
| 124 | + # 定义一个变换序列 |
| 125 | + seq = iaa.Sequential([ |
| 126 | + iaa.Multiply((1.2, 1.5)), # 改变亮度,不影响关键点 |
| 127 | + iaa.Affine( |
| 128 | + rotate=(-20, 20), |
| 129 | + # scale=(0.7, 0.9) # 旋转10度然后缩放,会影响关键点 |
| 130 | + ), |
| 131 | + # iaa.Fliplr(0.5), |
| 132 | + iaa.GaussianBlur(sigma=(0, 3.0)), |
| 133 | + # iaa.Crop(percent=(0, 0.4)) |
| 134 | + ], random_order=True) |
| 135 | + |
| 136 | + # 固定变换序列,之后就可以先变换图像然后变换关键点,这样可以保证两次的变换完全相同。 |
| 137 | + # 如果调用次函数,需要在每次batch的时候都调用一次,否则不同的batch执行相同的变换。 |
| 138 | + seq_det = seq.to_deterministic() |
| 139 | + |
| 140 | + # 转换成list或者batch来变换。由于只有一张图片, 因此用[0]来取出该图和关键点。 |
| 141 | + image_aug = seq_det.augment_images([image])[0] |
| 142 | + keypoints_aug = seq_det.augment_keypoints([keypoints])[0] |
| 143 | + |
| 144 | + # print coordinates before/after augmentation (see below) |
| 145 | + # use after.x_int and after.y_int to get rounded integer coordinates |
| 146 | + for i in range(len(keypoints.keypoints)): |
| 147 | + before = keypoints.keypoints[i] |
| 148 | + before = keypoints.get_coords_array() / 112 |
| 149 | + after = keypoints_aug.keypoints[i] |
| 150 | + after = keypoints_aug.get_coords_array() / 112 |
| 151 | + # after_new = keypoints_aug.get_coords_array() |
| 152 | + print("Keypoint %d: (%.16f, %.16f) -> (%.16f, %.16f)" % ( |
| 153 | + i, before[i][0], before[i][1], after[i][0], after[i][1]) |
| 154 | + ) |
| 155 | + |
| 156 | + # keypoints = keypoints.get_coords_array()*[h,w] |
| 157 | + # keypoints = ia.KeypointsOnImage.from_coords_array(keypoints,image.shape) |
| 158 | + # |
| 159 | + # keypoints_aug = keypoints_aug.get_coords_array()*[h,w] |
| 160 | + # keypoints_aug = ia.KeypointsOnImage.from_coords_array(keypoints_aug,image.shape) |
| 161 | + # 将关键点画在图片上。 |
| 162 | + # image with keypoints before/after augmentation (shown below) |
| 163 | + image_before = keypoints.draw_on_image(image, size=1) |
| 164 | + image_after = keypoints_aug.draw_on_image(image_aug, size=1) |
| 165 | + |
| 166 | + fig, axes = plt.subplots(2, 1, figsize=(20, 15)) |
| 167 | + plt.subplots_adjust(left=0.2, bottom=0.2, right=0.8, top=0.8, hspace=0.3, wspace=0.0) |
| 168 | + axes[0].set_title("image before") |
| 169 | + axes[0].imshow(image_before) |
| 170 | + axes[1].set_title("image after augmentation") |
| 171 | + axes[1].imshow(image_after) |
| 172 | + |
| 173 | + plt.show() |
| 174 | + |
| 175 | + # 增强n次 后显示图片 |
| 176 | + # images_aug = [seq.augment_image(image) for _ in range(8)] # 应用data augmentation |
| 177 | + # ia.imshow(ia.draw_grid(images_aug, cols=4, rows=2)) # 显示图片操作效果 |
| 178 | + |
| 179 | + |
| 180 | +if __name__ == '__main__': |
| 181 | + import time |
| 182 | + |
| 183 | + for i in range(10): |
| 184 | + test4() |
| 185 | + time.sleep(5) |
0 commit comments