MCPcopy Create free account
hub / github.com/MotrixLab/ADHMR / fix_cropBox

Function fix_cropBox

ADHMR/lib/utils/transforms.py:248–285  ·  view source on GitHub ↗

Crop bbox from image by Affinetransform. Parameters ---------- img: torch.Tensor A tensor with shape: `(3, H, W)`. bbox: list or tuple [xmin, ymin, xmax, ymax]. input_size: tuple Resulting image size, as (height, width). Returns ------- torch

(img, bbox, input_size)

Source from the content-addressed store, hash-verified

246
247
248def fix_cropBox(img, bbox, input_size):
249 """Crop bbox from image by Affinetransform.
250
251 Parameters
252 ----------
253 img: torch.Tensor
254 A tensor with shape: `(3, H, W)`.
255 bbox: list or tuple
256 [xmin, ymin, xmax, ymax].
257 input_size: tuple
258 Resulting image size, as (height, width).
259
260 Returns
261 -------
262 torch.Tensor
263 A tensor with shape: `(3, height, width)`.
264
265 """
266 xmin, ymin, xmax, ymax = bbox
267 input_ratio = input_size[0] / input_size[1]
268 bbox_ratio = (ymax - ymin) / (xmax - xmin)
269 if bbox_ratio > input_ratio:
270 # expand width
271 cx = (xmax + xmin) / 2
272 h = ymax - ymin
273 w = h / input_ratio
274 xmin = cx - w / 2
275 xmax = cx + w / 2
276 elif bbox_ratio < input_ratio:
277 # expand height
278 cy = (ymax + ymin) / 2
279 w = xmax - xmin
280 h = w * input_ratio
281 ymin = cy - h / 2
282 ymax = cy + h / 2
283 bbox = [int(x) for x in [xmin, ymin, xmax, ymax]]
284
285 return cv_cropBox(img, bbox, input_size), bbox
286
287
288def fix_cropBox_rot(img, bbox, input_size, rot):

Callers

nothing calls this directly

Calls 1

cv_cropBoxFunction · 0.85

Tested by

no test coverage detected