11from collections import namedtuple
2+
23import cv2 as cv
34import numpy as np
45
56from .blender import Blender
67from .stitching_error import StitchingError
78
89
9- class Rectangle (namedtuple (' Rectangle' , ' x y width height' )):
10+ class Rectangle (namedtuple (" Rectangle" , " x y width height" )):
1011 __slots__ = ()
1112
1213 @property
@@ -30,13 +31,13 @@ def y2(self):
3031 return self .y + self .height
3132
3233 def times (self , x ):
33- return Rectangle (* (int (round (i * x )) for i in self ))
34+ return Rectangle (* (int (round (i * x )) for i in self ))
3435
3536 def draw_on (self , img , color = (0 , 0 , 255 ), size = 1 ):
3637 if len (img .shape ) == 2 :
3738 img = cv .cvtColor (img , cv .COLOR_GRAY2RGB )
3839 start_point = (self .x , self .y )
39- end_point = (self .x2 - 1 , self .y2 - 1 )
40+ end_point = (self .x2 - 1 , self .y2 - 1 )
4041 cv .rectangle (img , start_point , end_point , color , size )
4142 return img
4243
@@ -56,10 +57,10 @@ def prepare(self, imgs, masks, corners, sizes):
5657 lir = self .estimate_largest_interior_rectangle (mask )
5758 corners = self .get_zero_center_corners (corners )
5859 rectangles = self .get_rectangles (corners , sizes )
59- self .overlapping_rectangles = self .get_overlaps (
60- rectangles , lir )
60+ self .overlapping_rectangles = self .get_overlaps (rectangles , lir )
6161 self .intersection_rectangles = self .get_intersections (
62- rectangles , self .overlapping_rectangles )
62+ rectangles , self .overlapping_rectangles
63+ )
6364
6465 def crop_images (self , imgs , aspect = 1 ):
6566 for idx , img in enumerate (imgs ):
@@ -75,8 +76,7 @@ def crop_img(self, img, idx, aspect=1):
7576
7677 def crop_rois (self , corners , sizes , aspect = 1 ):
7778 if self .do_crop :
78- scaled_overlaps = \
79- [r .times (aspect ) for r in self .overlapping_rectangles ]
79+ scaled_overlaps = [r .times (aspect ) for r in self .overlapping_rectangles ]
8080 cropped_corners = [r .corner for r in scaled_overlaps ]
8181 cropped_corners = self .get_zero_center_corners (cropped_corners )
8282 cropped_sizes = [r .size for r in scaled_overlaps ]
@@ -93,8 +93,7 @@ def estimate_largest_interior_rectangle(self, mask):
9393 # is explicitely desired (needs some time to compile at the first run!)
9494 import largestinteriorrectangle
9595
96- contours , hierarchy = \
97- cv .findContours (mask , cv .RETR_TREE , cv .CHAIN_APPROX_NONE )
96+ contours , hierarchy = cv .findContours (mask , cv .RETR_TREE , cv .CHAIN_APPROX_NONE )
9897 if not hierarchy .shape == (1 , 1 , 4 ) or not np .all (hierarchy == - 1 ):
9998 raise StitchingError ("Invalid Contour. Try without cropping." )
10099 contour = contours [0 ][:, 0 , :]
@@ -129,12 +128,14 @@ def get_overlap(rectangle1, rectangle2):
129128 y2 = min (rectangle1 .y2 , rectangle2 .y2 )
130129 if x2 < x1 or y2 < y1 :
131130 raise StitchingError ("Rectangles do not overlap!" )
132- return Rectangle (x1 , y1 , x2 - x1 , y2 - y1 )
131+ return Rectangle (x1 , y1 , x2 - x1 , y2 - y1 )
133132
134133 @staticmethod
135134 def get_intersections (rectangles , overlapping_rectangles ):
136- return [Cropper .get_intersection (r , overlap_r ) for r , overlap_r
137- in zip (rectangles , overlapping_rectangles )]
135+ return [
136+ Cropper .get_intersection (r , overlap_r )
137+ for r , overlap_r in zip (rectangles , overlapping_rectangles )
138+ ]
138139
139140 @staticmethod
140141 def get_intersection (rectangle , overlapping_rectangle ):
@@ -146,4 +147,4 @@ def get_intersection(rectangle, overlapping_rectangle):
146147
147148 @staticmethod
148149 def crop_rectangle (img , rectangle ):
149- return img [rectangle .y : rectangle .y2 , rectangle .x : rectangle .x2 ]
150+ return img [rectangle .y : rectangle .y2 , rectangle .x : rectangle .x2 ]
0 commit comments