东方耀AI技术分享
标题:
035、imgaug图像增强之keypoint变换
[打印本页]
作者:
东方耀
时间:
2020-1-13 17:16
标题:
035、imgaug图像增强之keypoint变换
035、imgaug图像增强之keypoint变换
imgaug支持在图像变换的同时变换图像中的关键点
# -*- coding: utf-8 -*-
__author__ = u'东方耀 微信:dfy_88888'
__date__ = '2020/1/13 16:21'
__product__ = 'PyCharm'
__filename__ = '02_keypoint变换'
import imgaug as ia
import imgaug.augmenters as iaa
import numpy as np
import imageio
print(ia.__version__)
# imgaug支持在图像变换的同时变换图像中的关键点
img_path = 'dfy_imgs/ai111.jpg'
img = imageio.imread(img_path)
print(type(img), img.shape)
ia.seed(666)
# 定义4个关键点
keypoints = ia.KeypointsOnImage([
ia.Keypoint(x=65, y=100),
ia.Keypoint(x=75, y=200),
ia.Keypoint(x=100, y=100),
ia.Keypoint(x=200, y=80)
], shape=img.shape)
print(type(keypoints), keypoints.shape)
# 定义一个变换序列
seq = iaa.Sequential([
iaa.Multiply(mul=(1.2, 1.5)), # 改变亮度,不影响关键点
iaa.Affine(rotate=20, scale=0.8) # 旋转10度然后缩放,会影响关键点
])
# 固定变换序列,之后就可以先变换图像然后变换关键点,这样可以保证两次的变换完全相同。
# 如果调用次函数,需要在每次batch的时候都调用一次,否则不同的batch执行相同的变换。
# deterministic 确定性的
seq_det = seq.to_deterministic()
# 转换成list或者batch来变换。由于只有一张图片, 因此用[0]来取出该图和关键点。
image_aug = seq_det.augment_images([img])[0]
keypoints_aug = seq_det.augment_keypoints([keypoints])[0]
# print coordinates before/after augmentation (打印图像增强前后的关键点坐标)
# use after.x_int and after.y_int to get rounded integer coordinates
for i in range(len(keypoints.keypoints)):
before = keypoints.keypoints[i]
after = keypoints_aug.keypoints[i]
print("Keypoint %d: (%.8f, %.8f) -> (%.8f, %.8f)" % (
i, before.x, before.y, after.x, after.y)
)
# 将关键点画在图片上。 size是关键点的大小
# image with keypoints before/after augmentation (shown below)
image_before = keypoints.draw_on_image(img, size=15)
image_after = keypoints_aug.draw_on_image(image_aug, size=7)
ia.imshow(np.hstack([image_before, image_after]))
复制代码
作者:
xsoft
时间:
2020-2-3 15:21
谢谢老师答疑解惑
欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/)
Powered by Discuz! X3.4