MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / get_affine_transform

Function get_affine_transform

PATH/core/data/transforms/post_transforms.py:191–246  ·  view source on GitHub ↗

Get the affine transform matrix, given the center/scale/rot/output_size. Args: center (np.ndarray[2, ]): Center of the bounding box (x, y). scale (np.ndarray[2, ]): Scale of the bounding box wrt [width, height]. rot (float): Rotation angle (degree). o

(center,
                         scale,
                         rot,
                         output_size,
                         shift=(0., 0.),
                         inv=False)

Source from the content-addressed store, hash-verified

189
190
191def get_affine_transform(center,
192 scale,
193 rot,
194 output_size,
195 shift=(0., 0.),
196 inv=False):
197 """Get the affine transform matrix, given the center/scale/rot/output_size.
198
199 Args:
200 center (np.ndarray[2, ]): Center of the bounding box (x, y).
201 scale (np.ndarray[2, ]): Scale of the bounding box
202 wrt [width, height].
203 rot (float): Rotation angle (degree).
204 output_size (np.ndarray[2, ] | list(2,)): Size of the
205 destination heatmaps.
206 shift (0-100%): Shift translation ratio wrt the width/height.
207 Default (0., 0.).
208 inv (bool): Option to inverse the affine transform direction.
209 (inv=False: src->dst or inv=True: dst->src)
210
211 Returns:
212 np.ndarray: The transform matrix.
213 """
214 assert len(center) == 2
215 assert len(scale) == 2
216 assert len(output_size) == 2
217 assert len(shift) == 2
218
219 # pixel_std is 200.
220 scale_tmp = scale * 200.0
221
222 shift = np.array(shift)
223 src_w = scale_tmp[0]
224 dst_w = output_size[0]
225 dst_h = output_size[1]
226
227 rot_rad = np.pi * rot / 180
228 src_dir = rotate_point([0., src_w * -0.5], rot_rad)
229 dst_dir = np.array([0., dst_w * -0.5])
230
231 src = np.zeros((3, 2), dtype=np.float32)
232 src[0, :] = center + scale_tmp * shift
233 src[1, :] = center + src_dir + scale_tmp * shift
234 src[2, :] = _get_3rd_point(src[0, :], src[1, :])
235
236 dst = np.zeros((3, 2), dtype=np.float32)
237 dst[0, :] = [dst_w * 0.5, dst_h * 0.5]
238 dst[1, :] = np.array([dst_w * 0.5, dst_h * 0.5]) + dst_dir
239 dst[2, :] = _get_3rd_point(dst[0, :], dst[1, :])
240
241 if inv:
242 trans = cv2.getAffineTransform(np.float32(dst), np.float32(src))
243 else:
244 trans = cv2.getAffineTransform(np.float32(src), np.float32(dst))
245
246 return trans
247
248

Callers 2

__call__Method · 0.90
__call__Method · 0.90

Calls 2

rotate_pointFunction · 0.85
_get_3rd_pointFunction · 0.85

Tested by

no test coverage detected