(segm, bbox, size)
return mask
Recover mask from cropped and resized mask.

    This function requires cv2.

    Args:
        segm (~numpy.ndarray): See below.
        bbox (~numpy.ndarray): See below.
        size (tuple): This is a tuple of length 2. Its elements are
            ordered as (height, width).

    Returns:
        ~numpy.ndarray: See below.

    .. csv-table::
        :header: name, shape, dtype, format

        :obj:`segm`, ":math:`(R, S, S)`", :obj:`float32`, --
        :obj:`bbox`, ":math:`(R, 4)`", :obj:`float32`, \
        ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`"
        :obj:`mask` (output), ":math:`(R, H, W)`", :obj:`bool`, --
Recover mask from cropped and resized mask.
Recover mask from cropped and resized mask .
def segm_to_mask(segm, bbox, size):
    """Recover mask from cropped and resized mask.

    This function requires cv2.

    Args:
        segm (~numpy.ndarray): See below.
        bbox (~numpy.ndarray): See below.
        size (tuple): This is a tuple of length 2. Its elements are
            ordered as (height, width).

    Returns:
        ~numpy.ndarray: See below.

    .. csv-table::
        :header: name, shape, dtype, format

        :obj:`segm`, ":math:`(R, S, S)`", :obj:`float32`, --
        :obj:`bbox`, ":math:`(R, 4)`", :obj:`float32`, \
        ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`"
        :obj:`mask` (output), ":math:`(R, H, W)`", :obj:`bool`, --

    """
    pad = 1
    H, W = size
    _, segm_size, _ = segm.shape

    mask = np.zeros((len(bbox), H, W), dtype=np.bool)

    # As commented in mask_to_segm, cv2.resize needs adjust.
    padded_segm_size = segm_size + pad * 2
    expand_scale = padded_segm_size / segm_size
    bbox = _expand_bbox(bbox, expand_scale)
    canvas_mask = np.zeros(
        (padded_segm_size, padded_segm_size), dtype=np.float32)
    bbox = _integerize_bbox(bbox)

    for i, (bb, sgm) in enumerate(zip(bbox, segm)):
        bb_height = bb[2] - bb[0]
        bb_width = bb[3] - bb[1]
        if bb_height == 0 or bb_width == 0:
            continue

        canvas_mask[pad:-pad, pad:-pad] = sgm

        with chainer.using_config('cv_resize_backend', 'cv2'):
            crop_mask = transforms.resize(
                canvas_mask[None], (bb_height, bb_width))[0]
        crop_mask = crop_mask > 0.5

        y_min = max(bb[0], 0)
        x_min = max(bb[1], 0)
        y_max = max(min(bb[2], H), 0)
        x_max = max(min(bb[3], W), 0)
        y_offset = y_min - bb[0]
        x_offset = x_min - bb[1]
        mask[i, y_min:y_max, x_min:x_max] = crop_mask[
            y_offset:y_offset + y_max - y_min,
            x_offset:x_offset + x_max - x_min]
    return mask
def segm_to_mask ( segm , bbox , size ) : pad = 1 H , W = size _ , segm_size , _ = segm . shape mask = np . zeros ( ( len ( bbox ) , H , W ) , dtype = np . bool ) # As commented in mask_to_segm, cv2.resize needs adjust. padded_segm_size = segm_size + pad * 2 expand_scale = padded_segm_size / segm_size bbox = _expand_bbox ( bbox , expand_scale ) canvas_mask = np . zeros ( ( padded_segm_size , padded_segm_size ) , dtype = np . float32 ) bbox = _integerize_bbox ( bbox ) for i , ( bb , sgm ) in enumerate ( zip ( bbox , segm ) ) : bb_height = bb [ 2 ] - bb [ 0 ] bb_width = bb [ 3 ] - bb [ 1 ] if bb_height == 0 or bb_width == 0 : continue canvas_mask [ pad : - pad , pad : - pad ] = sgm with chainer . using_config ( 'cv_resize_backend' , 'cv2' ) : crop_mask = transforms . resize ( canvas_mask [ None ] , ( bb_height , bb_width ) ) [ 0 ] crop_mask = crop_mask > 0.5 y_min = max ( bb [ 0 ] , 0 ) x_min = max ( bb [ 1 ] , 0 ) y_max = max ( min ( bb [ 2 ] , H ) , 0 ) x_max = max ( min ( bb [ 3 ] , W ) , 0 ) y_offset = y_min - bb [ 0 ] x_offset = x_min - bb [ 1 ] mask [ i , y_min : y_max , x_min : x_max ] = crop_mask [ y_offset : y_offset + y_max - y_min , x_offset : x_offset + x_max - x_min ] return mask