Giter Site home page Giter Site logo

Comments (2)

mattijn avatar mattijn commented on May 29, 2024

Thanks for raising this issue. My apologies for not replier sooner. I'm not sure why I havent seen this issue before.

Your problem is interesting and I have no idea if topojson can be of value here. I tried a few things, but I didn't get as close as your result, so probably you got already further than I did.

Anyway what I tried is using the prequantize and presimplify options, while setting topology to False, since you are merely aiming for a combination of simplification and quantization, I think.

This is what I tried

import cv2
import numpy as np
from collections import defaultdict
from shapely.geometry import MultiPolygon, Polygon
from matplotlib import pyplot as plt
import topojson as tp

# function from here: https://michhar.github.io/masks_to_polygons_and_back/
def mask_to_polygons(mask, min_area=10.):
    """Convert a mask ndarray (binarized image) to Multipolygons"""
    # first, find contours with cv2: it's much faster than shapely
    contours, hierarchy = cv2.findContours(mask,
                                  cv2.RETR_CCOMP,
                                  cv2.CHAIN_APPROX_NONE)
    if not contours:
        return MultiPolygon()
    # now messy stuff to associate parent and child contours
    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(contours[idx])
    # create actual polygons filtering by area (removes artifacts)
    all_polygons = []
    for idx, cnt in enumerate(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 = MultiPolygon(all_polygons)

    return all_polygons
img = cv2.imread(r'/Users/mattijnvanhoek/Downloads/110661906-f8751b80-81ff-11eb-87e3-91b1d545c015.png', cv2.IMREAD_UNCHANGED)
plt.imshow(img)

Unknown-4

edges = cv2.Canny(img, 1, 1)
plt.imshow(edges, cmap='gray', interpolation='bicubic')

Unknown-5

poly = mask_to_polygons(edges, min_area=1)
poly # seems to be reversed, but not important now

Image 18-04-2021 at 23 09

# low prequantize values only work with the topojson version 1.1
# !python -m pip install git+https://github.com/mattijn/topojson.git --upgrade
tp.Topology(poly, prequantize=False, presimplify=200, topology=False).to_alt()

Image 18-04-2021 at 23 03

tp.Topology(poly, prequantize=50, presimplify=False, topology=False).to_alt()

Image 18-04-2021 at 23 04

from topojson.

callzhang avatar callzhang commented on May 29, 2024

Hi Matt, sorry for my late reply. I have tried different options and couldn't "merge" the neighbouring points (from difference polygons) together. So I just loop through all the points and manually merged the points together.
My use case is convert a bitmap mask (from semantic segmentation result from a deep learning model) to vectorized results. Maybe it's a good feature for you to add. Let me know if here is anything I can contribute.

from topojson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.