Draw 2d gaussian on input image. Parameters ---------- img: torch.Tensor A tensor with shape: `(3, H, W)`. pt: list or tuple A point: (x, y). sigma: int Sigma of gaussian distribution. Returns ------- torch.Tensor A tensor with shape:
(img, pt, sigma)
| 460 | |
| 461 | |
| 462 | def drawGaussian(img, pt, sigma): |
| 463 | """Draw 2d gaussian on input image. |
| 464 | |
| 465 | Parameters |
| 466 | ---------- |
| 467 | img: torch.Tensor |
| 468 | A tensor with shape: `(3, H, W)`. |
| 469 | pt: list or tuple |
| 470 | A point: (x, y). |
| 471 | sigma: int |
| 472 | Sigma of gaussian distribution. |
| 473 | |
| 474 | Returns |
| 475 | ------- |
| 476 | torch.Tensor |
| 477 | A tensor with shape: `(3, H, W)`. |
| 478 | |
| 479 | """ |
| 480 | img = to_numpy(img) |
| 481 | tmpSize = 3 * sigma |
| 482 | # Check that any part of the gaussian is in-bounds |
| 483 | ul = [int(pt[0] - tmpSize), int(pt[1] - tmpSize)] |
| 484 | br = [int(pt[0] + tmpSize + 1), int(pt[1] + tmpSize + 1)] |
| 485 | |
| 486 | if (ul[0] >= img.shape[1] or ul[1] >= img.shape[0] or br[0] < 0 or br[1] < 0): |
| 487 | # If not, just return the image as is |
| 488 | return to_torch(img) |
| 489 | |
| 490 | # Generate gaussian |
| 491 | size = 2 * tmpSize + 1 |
| 492 | x = np.arange(0, size, 1, float) |
| 493 | y = x[:, np.newaxis] |
| 494 | x0 = y0 = size // 2 |
| 495 | # The gaussian is not normalized, we want the center value to equal 1 |
| 496 | g = np.exp(- ((x - x0) ** 2 + (y - y0) ** 2) / (2 * sigma ** 2)) |
| 497 | |
| 498 | # Usable gaussian range |
| 499 | g_x = max(0, -ul[0]), min(br[0], img.shape[1]) - ul[0] |
| 500 | g_y = max(0, -ul[1]), min(br[1], img.shape[0]) - ul[1] |
| 501 | # Image range |
| 502 | img_x = max(0, ul[0]), min(br[0], img.shape[1]) |
| 503 | img_y = max(0, ul[1]), min(br[1], img.shape[0]) |
| 504 | |
| 505 | img[img_y[0]:img_y[1], img_x[0]:img_x[1]] = g[g_y[0]:g_y[1], g_x[0]:g_x[1]] |
| 506 | return to_torch(img) |
| 507 | |
| 508 | |
| 509 | def flip(x): |