File size: 861 Bytes
8ea2eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6928e82
8ea2eff
 
 
 
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
# MaxPool-based downsampling for coarse labels
"""Downsample binary masks preserving thin positives.

We use area-based resize on float32 masks followed by a >0 threshold.
This emulates block-wise max pooling: any positive in the source region
produces a positive in the target pixel.
"""

import numpy as np


def downsample_label_maxpool(mask: np.ndarray, out_h: int, out_w: int) -> np.ndarray:
    """
    Args:
        mask: HxW binary (0/1) numpy array
        out_h, out_w: target size
    Returns:
        H'xW' binary array via max-pooling-like downsample
    """
    assert mask.ndim == 2
    # Convert to float32 so area resize yields fractional averages > 0 if any positive present
    import cv2

    m = mask.astype(np.float32)
    r = cv2.resize(m, (out_w, out_h), interpolation=cv2.INTER_AREA)
    out = (r > 0.0).astype(np.uint8)
    return out