东方耀AI技术分享

标题: 036、imgaug图像增强之Bounding Boxes变换 [打印本页]

作者: 东方耀    时间: 2020-1-13 17:33
标题: 036、imgaug图像增强之Bounding Boxes变换
036、imgaug图像增强之Bounding Boxes变换


  1. import imgaug as ia
  2. import imgaug.augmenters as iaa
  3. import numpy as np
  4. import imageio

  5. print(ia.__version__)

  6. img_path = 'dfy_imgs/ai111.jpg'
  7. image = imageio.imread(img_path)
  8. print(type(image), image.shape)
  9. ia.seed(666)


  10. # 定义2个bounding box
  11. bbs = ia.BoundingBoxesOnImage([
  12.     ia.BoundingBox(x1=65, y1=100, x2=200, y2=150),
  13.     ia.BoundingBox(x1=150, y1=80, x2=200, y2=130)
  14. ], shape=image.shape)

  15. seq = iaa.Sequential([
  16.     iaa.Multiply((1.2, 1.5)),   # 改变亮度, 不影响bounding box
  17.     iaa.Affine(translate_px={"x": 40, "y": 60}, scale=0.8)   # 平移后缩放,会影响bounding box
  18. ])

  19. # 固定变换
  20. seq_det = seq.to_deterministic()

  21. # 变换图像和bounding box
  22. image_aug = seq_det.augment_images([image])[0]
  23. bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]

  24. # 打印坐标
  25. # use .x1_int, .y_int, ... to get integer coordinates
  26. for i in range(len(bbs.bounding_boxes)):
  27.     before = bbs.bounding_boxes[i]
  28.     after = bbs_aug.bounding_boxes[i]
  29.     print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (
  30.         i,
  31.         before.x1, before.y1, before.x2, before.y2,
  32.         after.x1, after.y1, after.x2, after.y2)
  33.     )

  34. # image with BBs before/after augmentation (shown below)
  35. image_before = bbs.draw_on_image(image, size=2)
  36. image_after = bbs_aug.draw_on_image(image_aug, size=2, color=[0, 0, 255])

  37. ia.imshow(np.hstack([image_before, image_after]))

复制代码



作者: xsoft    时间: 2020-2-3 15:21
谢谢老师答疑解惑




欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/) Powered by Discuz! X3.4