图像扩展 同比例缩小,补成正方形,补黑
/mnt/pfs/users/lbg/project/4dhuman/multi-hmr/utils/vitdet_dataset.py
def rotate_2d(pt_2d: np.array, rot_rad: float) -> np.array:
"""
Rotate a 2D point on the x-y plane.
Args:
pt_2d (np.array): Input 2D point with shape (2,).
rot_rad (float): Rotation angle
Returns:
np.array: Rotated 2D point.
"""
x = pt_2d[0]
y = pt_2d[1]
sn, cs = np.sin(rot_rad), np.cos(rot_rad)
xx = x * cs - y * sn
yy = x * sn + y * cs
return np.array([xx, yy], dtype=np.float32)
def gen_trans_from_patch_cv(c_x: float, c_y: float,
src_width: float, src_height: float,
dst_width: float, dst_height: float,
scale: float, rot: float) -> np.array:
"""
Create transformation matrix for the bounding box crop.
Args:
c_x (float): Bounding box center x coordinate in the original image.
c_y (float): Bounding box center y coordinate in the original image.
src_width (float): Bounding box width.
src_height (float): Bounding box height.
dst_width (float): Output box width.
dst_height (float): Output box height.
scale (float): Rescaling factor for the bounding box (augmentation).
rot (float): Random rotation applied to the box.
Returns:
trans (np.array): Target geometric transformation.
"""
# augment size with scale
src_w = src_width * scale
src_h = src_height * scale
src_center = np.zeros(2)
src_center[0] = c_x
src_center[1] = c_y
# augment rotation
rot_rad = np.pi * rot / 180
src_downdir = rotate_2d(np.array([0, src_h * 0.5], dtype=np.float32), rot_rad)
src_rightdir = rotate_2d(np.array([src_w * 0.5, 0], dtype=np.float32), rot_rad)
dst_w = dst_width
dst_h = dst_height
dst_center = np.array([dst_w * 0.5, dst_h * 0.5], dtype=np.float32)
dst_downdir = np.array([0, dst_h * 0.5], dtype=np.float32)
dst_rightdir = np.array([dst_w * 0.5, 0], dtype=np.float32)
src = np.zeros((3, 2), dtype=np.float32)
src[0, :] = src_center
src[1, :] = src_center + src_downdir
src[2, :] = src_center + src_rightdir
dst = np.zeros((3, 2), dtype=np.float32)
dst[0, :] = dst_center
dst[1, :] = dst_center + dst_downdir
dst[2, :] = dst_center + dst_rightdir
trans = cv2.getAffineTransform(np.float32(src), np.float32(dst))
return trans
def generate_image_patch_cv2(img: np.array, c_x: float, c_y: float,
bb_width: float, bb_height: float,
patch_width: float, patch_height: float,
do_flip: bool, scale: float, rot: float,
border_mode=cv2.BORDER_CONSTANT, border_value=0) -> Tuple[np.array, np.array]:
"""
Crop the input image and return the crop and the corresponding transformation matrix.
Args:
img (np.array): Input image of shape (H, W, 3)
c_x (float): Bounding box center x coordinate in the original image.
c_y (float): Bounding box center y coordinate in the original image.
bb_width (float): Bounding box width.
bb_height (float): Bounding box height.
patch_width (float): Output box width.
patch_height (float): Output box height.
do_flip (bool): Whether to flip image or not.
scale (float): Rescaling factor for the bounding box (augmentation).
rot (float): Random rotation applied to the box.
Returns:
img_patch (np.array): Cropped image patch of shape (patch_height, patch_height, 3)
trans (np.array): Transformation matrix.
"""
img_height, img_width, img_channels = img.shape
if do_flip:
img = img[:, ::-1, :]
c_x = img_width - c_x - 1
trans = gen_trans_from_patch_cv(c_x, c_y, bb_width, bb_height, patch_width, patch_height, scale, rot)
img_patch = cv2.warpAffine(img, trans, (int(patch_width), int(patch_height)),
flags=cv2.INTER_LINEAR,
borderMode=border_mode,
borderValue=border_value,
)
cv2.imwrite("aaa.jpg",img_patch)
# Force borderValue=cv2.BORDER_CONSTANT for alpha channel
if (img.shape[2] == 4) and (border_mode != cv2.BORDER_CONSTANT):
img_patch[:,:,3] = cv2.warpAffine(img[:,:,3], trans, (int(patch_width), int(patch_height)),
flags=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT,
)
return img_patch, trans
原文地址:https://blog.csdn.net/jacke121/article/details/145216151
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!