cytometer/utils.py
def bounding_box_with_margin(label, inc=0.0, coordinates='xy'):
    """
    Create a square bounding box around a segmentation mask, with optional enlargement/reduction.
    The output is given as (x0, y0, xend, yend) for plotting or (r0, c0, rend, cend) for indexing arrays.

    Note that because we need integers for indexing, the bounding box may not be completely centered on the segmentation
    mask.

    Note also that the bounding box may have negative indices, or indices beyond the image size.

    :param label: 2D numpy.array with segmentation mask.
    :param inc: (def 0.0) Scalar. The size of the box will be increased (or decreased with negative value) 100*inc%.
    E.g. if inc=0.20, the size of the box will be increased by 20% with respect to the smallest box that encloses the
    segmentation.
    :param coordinates: (def 'xy') 'xy' or 'rc'.
    :return: Coordinates of the bottom left and top right corners of the box.
    If coordinates=='xy': (x0, y0, xend, yend): These are the true coordinates of the box corners, and these values
    can be directly used for plotting.
    If coordinates=='rc': (r0, c0, rend, cend): These are rounded row/column indices that can be used for indexing
    an array, e.g. x[r0:rend, c0:cend]. Note that
        cend = xend+1
        rend = yend+1
    (the +1 is necessary because python drops the last index when slicing an array).
    """

    # compute region properties
    props = regionprops((label != 0).astype(np.uint8), coordinates='rc')
    assert (len(props) == 1)

    # box enclosing segmentation
    bbox = props[0]['bbox']

    # ease nomenclature of box corners
    (bbox_r0, bbox_c0, bbox_rend, bbox_cend) = bbox

    # make the box square
    bbox_r_len = bbox_rend - bbox_r0
    bbox_c_len = bbox_cend - bbox_c0
    bbox_len = np.max((bbox_r_len, bbox_c_len))

    if inc != 0.0:
        # increase or decrease the box size by the scalar
        bbox_len = np.round(bbox_len * (1 + inc))

    # bottom left corner of the box
    bbox_r0 -= np.round((bbox_len - bbox_r_len) / 2.0)
    bbox_c0 -= np.round((bbox_len - bbox_c_len) / 2.0)

    # top right corner of the box (+1 so that we can use it for indexing)
    bbox_rend = bbox_r0 + bbox_len
    bbox_cend = bbox_c0 + bbox_len

    if coordinates == 'xy':
        return np.float64(bbox_c0), np.float64(bbox_r0), np.float64(bbox_cend - 1), np.float64(bbox_rend - 1)
    elif coordinates == 'rc':
        return np.int64(bbox_r0), np.int64(bbox_c0), np.int64(bbox_rend), np.int64(bbox_cend)
    else:
        raise ValueError('Unknown "coordinates" value.')

    Create a square bounding box around a segmentation mask, with optional enlargement/reduction.
    The output is given as (x0, y0, xend, yend) for plotting or (r0, c0, rend, cend) for indexing arrays.

    Note that because we need integers for indexing, the bounding box may not be completely centered on the segmentation
    mask.

    Note also that the bounding box may have negative indices, or indices beyond the image size.

    :param label: 2D numpy.array with segmentation mask.
    :param inc: (def 0.0) Scalar. The size of the box will be increased (or decreased with negative value) 100*inc%.
    E.g. if inc=0.20, the size of the box will be increased by 20% with respect to the smallest box that encloses the
    segmentation.
    :param coordinates: (def 'xy') 'xy' or 'rc'.
    :return: Coordinates of the bottom left and top right corners of the box.
    If coordinates=='xy': (x0, y0, xend, yend): These are the true coordinates of the box corners, and these values
    can be directly used for plotting.
    If coordinates=='rc': (r0, c0, rend, cend): These are rounded row/column indices that can be used for indexing
    an array, e.g. x[r0:rend, c0:cend]. Note that
        cend = xend+1
        rend = yend+1
    (the +1 is necessary because python drops the last index when slicing an array).
    
Create a square bounding box around a segmentation mask, with optional enlargement/reduction.
The output is given as (x0, y0, xend, yend) for plotting or (r0, c0, rend, cend) for indexing arrays.

Note that because we need integers for indexing, the bounding box may not be completely centered on the segmentation
mask.

Note also that the bounding box may have negative indices, or indices beyond the image size.
Create a square bounding box around a segmentation mask with optional enlargement / reduction . The output is given as ( x0 y0 xend yend ) for plotting or ( r0 c0 rend cend ) for indexing arrays . Note that because we need integers for indexing the bounding box may not be completely centered on the segmentation mask . Note also that the bounding box may have negative indices or indices beyond the image size .
def bounding_box_with_margin(label, inc=0.0, coordinates='xy'):
    props = regionprops((label != 0).astype(np.uint8), coordinates='rc')
    assert (len(props) == 1)
    bbox = props[0]['bbox']
    (bbox_r0, bbox_c0, bbox_rend, bbox_cend) = bbox
    bbox_r_len = bbox_rend - bbox_r0
    bbox_c_len = bbox_cend - bbox_c0
    bbox_len = np.max((bbox_r_len, bbox_c_len))
    if inc != 0.0:
        bbox_len = np.round(bbox_len * (1 + inc))
    bbox_r0 -= np.round((bbox_len - bbox_r_len) / 2.0)
    bbox_c0 -= np.round((bbox_len - bbox_c_len) / 2.0)
    bbox_rend = bbox_r0 + bbox_len
    bbox_cend = bbox_c0 + bbox_len
    if coordinates == 'xy':
        return np.float64(bbox_c0), np.float64(bbox_r0), np.float64(bbox_cend - 1), np.float64(bbox_rend - 1)
    elif coordinates == 'rc':
        return np.int64(bbox_r0), np.int64(bbox_c0), np.int64(bbox_rend), np.int64(bbox_cend)
    else:
        raise ValueError('Unknown "coordinates" value.')
def bounding_box_with_margin ( label , inc = 0.0 , coordinates = 'xy' ) : props = regionprops ( ( label != 0 ) . astype ( np . uint8 ) , coordinates = 'rc' ) assert ( len ( props ) == 1 ) bbox = props [ 0 ] [ 'bbox' ] ( bbox_r0 , bbox_c0 , bbox_rend , bbox_cend ) = bbox bbox_r_len = bbox_rend - bbox_r0 bbox_c_len = bbox_cend - bbox_c0 bbox_len = np . max ( ( bbox_r_len , bbox_c_len ) ) if inc != 0.0 : bbox_len = np . round ( bbox_len * ( 1 + inc ) ) bbox_r0 -= np . round ( ( bbox_len - bbox_r_len ) / 2.0 ) bbox_c0 -= np . round ( ( bbox_len - bbox_c_len ) / 2.0 ) bbox_rend = bbox_r0 + bbox_len bbox_cend = bbox_c0 + bbox_len if coordinates == 'xy' : return np . float64 ( bbox_c0 ) , np . float64 ( bbox_r0 ) , np . float64 ( bbox_cend - 1 ) , np . float64 ( bbox_rend - 1 ) elif coordinates == 'rc' : return np . int64 ( bbox_r0 ) , np . int64 ( bbox_c0 ) , np . int64 ( bbox_rend ) , np . int64 ( bbox_cend ) else : raise ValueError ( 'Unknown "coordinates" value.' )
Create a square bounding box around a segmentation mask, with optional enlargement/reduction.
Create a square bounding box around a segmentation mask with optional enlargement / reduction .
"""
    Create a square bounding box around a segmentation mask, with optional enlargement/reduction.
    The output is given as (x0, y0, xend, yend) for plotting or (r0, c0, rend, cend) for indexing arrays.

    Note that because we need integers for indexing, the bounding box may not be completely centered on the segmentation
    mask.

    Note also that the bounding box may have negative indices, or indices beyond the image size.

    :param label: 2D numpy.array with segmentation mask.
    :param inc: (def 0.0) Scalar. The size of the box will be increased (or decreased with negative value) 100*inc%.
    E.g. if inc=0.20, the size of the box will be increased by 20% with respect to the smallest box that encloses the
    segmentation.
    :param coordinates: (def 'xy') 'xy' or 'rc'.
    :return: Coordinates of the bottom left and top right corners of the box.
    If coordinates=='xy': (x0, y0, xend, yend): These are the true coordinates of the box corners, and these values
    can be directly used for plotting.
    If coordinates=='rc': (r0, c0, rend, cend): These are rounded row/column indices that can be used for indexing
    an array, e.g. x[r0:rend, c0:cend]. Note that
        cend = xend+1
        rend = yend+1
    (the +1 is necessary because python drops the last index when slicing an array).
    """ # compute region properties # box enclosing segmentation # ease nomenclature of box corners # make the box square # increase or decrease the box size by the scalar # bottom left corner of the box # top right corner of the box (+1 so that we can use it for indexing)
Coordinates of the bottom left and top right corners of the box.
If coordinates=='xy': (x0, y0, xend, yend): These are the true coordinates of the box corners, and these values
can be directly used for plotting.
If coordinates=='rc': (r0, c0, rend, cend): These are rounded row/column indices that can be used for indexing
an array, e.g.
Coordinates of the bottom left and top right corners of the box . If coordinates == ' xy ' : ( x0 y0 xend yend ) : These are the true coordinates of the box corners and these values can be directly used for plotting . If coordinates == ' rc ' : ( r0 c0 rend cend ) : These are rounded row / column indices that can be used for indexing an array e . g .
label
2D numpy.array with segmentation mask.
2D numpy . array with segmentation mask .
inc
None
coordinates
None