Ejemplo n.º 1
0
def distance_transform(bitmap):
    """
    @type  bitmap: numpy array
    @param bitmap: image for which distance transform will be calculated
    
    @return: numpy array holding distance transform of provided bitmap
    
    Calculates distance transtofm of provided bitmap. This function is wrapper
    for actual distance transform.
    """
    return dtransform(bitmap)
Ejemplo n.º 2
0
def distance_transform(bitmap):
    """
    @type  bitmap: np array
    @param bitmap: image for which distance transform will be calculated

    @return: np array holding distance transform of provided bitmap

    Calculates distance transtofm of provided bitmap. This function is wrapper
    for actual distance transform.
    """
    return dtransform(bitmap)
Ejemplo n.º 3
0
def getBestLabelLocation(ImageForTracing):
    """
    @type  ImageForTracing: PIL image
    @param ImageForTracing: Image to calculate distance transform

    @return: tuple of two integers (x,y): coordinates of best-placed label

    Calculates coordinates corresponding to visual-center of given area defined
    by black pixels. "visual-center" means: 'looks like is is in the center of
    the structure". Coordinates of central points are calculated basing on
    maximum of distance transform: Central point coordinate = location of
    distance transform's maximum.

    Input bitmap: object: black, background: white
    Used to dtransform: obj. white, bg - black
    """
    ImageForTracing = ImageOps.invert(ImageForTracing)
    Imbbox = ImageForTracing.getbbox()

    # We need to provide frame with background color ensure proper results
    Imbbox = tuple(np.array(Imbbox) + np.array([-1, -1, 1, 1]))

    # Crop input image so distance transform is not calculated only in essential
    # part of the image. Plain backgroud area is skipped.
    CroppedImTracing = ImageOps.invert(
        ImageForTracing.crop(Imbbox).convert("L"))
    CroppedImTracing = ImageOps.invert(CroppedImTracing)

    # Cacluate distance transform and extract its maximum.
    distanceTransform = dtransform(tonpArray(CroppedImTracing))
    (y,x) = ( distanceTransform.argmax() // distanceTransform.shape[1] ,\
            ( distanceTransform.argmax() % distanceTransform.shape[1] ))

    # Take into account that initial image was cropped -> return to original
    # image coordinates
    x, y = x + Imbbox[0], y + Imbbox[1]

    return (x, y)
Ejemplo n.º 4
0
def getBestLabelLocation(ImageForTracing):
    """
    @type  ImageForTracing: PIL image
    @param ImageForTracing: Image to calculate distance transform

    @return: tuple of two integers (x,y): coordinates of best-placed label

    Calculates coordinates corresponding to visual-center of given area defined
    by black pixels. "visual-center" means: 'looks like is is in the center of
    the structure". Coordinates of central points are calculated basing on
    maximum of distance transform: Central point coordinate = location of
    distance transform's maximum.

    Input bitmap: object: black, background: white
    Used to dtransform: obj. white, bg - black
    """
    ImageForTracing = ImageOps.invert(ImageForTracing)
    Imbbox = ImageForTracing.getbbox()
    
    # We need to provide frame with background color ensure proper results
    Imbbox = tuple(numpy.array(Imbbox) + numpy.array([-1, -1, 1, 1]))

    # Crop input image so distance transform is not calculated only in essential
    # part of the image. Plain backgroud area is skipped.
    CroppedImTracing = ImageOps.invert(ImageForTracing.crop(Imbbox).convert("L") )
    CroppedImTracing = ImageOps.invert(CroppedImTracing)

    # Cacluate distance transform and extract its maximum.
    distanceTransform = dtransform(toNumpyArray(CroppedImTracing))
    (y,x) = ( distanceTransform.argmax() // distanceTransform.shape[1] ,\
            ( distanceTransform.argmax() % distanceTransform.shape[1] ))

    # Take into account that initial image was cropped -> return to original
    # image coordinates
    x,y = x+Imbbox[0],y+Imbbox[1]

    return (x,y)