cytometer/utils.py
def labels2contours(window_labels, offset_xy=None, scaling_factor_xy=None):
    """
    Extract contours from labels.

    Each label is assumed to be a polygon. The polygon border is extracted as a list of (x, y)-points using marching
    cubes (http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.find_contours).

    The contour points assume that pixel size is (1, 1).

    :param window_labels: np.array (n, row, col). Each (i, ...) is a 2D image with one or more labels.
    :param offset_xy: (def None) np.array (n, 2). Each row contains the (x, y) coordinates of the first pixel. This
    is useful when you have a collection of cropped objects obtained with one_image_per_label_v2, and you want to
    compute the coordinates that the contours had in the original image, before the cropping.
    :return:
    * contours: List of np.array (m_i, x, y). Each np.array contains the points of a contour.
    """

    if len(window_labels) == 0:
        return []

    if offset_xy is not None:
        if window_labels.shape[0] != offset_xy.shape[0] or offset_xy.shape[1] != 2:
            raise ValueError('offset must have shape (n, 2) if window_labels has shape (n, row, col)')

    if scaling_factor_xy is not None:
        if window_labels.shape[0] != len(scaling_factor_xy):
            raise ValueError('scaling factor must be a list with n elements if window_labels has shape (n, row, col)')

    contours = []
    for i in range(window_labels.shape[0]):

        if DEBUG:
            plt.clf()
            plt.imshow(window_labels[i, ...])

        for lab in np.unique(window_labels[i, ...]):
            if lab == 0:
                continue
            # convert labels to contours (points) using marching squares
            # http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.find_contours
            # compute (row, col) coordinates for contour points from binary mask
            aux = find_contours(window_labels[i, ...] == lab, 0.5,
                                fully_connected='low', positive_orientation='low')[0]
            # convert to (x, y) coordinates
            aux = aux[:, [1, 0]]
            if DEBUG:
                plt.plot(aux[:, 0], aux[:, 1], 'w')
            # undo scaling
            if scaling_factor_xy is not None:
                aux[:, 0] /= scaling_factor_xy[i][0]
                aux[:, 1] /= scaling_factor_xy[i][1]
            # add window offset
            if offset_xy is not None:
                aux[:, 0] = aux[:, 0] + offset_xy[i, 0]
                aux[:, 1] = aux[:, 1] + offset_xy[i, 1]
            # add to the list of contours
            contours.append(aux)

    return contours

    Extract contours from labels.

    Each label is assumed to be a polygon. The polygon border is extracted as a list of (x, y)-points using marching
    cubes (http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.find_contours).

    The contour points assume that pixel size is (1, 1).

    :param window_labels: np.array (n, row, col). Each (i, ...) is a 2D image with one or more labels.
    :param offset_xy: (def None) np.array (n, 2). Each row contains the (x, y) coordinates of the first pixel. This
    is useful when you have a collection of cropped objects obtained with one_image_per_label_v2, and you want to
    compute the coordinates that the contours had in the original image, before the cropping.
    :return:
    * contours: List of np.array (m_i, x, y). Each np.array contains the points of a contour.
    
Extract contours from labels.
Each label is assumed to be a polygon. The polygon border is extracted as a list of (x, y)-points using marching
cubes .

The contour points assume that pixel size is (1, 1).
Extract contours from labels . Each label is assumed to be a polygon . The polygon border is extracted as a list of ( x y ) - points using marching cubes . The contour points assume that pixel size is ( 1 1 ) .
def labels2contours(window_labels, offset_xy=None, scaling_factor_xy=None):
    if len(window_labels) == 0:
        return []
    if offset_xy is not None:
        if window_labels.shape[0] != offset_xy.shape[0] or offset_xy.shape[1] != 2:
            raise ValueError('offset must have shape (n, 2) if window_labels has shape (n, row, col)')
    if scaling_factor_xy is not None:
        if window_labels.shape[0] != len(scaling_factor_xy):
            raise ValueError('scaling factor must be a list with n elements if window_labels has shape (n, row, col)')
    contours = []
    for i in range(window_labels.shape[0]):
        if DEBUG:
            plt.clf()
            plt.imshow(window_labels[i, ...])
        for lab in np.unique(window_labels[i, ...]):
            if lab == 0:
                continue
            aux = find_contours(window_labels[i, ...] == lab, 0.5,
                                fully_connected='low', positive_orientation='low')[0]
            aux = aux[:, [1, 0]]
            if DEBUG:
                plt.plot(aux[:, 0], aux[:, 1], 'w')
            if scaling_factor_xy is not None:
                aux[:, 0] /= scaling_factor_xy[i][0]
                aux[:, 1] /= scaling_factor_xy[i][1]
            if offset_xy is not None:
                aux[:, 0] = aux[:, 0] + offset_xy[i, 0]
                aux[:, 1] = aux[:, 1] + offset_xy[i, 1]
            contours.append(aux)
    return contours
def labels2contours ( window_labels , offset_xy = None , scaling_factor_xy = None ) : if len ( window_labels ) == 0 : return [ ] if offset_xy is not None : if window_labels . shape [ 0 ] != offset_xy . shape [ 0 ] or offset_xy . shape [ 1 ] != 2 : raise ValueError ( 'offset must have shape (n, 2) if window_labels has shape (n, row, col)' ) if scaling_factor_xy is not None : if window_labels . shape [ 0 ] != len ( scaling_factor_xy ) : raise ValueError ( 'scaling factor must be a list with n elements if window_labels has shape (n, row, col)' ) contours = [ ] for i in range ( window_labels . shape [ 0 ] ) : if DEBUG : plt . clf ( ) plt . imshow ( window_labels [ i , ... ] ) for lab in np . unique ( window_labels [ i , ... ] ) : if lab == 0 : continue aux = find_contours ( window_labels [ i , ... ] == lab , 0.5 , fully_connected = 'low' , positive_orientation = 'low' ) [ 0 ] aux = aux [ : , [ 1 , 0 ] ] if DEBUG : plt . plot ( aux [ : , 0 ] , aux [ : , 1 ] , 'w' ) if scaling_factor_xy is not None : aux [ : , 0 ] /= scaling_factor_xy [ i ] [ 0 ] aux [ : , 1 ] /= scaling_factor_xy [ i ] [ 1 ] if offset_xy is not None : aux [ : , 0 ] = aux [ : , 0 ] + offset_xy [ i , 0 ] aux [ : , 1 ] = aux [ : , 1 ] + offset_xy [ i , 1 ] contours . append ( aux ) return contours
Extract contours from labels.
Extract contours from labels .
"""
    Extract contours from labels.

    Each label is assumed to be a polygon. The polygon border is extracted as a list of (x, y)-points using marching
    cubes (http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.find_contours).

    The contour points assume that pixel size is (1, 1).

    :param window_labels: np.array (n, row, col). Each (i, ...) is a 2D image with one or more labels.
    :param offset_xy: (def None) np.array (n, 2). Each row contains the (x, y) coordinates of the first pixel. This
    is useful when you have a collection of cropped objects obtained with one_image_per_label_v2, and you want to
    compute the coordinates that the contours had in the original image, before the cropping.
    :return:
    * contours: List of np.array (m_i, x, y). Each np.array contains the points of a contour.
    """ # convert labels to contours (points) using marching squares # http://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.find_contours # compute (row, col) coordinates for contour points from binary mask # convert to (x, y) coordinates # undo scaling # add window offset # add to the list of contours
List of np.array (m_i, x, y). Each np.array contains the points of a contour.
List of np . array ( m_i x y ) . Each np . array contains the points of a contour .
window_labels
np.array (n, row, col). Each (i, ...) is a 2D image with one or more labels.
np . array ( n row col ) . Each ( i ... ) is a 2D image with one or more labels .
offset_xy
(def None) np.array (n, 2). Each row contains the (x, y) coordinates of the first pixel. This
is useful when you have a collection of cropped objects obtained with one_image_per_label_v2, and you want to
compute the coordinates that the contours had in the original image, before the cropping.
( def None ) np . array ( n 2 ) . Each row contains the ( x y ) coordinates of the first pixel . This is useful when you have a collection of cropped objects obtained with one_image_per_label_v2 and you want to compute the coordinates that the contours had in the original image before the cropping .
scaling_factor_xy