@@ -152,6 +152,32 @@ def __init__(
152152 self ._angle = angle
153153 self ._borderValue = borderValue
154154
155+ @staticmethod
156+ def rotate_image (image : np .ndarray , angle : typing .Union [float , int ], borderValue : tuple = (0 ,0 ,0 )) -> np .ndarray :
157+ # grab the dimensions of the image and then determine the centre
158+ height , width = image .shape [:2 ]
159+ center_x , center_y = (width // 2 , height // 2 )
160+
161+ # grab the rotation matrix (applying the negative of the
162+ # angle to rotate clockwise), then grab the sine and cosine
163+ # (i.e., the rotation components of the matrix)
164+ M = cv2 .getRotationMatrix2D ((center_x , center_y ), angle , 1.0 )
165+ cos = np .abs (M [0 , 0 ])
166+ sin = np .abs (M [0 , 1 ])
167+
168+ # compute the new bounding dimensions of the image
169+ nW = int ((height * sin ) + (width * cos ))
170+ nH = int ((height * cos ) + (width * sin ))
171+
172+ # adjust the rotation matrix to take into account translation
173+ M [0 , 2 ] += (nW / 2 ) - center_x
174+ M [1 , 2 ] += (nH / 2 ) - center_y
175+
176+ # perform the actual rotation and return the image
177+ img = cv2 .warpAffine (image , M , (nW , nH ), borderValue = borderValue )
178+
179+ return img
180+
155181 @randomness_decorator
156182 def __call__ (self , image : Image , annotation : typing .Any ) -> typing .Tuple [Image , typing .Any ]:
157183 """ Randomly rotate image
@@ -174,30 +200,12 @@ def __call__(self, image: Image, annotation: typing.Any) -> typing.Tuple[Image,
174200 borderValue = np .random .randint (0 , 255 , 3 ) if self ._borderValue is None else self ._borderValue
175201 borderValue = [int (v ) for v in borderValue ]
176202
177- # grab the dimensions of the image and then determine the centre
178- center_x , center_y = image .center
179-
180- # grab the rotation matrix (applying the negative of the
181- # angle to rotate clockwise), then grab the sine and cosine
182- # (i.e., the rotation components of the matrix)
183- M = cv2 .getRotationMatrix2D ((center_x , center_y ), angle , 1.0 )
184- cos = np .abs (M [0 , 0 ])
185- sin = np .abs (M [0 , 1 ])
186-
187- # compute the new bounding dimensions of the image
188- nW = int ((image .height * sin ) + (image .width * cos ))
189- nH = int ((image .height * cos ) + (image .width * sin ))
190-
191- # adjust the rotation matrix to take into account translation
192- M [0 , 2 ] += (nW / 2 ) - center_x
193- M [1 , 2 ] += (nH / 2 ) - center_y
194-
195- # perform the actual rotation and return the image
196- img = cv2 .warpAffine (image .numpy (), M , (nW , nH ), borderValue = borderValue )
203+ img = self .rotate_image (image .numpy (), angle , borderValue )
197204
198205 if self ._augment_annotation and isinstance (annotation , Image ):
199- annotation_warp = cv2 .warpAffine (annotation .numpy (), M , (nW , nH ), borderValue = (0 , 0 , 0 ))
200- annotation .update (annotation_warp )
206+ # perform the actual rotation and return the annotation image
207+ annotation_image = self .rotate_image (annotation .numpy (), angle , borderValue = (0 , 0 , 0 ))
208+ annotation .update (annotation_image )
201209
202210 image .update (img )
203211
0 commit comments