Spaces:
Running
on
L40S
Running
on
L40S
File size: 2,642 Bytes
d7e58f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
import torch
def test_contour_expand():
from mmcv.ops import contour_expand
np_internal_kernel_label = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 2, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 2, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 2, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 2, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0,
0]]).astype(np.int32)
np_kernel_mask1 = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0,
0]]).astype(np.uint8)
np_kernel_mask2 = (np_internal_kernel_label > 0).astype(np.uint8)
np_kernel_mask = np.stack([np_kernel_mask1, np_kernel_mask2])
min_area = 1
kernel_region_num = 3
result = contour_expand(np_kernel_mask, np_internal_kernel_label, min_area,
kernel_region_num)
gt = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 2, 2, 2, 0],
[0, 0, 1, 1, 1, 1, 2, 2, 2, 0], [0, 0, 1, 1, 1, 1, 2, 2, 2, 0],
[0, 0, 1, 1, 1, 1, 2, 2, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
assert np.allclose(result, gt)
np_kernel_mask_t = torch.from_numpy(np_kernel_mask)
np_internal_kernel_label_t = torch.from_numpy(np_internal_kernel_label)
result = contour_expand(np_kernel_mask_t, np_internal_kernel_label_t,
min_area, kernel_region_num)
assert np.allclose(result, gt)
|