utils.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:kaggle-dstl 作者: lopuhin 项目源码 文件源码
def mask_to_polygons(mask: np.ndarray, epsilon=5., min_area=10.,
                     fix=False) -> MultiPolygon:
    if fix:
        epsilon *= 4
    image, contours, hierarchy = cv2.findContours(
        ((mask == 1) * 255).astype(np.uint8),
        cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS)
    approx_contours = [cv2.approxPolyDP(cnt, epsilon, True) for cnt in contours]
    if not contours:
        return MultiPolygon()

    cnt_children = defaultdict(list)
    child_contours = set()
    assert hierarchy.shape[0] == 1
    # http://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html
    for idx, (_, _, _, parent_idx) in enumerate(hierarchy[0]):
        if parent_idx != -1:
            child_contours.add(idx)
            cnt_children[parent_idx].append(approx_contours[idx])

    all_polygons = []
    for idx, cnt in enumerate(approx_contours):
        if idx not in child_contours and cv2.contourArea(cnt) >= min_area:
            assert cnt.shape[1] == 1
            poly = Polygon(
                shell=cnt[:, 0, :],
                holes=[c[:, 0, :] for c in cnt_children.get(idx, [])
                       if cv2.contourArea(c) >= min_area])
            all_polygons.append(poly)

    all_polygons = to_multipolygon(MultiPolygon(all_polygons).buffer(0))
    # return all_polygons - this was used to generate the final merges
    if fix:
        all_polygons = all_polygons.buffer(-1e-7)
        all_polygons = all_polygons.buffer(-1e-7)
    # FIXME - a great idea, but should be done after conversion to final coordinates
    all_polygons = shapely.wkt.loads(
        shapely.wkt.dumps(all_polygons, rounding_precision=8))
    while not all_polygons.is_valid:
        all_polygons = to_multipolygon(all_polygons.buffer(0))
        all_polygons = shapely.wkt.loads(
            shapely.wkt.dumps(all_polygons, rounding_precision=8))
    return all_polygons
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号