def randomShiftScaleRotate(img, shift_limit=0.0625, scale_limit=0.1, rotate_limit=45, u=0.5):
if random.random() < u:
height, width, channel = img.shape
angle = random.uniform(-rotate_limit, rotate_limit) # degree
scale = random.uniform(1 - scale_limit, 1 + scale_limit)
dx = round(random.uniform(-shift_limit, shift_limit)) * width
dy = round(random.uniform(-shift_limit, shift_limit)) * height
cc = math.cos(angle / 180 * math.pi) * (scale)
ss = math.sin(angle / 180 * math.pi) * (scale)
rotate_matrix = np.array([[cc, -ss], [ss, cc]])
box0 = np.array([[0, 0], [width, 0], [width, height], [0, height], ])
box1 = box0 - np.array([width / 2, height / 2])
box1 = np.dot(box1, rotate_matrix.T) + np.array([width / 2 + dx, height / 2 + dy])
box0 = box0.astype(np.float32)
box1 = box1.astype(np.float32)
mat = cv2.getPerspectiveTransform(box0, box1)
img = cv2.warpPerspective(img, mat, (width, height), flags=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_REFLECT_101) # cv2.BORDER_CONSTANT, borderValue = (0, 0, 0)) #cv2.BORDER_REFLECT_101
return img
n06_pytorch_utils.py 文件源码
python
阅读 27
收藏 0
点赞 0
评论 0
评论列表
文章目录