def chapter_examples_bounding_boxes_iou(): import numpy as np import imgaug as ia from imgaug.augmentables.bbs import BoundingBox ia.seed(1) # Define image with two bounding boxes. image = ia.quokka(size=(256, 256)) bb1 = BoundingBox(x1=50, x2=100, y1=25, y2=75) bb2 = BoundingBox(x1=75, x2=125, y1=50, y2=100) # Compute intersection, union and IoU value # Intersection and union are both bounding boxes. They are here # decreased/increased in size purely for better visualization. bb_inters = bb1.intersection(bb2).extend(all_sides=-1) bb_union = bb1.union(bb2).extend(all_sides=2) iou = bb1.iou(bb2) # Draw bounding boxes, intersection, union and IoU value on image. image_bbs = np.copy(image) image_bbs = bb1.draw_on_image(image_bbs, size=2, color=[0, 255, 0]) image_bbs = bb2.draw_on_image(image_bbs, size=2, color=[0, 255, 0]) image_bbs = bb_inters.draw_on_image(image_bbs, size=2, color=[255, 0, 0]) image_bbs = bb_union.draw_on_image(image_bbs, size=2, color=[0, 0, 255]) image_bbs = ia.draw_text(image_bbs, text="IoU=%.2f" % (iou, ), x=bb_union.x2 + 10, y=bb_union.y1 + bb_union.height // 2, color=[255, 255, 255], size=13) # ------------ save("examples_bounding_boxes", "iou.jpg", grid([image_bbs], cols=1, rows=1), quality=90)
BoundingBox(x1=100, x2=150, y1=25, y2=75), BoundingBox(x1=175, x2=225, y1=25, y2=75) ], shape=image.shape) seq = iaa.Affine(translate_px={"x": 120}) image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) # Draw the BBs (a) in their original form, (b) after augmentation, # (c) after augmentation and removing those fully outside the image, # (d) after augmentation and removing those fully outside the image and # clipping those partially inside the image so that they are fully inside. image_before = draw_bbs(image, bbs, 100) image_after1 = draw_bbs(image_aug, bbs_aug, 100) image_after2 = draw_bbs(image_aug, bbs_aug.remove_out_of_image(), 100) image_after3 = draw_bbs(image_aug, bbs_aug.remove_out_of_image().clip_out_of_image(), 100) ia.imshow(image_after2) [x.compute_out_of_image_fraction(image) for x in bbs_aug] ia.imshow(image_after3) # compute iou bb1 = BoundingBox(x1=50, x2=100, y1=25, y2=75) bb2 = BoundingBox(x1=75, x2=125, y1=50, y2=100) # Compute intersection, union and IoU value # Intersection and union are both bounding boxes. They are here # decreased/increased in size purely for better visualization. bb_inters = bb1.intersection(bb2).extend(all_sides=-1) bb_union = bb1.union(bb2).extend(all_sides=2) iou = bb1.iou(bb2)