This commit is contained in:
Glenn Jocher 2019-04-24 19:56:04 +02:00
parent fbf0014cd6
commit 387bdb010d
1 changed files with 5 additions and 3 deletions

View File

@ -277,7 +277,8 @@ def random_affine(img, targets=(), degrees=(-10, 10), translate=(.1, .1), scale=
if targets is None:
targets = []
border = 0 # width of added border (optional)
height = max(img.shape[0], img.shape[1]) + border * 2
height = img.shape[0] + border * 2
width = img.shape[1] + border * 2
# Rotation and Scale
R = np.eye(3)
@ -297,7 +298,7 @@ def random_affine(img, targets=(), degrees=(-10, 10), translate=(.1, .1), scale=
S[1, 0] = math.tan((random.random() * (shear[1] - shear[0]) + shear[0]) * math.pi / 180) # y shear (deg)
M = S @ T @ R # Combined rotation matrix. ORDER IS IMPORTANT HERE!!
imw = cv2.warpPerspective(img, M, dsize=(height, height), flags=cv2.INTER_LINEAR,
imw = cv2.warpPerspective(img, M, dsize=(width, height), flags=cv2.INTER_LINEAR,
borderValue=borderValue) # BGR order borderValue
# Return warped points also
@ -326,7 +327,8 @@ def random_affine(img, targets=(), degrees=(-10, 10), translate=(.1, .1), scale=
xy = np.concatenate((x - w / 2, y - h / 2, x + w / 2, y + h / 2)).reshape(4, n).T
# reject warped points outside of image
np.clip(xy, 0, height, out=xy)
xy[:, [0, 2]] = xy[:, [0, 2]].clip(0, width)
xy[:, [1, 3]] = xy[:, [1, 3]].clip(0, height)
w = xy[:, 2] - xy[:, 0]
h = xy[:, 3] - xy[:, 1]
area = w * h