def sized_mask(
        arr: Union[np.ndarray, coo_matrix], shape: Tuple[int, int] = None,
        full: bool = False):
    """return a 2D dense array representation of the mask, optionally
    cropped and padded

    Parameters
    ----------
    arr: numpy.ndarray or scipy.sparse.coo_matrix:
        a representation of the mask
    shape: tuple(int, int)
        [h, w] for padded shape. If None, cropped to existing values
    full: bool
        if True, the full-frame array is returned

    Returns
    -------
    mask: numpy.ndarray
        2D dense matrix representation of the mask

    """
    if isinstance(arr, coo_matrix):
        mask = arr.toarray()
    else:
        mask = arr
    if not full:
        mask = crop_2d_array(mask)
        if shape is not None:
            mask = center_pad_2d(mask, shape)
    return mask
Exemplo n.º 2
0
def postage_stamp(data: coo_matrix,
                  canvas_size: Optional[Tuple[int, int]] = None):
    """
    Generate a "postage stamp" image data for a matrix in coo_format.

    Parameters
    ==========
    coo_matrix: scipy.sparse.coo_matrix representation of an image to
        represent as a png
    canvas_size: tuple of canvas size in pixels, where each row and
        column is one pixel (rows, columns). If None, will fit tight to
        outer boundaries of the data. If larger than the image data,
        will place the image in the center of the canvas (as closely as
        possible given dimensions).

    Returns
    =======
    2-dimensional array representing PNG data normalized for grayscale
    (values 0-255).
    """
    # Crop image down to minimum-sized rectangle containing all nonzero data
    arr = crop_2d_array(data)
    img_size = arr.shape

    # Normalize to grayscale based on data values
    gray = (arr / arr.max() * 255).astype(np.uint8)

    # Don't need to do any transforms if fitting tightly to image data
    if canvas_size is None or canvas_size == img_size:
        return gray
    else:
        return center_pad_2d(gray, canvas_size, 0)
Exemplo n.º 3
0
def test_compare_crop_pad_and_extents(arr, shape):
    """check that cropping and padding an image
    is the same as getting the extents and
    indexing the array (i.e. as would be applied to video frame)
    """
    cropped_padded = au.center_pad_2d(au.crop_2d_array(arr), shape)
    extents, pad_width = au.content_extents(arr, shape)
    indexed = np.pad(
            arr[extents[0]:extents[1], extents[2]:extents[3]],
            pad_width)
    assert np.all(cropped_padded == indexed)
Exemplo n.º 4
0
def test_crop_array_raises_error(arr):
    with pytest.raises(ValueError):
        au.crop_2d_array(arr)
Exemplo n.º 5
0
def test_crop_array(arr, expected):
    np.testing.assert_array_equal(expected, au.crop_2d_array(arr))