def mask_polygons_together_with_border()

in geospatial/preprocessing/create_label_masks.py [0:0]


def mask_polygons_together_with_border(size, polys, border):
    """

    Args:
        size: A tuple of (width, height, channels)
        polys: A dict of feature uid: (numpy array of coords, numerical category of the building), from
            get_feature_info()
        border: Pixel width to shrink each shape by to create some space between adjacent shapes

    Returns:
        a dict of masked polygons with the shapes filled in from cv2.fillPoly
    """

    # For each WKT polygon, read the WKT format and fill the polygon as an image
    mask_img = np.zeros(size, np.uint8)  # 0 is the background class

    for uid, tup in polys.items():
        # poly is a np.ndarray
        poly, damage_class_num = tup

        # blank = np.zeros(size, np.uint8)

        # Creating a shapely polygon object out of the numpy array
        polygon = Polygon(poly)

        # Getting the center points from the polygon and the polygon points
        (poly_center_x, poly_center_y) = polygon.centroid.coords[0]
        polygon_points = polygon.exterior.coords

        # Setting a new polygon with each X,Y manipulated based off the center point
        shrunk_polygon = []
        for (x, y) in polygon_points:
            if x < poly_center_x:
                x += border
            elif x > poly_center_x:
                x -= border

            if y < poly_center_y:
                y += border
            elif y > poly_center_y:
                y -= border

            shrunk_polygon.append([x, y])

        # Transforming the polygon back to a np.ndarray
        ns_poly = np.array(shrunk_polygon, np.int32)

        # Filling the shrunken polygon to add a border between close polygons
        # Assuming there is no overlap!
        fillPoly(mask_img, [ns_poly], (damage_class_num, damage_class_num, damage_class_num))

    mask_img = mask_img[:, :, 0].squeeze()
    # print(f'shape of final mask_img: {mask_img.shape}')
    return mask_img