|
036、imgaug图像增强之Bounding Boxes变换
- import imgaug as ia
- import imgaug.augmenters as iaa
- import numpy as np
- import imageio
- print(ia.__version__)
- img_path = 'dfy_imgs/ai111.jpg'
- image = imageio.imread(img_path)
- print(type(image), image.shape)
- ia.seed(666)
- # 定义2个bounding box
- bbs = ia.BoundingBoxesOnImage([
- ia.BoundingBox(x1=65, y1=100, x2=200, y2=150),
- ia.BoundingBox(x1=150, y1=80, x2=200, y2=130)
- ], shape=image.shape)
- seq = iaa.Sequential([
- iaa.Multiply((1.2, 1.5)), # 改变亮度, 不影响bounding box
- iaa.Affine(translate_px={"x": 40, "y": 60}, scale=0.8) # 平移后缩放,会影响bounding box
- ])
- # 固定变换
- seq_det = seq.to_deterministic()
- # 变换图像和bounding box
- image_aug = seq_det.augment_images([image])[0]
- bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
- # 打印坐标
- # use .x1_int, .y_int, ... to get integer coordinates
- for i in range(len(bbs.bounding_boxes)):
- before = bbs.bounding_boxes[i]
- after = bbs_aug.bounding_boxes[i]
- print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (
- i,
- before.x1, before.y1, before.x2, before.y2,
- after.x1, after.y1, after.x2, after.y2)
- )
- # image with BBs before/after augmentation (shown below)
- image_before = bbs.draw_on_image(image, size=2)
- image_after = bbs_aug.draw_on_image(image_aug, size=2, color=[0, 0, 255])
- ia.imshow(np.hstack([image_before, image_after]))
复制代码
|
|