Skip to content

Filtering

A suite of common filtering utilities.

This module includes several functions for filtering depth maps.

This module contains the following functions:

  • conf_filter(depth_map, conf_map, device, min_conf) - Filters a map by confidence values above a minimum threshold.
  • geometric_filter(src_depth, src_cam, tgt_depth, tgt_cam, pix_th, depth_th) - Computes a geometric filter based off of pixel and depth reprojection error.
  • topk_filter(depth_map, conf_map, device, percent) - Filters a map by the top percentage of confidence values.
  • topk_strict_filter(depth_map, filter_prob, device, percent) - Filters a map by the top percentage of confidence values.

conf_filter(depth_map, conf_map, device='cuda:0', min_conf=0.8)

Filters a map by confidence values above a minimum threshold.

Parameters:

Name Type Description Default
depth_map torch.Tensor required
conf_map torch.Tensor required
device str 'cuda:0'
min_conf float 0.8

Returns:

Name Type Description
filtered_map torch.Tensor
mask torch.Tensor
Source code in cvt/filtering.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def conf_filter(depth_map: torch.Tensor, conf_map: torch.Tensor, device: str = 'cuda:0', min_conf: float = 0.8) -> Tuple[torch.Tensor, torch.Tensor]:
    """Filters a map by confidence values above a minimum threshold.

    Parameters:
        depth_map:
        conf_map:
        device:
        min_conf:

    Returns:
        filtered_map:
        mask:
    """
    mask = (torch.ge(conf_map, min_conf)).to(torch.float32).to(device)
    return depth_map*mask, mask

geometric_filter(src_depth, src_cam, tgt_depth, tgt_cam, pix_th=1.0, depth_th=0.01)

Computes a geometric filter based off of pixel and depth reprojection error.

Parameters:

Name Type Description Default
src_depth np.ndarray required
src_cam np.ndarray required
tgt_depth np.ndarray required
tgt_depth np.ndarray required
pix_th float 1.0
depth_th float 0.01

Returns:

Name Type Description
mask np.ndarray
depth_reprojected np.ndarray
coords_tgt np.ndarray
Source code in cvt/filtering.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def geometric_filter(src_depth: np.ndarray, src_cam: np.ndarray, tgt_depth: np.ndarray, tgt_cam: np.ndarray, pix_th: float=1.0, depth_th: float=0.01) -> Tuple[np.ndarray,np.ndarray,np.ndarray]:
    """Computes a geometric filter based off of pixel and depth reprojection error.

    Parameters:
        src_depth:
        src_cam:
        tgt_depth:
        tgt_depth:
        pix_th:
        depth_th:

    Returns:
        mask:
        depth_reprojected:
        coords_tgt:
    """
    width, height = src_depth.shape[1], src_depth.shape[0]
    x_ref, y_ref = np.meshgrid(np.arange(0, width), np.arange(0, height))
    depth_reprojected, coords_reprojected, coords_tgt = reproject(src_depth, src_cam, tgt_depth, tgt_cam)

    # measure pixel difference
    dist = np.sqrt((coords_reprojected[:,:,0] - x_ref) ** 2 + (coords_reprojected[:,:,1] - y_ref) ** 2)

    # measure depth difference
    depth_diff = np.abs(depth_reprojected - src_depth)
    relative_depth_diff = depth_diff / src_depth

    # compute mask
    mask = np.logical_and(dist < pix_th, relative_depth_diff < depth_th)

    # apply mask to depth map
    depth_reprojected[~mask] = 0

    return mask, depth_reprojected, coords_tgt

topk_filter(depth_map, conf_map, device='cuda:0', percent=0.3)

Filters a map by the top percentage of confidence values.

Parameters:

Name Type Description Default
depth_map np.ndarray required
conf_map np.ndarray required
device str 'cuda:0'
percent float 0.3

Returns:

Name Type Description
filtered_map np.ndarray
mask np.ndarray
Source code in cvt/filtering.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def topk_filter(depth_map: np.ndarray, conf_map:np.ndarray, device: str='cuda:0', percent: float=0.3) -> Tuple[np.ndarray,np.ndarray]:
    """Filters a map by the top percentage of confidence values.

    Parameters:
        depth_map:
        conf_map:
        device:
        percent:

    Returns:
        filtered_map:
        mask:
    """
    height, width = depth_map.shape

    # calculate k number of points to keep
    valid_map = torch.ne(conf_map, 0.0).to(torch.float32)
    valid_count = torch.sum(valid_map)
    k = int(percent * valid_count)

    # flatten and grab top-k indices
    filter_prob = conf_map.reshape(-1)
    (vals, indices) = torch.topk(filter_prob, k=k, dim=0)

    # get min confidence value
    min_conf = torch.min(vals)

    # filter by min conf value
    filt = (torch.ge(conf_map, min_conf)).to(torch.float32).to(device)

    return depth_map*filt, filt

topk_strict_filter(depth_map, filter_prob, device='cuda:0', percent=0.3)

Filters a map by the top percentage of confidence values.

Parameters:

Name Type Description Default
depth_map np.ndarray required
filter_prob np.ndarray required
device str 'cuda:0'
percent float 0.3

Returns:

Name Type Description
filtered_map np.ndarray
mask np.ndarray
Source code in cvt/filtering.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def topk_strict_filter(depth_map: np.ndarray, filter_prob: np.ndarray, device: str = 'cuda:0', percent: float = 0.3) -> Tuple[np.ndarray,np.ndarray]:
    """Filters a map by the top percentage of confidence values.

    Parameters:
        depth_map:
        filter_prob:
        device:
        percent:

    Returns:
        filtered_map:
        mask:
    """
    height, width = depth_map.shape

    # calculate k number of points to keep
    valid_map = torch.ne(filter_prob, 0.0).to(torch.float32)
    valid_count = torch.sum(valid_map)
    k = int(percent * valid_count)

    # flatten and grab top-k indices
    filter_prob = filter_prob.reshape(-1)
    (vals, indices) = torch.topk(filter_prob, k=k, dim=0)

    # calculate the row and column given each index
    row_indices = torch.div(indices, width, rounding_mode="floor").unsqueeze(-1)
    col_indices = torch.remainder(indices, width).unsqueeze(-1)

    # concatenate the [r,c] indices into a single tensor
    indices = torch.cat((row_indices, col_indices), dim=1)
    filt = torch.zeros((height,width), dtype=torch.uint8).to(device)

    # set top-k indices to 1
    for r,c in indices:
        filt[r,c] = 1

    return depth_map*filt, filt