@@ -368,16 +368,14 @@ def min(self):
368
368
"""
369
369
(property) :attr:`min` is the bottom-left corner of the bounding box.
370
370
"""
371
- return [min (self .get_points ()[:, 0 ]),
372
- min (self .get_points ()[:, 1 ])]
371
+ return np .min (self .get_points (), axis = 0 )
373
372
374
373
@property
375
374
def max (self ):
376
375
"""
377
376
(property) :attr:`max` is the top-right corner of the bounding box.
378
377
"""
379
- return [max (self .get_points ()[:, 0 ]),
380
- max (self .get_points ()[:, 1 ])]
378
+ return np .max (self .get_points (), axis = 1 )
381
379
382
380
@property
383
381
def intervalx (self ):
@@ -695,50 +693,23 @@ def union(bboxes):
695
693
"""
696
694
if not len (bboxes ):
697
695
raise ValueError ("'bboxes' cannot be empty" )
698
-
699
- if len (bboxes ) == 1 :
700
- return bboxes [0 ]
701
-
702
- x0 = np .inf
703
- y0 = np .inf
704
- x1 = - np .inf
705
- y1 = - np .inf
706
-
707
- for bbox in bboxes :
708
- points = bbox .get_points ()
709
- xs = points [:, 0 ]
710
- ys = points [:, 1 ]
711
- x0 = min (x0 , np .min (xs ))
712
- y0 = min (y0 , np .min (ys ))
713
- x1 = max (x1 , np .max (xs ))
714
- y1 = max (y1 , np .max (ys ))
715
-
716
- return Bbox .from_extents (x0 , y0 , x1 , y1 )
696
+ x0 = min (bbox .xmin for bbox in bboxes )
697
+ x1 = max (bbox .xmax for bbox in bboxes )
698
+ y0 = min (bbox .ymin for bbox in bboxes )
699
+ y1 = max (bbox .ymax for bbox in bboxes )
700
+ return Bbox ([[x0 , y0 ], [x1 , y1 ]])
717
701
718
702
@staticmethod
719
703
def intersection (bbox1 , bbox2 ):
720
704
"""
721
705
Return the intersection of the two bboxes or None
722
706
if they do not intersect.
723
-
724
- Implements the algorithm described at:
725
-
726
- http://www.tekpool.com/node/2687
727
-
728
707
"""
729
- intersects = not (bbox2 .xmin > bbox1 .xmax or
730
- bbox2 .xmax < bbox1 .xmin or
731
- bbox2 .ymin > bbox1 .ymax or
732
- bbox2 .ymax < bbox1 .ymin )
733
-
734
- if intersects :
735
- x0 = max ([bbox1 .xmin , bbox2 .xmin ])
736
- x1 = min ([bbox1 .xmax , bbox2 .xmax ])
737
- y0 = max ([bbox1 .ymin , bbox2 .ymin ])
738
- y1 = min ([bbox1 .ymax , bbox2 .ymax ])
739
- return Bbox .from_extents (x0 , y0 , x1 , y1 )
740
-
741
- return None
708
+ x0 = max (bbox1 .xmin , bbox2 .xmin )
709
+ x1 = min (bbox1 .xmax , bbox2 .xmax )
710
+ y0 = max (bbox1 .ymin , bbox2 .ymin )
711
+ y1 = min (bbox1 .ymax , bbox2 .ymax )
712
+ return Bbox ([[x0 , y0 ], [x1 , y1 ]]) if x0 <= x1 and y0 <= y1 else None
742
713
743
714
744
715
class Bbox (BboxBase ):
0 commit comments