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

Function cv_cropBoxInverse

ADHMR/lib/utils/transforms.py:345–398  ·  view source on GitHub ↗

Paste the cropped bbox to the original image. Parameters ---------- inp: torch.Tensor A tensor with shape: `(3, height, width)`. bbox: list or tuple [xmin, ymin, xmax, ymax]. img_size: tuple Original image size, as (img_H, img_W). output_size: tuple

(inp, bbox, img_size, output_size)

Source from the content-addressed store, hash-verified

343
344
345def cv_cropBoxInverse(inp, bbox, img_size, output_size):
346 """Paste the cropped bbox to the original image.
347
348 Parameters
349 ----------
350 inp: torch.Tensor
351 A tensor with shape: `(3, height, width)`.
352 bbox: list or tuple
353 [xmin, ymin, xmax, ymax].
354 img_size: tuple
355 Original image size, as (img_H, img_W).
356 output_size: tuple
357 Cropped input size, as (height, width).
358 Returns
359 -------
360 torch.Tensor
361 A tensor with shape: `(3, img_H, img_W)`.
362
363 """
364 xmin, ymin, xmax, ymax = bbox
365 xmax -= 1
366 ymax -= 1
367 resH, resW = output_size
368 imgH, imgW = img_size
369
370 lenH = max((ymax - ymin), (xmax - xmin) * resH / resW)
371 lenW = lenH * resW / resH
372 if inp.dim() == 2:
373 inp = inp[np.newaxis, :, :]
374
375 box_shape = [ymax - ymin, xmax - xmin]
376 pad_size = [(lenH - box_shape[0]) // 2, (lenW - box_shape[1]) // 2]
377
378 src = np.zeros((3, 2), dtype=np.float32)
379 dst = np.zeros((3, 2), dtype=np.float32)
380
381 src[0, :] = 0
382 src[1, :] = np.array([resW - 1, resH - 1], np.float32)
383 dst[0, :] = np.array([xmin - pad_size[1], ymin - pad_size[0]], np.float32)
384 dst[1, :] = np.array([xmax + pad_size[1], ymax + pad_size[0]], np.float32)
385
386 src[2:, :] = get_3rd_point(src[0, :], src[1, :])
387 dst[2:, :] = get_3rd_point(dst[0, :], dst[1, :])
388
389 trans = cv2.getAffineTransform(np.float32(src), np.float32(dst))
390 dst_img = cv2.warpAffine(torch_to_im(inp), trans,
391 (imgW, imgH), flags=cv2.INTER_LINEAR)
392 if dst_img.ndim == 3 and dst_img.shape[2] == 1:
393 dst_img = dst_img[:, :, 0]
394 return dst_img
395 elif dst_img.ndim == 2:
396 return dst_img
397 else:
398 return im_to_torch(torch.Tensor(dst_img))
399
400
401def cv_rotate(img, rot, input_size):

Callers

nothing calls this directly

Calls 3

get_3rd_pointFunction · 0.85
torch_to_imFunction · 0.85
im_to_torchFunction · 0.85

Tested by

no test coverage detected