How to draw polygons with Python?

前端 未结 6 999
南笙
南笙 2021-02-05 08:38

I have input values of x, y coordinates in the following format:

[[1,1], [2,1], [2,2], [1,2], [0.5,1.5]]

I want to draw polygons, but I don\'t

6条回答
  •  难免孤独
    2021-02-05 09:06

    If you want to draw polygons on a matrix representing an image, scikit-image has 3 functions for you:

    • skimage.draw.polygon2mask(image_shape, polygon) that directly returns a bool-type numpy.array where True means the point is inside the polygon.
    • skimage.draw.polygon(r, c[, shape]) that returns the row and column indices of the matrix points that are inside the polygon. This function is called internally by polygon2mask().
    • skimage.draw.polygon_perimeter(r, c[, shape[, clip]]) that returns the row and column indices of the points that best represent the perimeter of the polygon. This can include points that polygon() would have let aside.

    Correct me if your benchmark said the contrary, but I think these functions are quite fast.

    Example

    import numpy as np
    from skimage.draw import polygon2mask, polygon, polygon_perimeter
    
    shape = (10, 10)  # image shape
    points = [(5, -1), (-1, 5), (5, 11), (10, 5)]  # polygon points
    
    imgp2 = polygon2mask(shape, points).astype(str)  # astype() converts bools to strings
    imgp2[imgp2 == "True"] = "O"
    imgp2[imgp2 == "False"] = "."
    
    imgp = np.full(shape, ".")  # fill a n*d matrix with '.'
    imgpp = imgp.copy()
    points = np.transpose(points)  # change format to ([5, -1, 5, 10], [-1, 5, 11, 5])
    
    rr, cc = polygon(*points, shape=shape)
    imgp[rr, cc] = "O"
    
    rr, cc = polygon_perimeter(*points, shape=shape, clip=True)
    imgpp[rr, cc] = "O"
    
    print(imgp2, imgp, imgpp, sep="\n\n")
    

    Result:

    [['.' '.' '.' '.' 'O' 'O' '.' '.' '.' '.']
     ['.' '.' '.' 'O' 'O' 'O' 'O' '.' '.' '.']
     ['.' '.' 'O' 'O' 'O' 'O' 'O' 'O' '.' '.']
     ['.' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' '.']
     ['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']
     ['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']
     ['.' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']
     ['.' '.' 'O' 'O' 'O' 'O' 'O' 'O' 'O' '.']
     ['.' '.' '.' 'O' 'O' 'O' 'O' 'O' '.' '.']
     ['.' '.' '.' '.' 'O' 'O' 'O' '.' '.' '.']]
    
    [['.' '.' '.' '.' 'O' 'O' '.' '.' '.' '.']
     ['.' '.' '.' 'O' 'O' 'O' 'O' '.' '.' '.']
     ['.' '.' 'O' 'O' 'O' 'O' 'O' 'O' '.' '.']
     ['.' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' '.']
     ['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']
     ['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']
     ['.' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']
     ['.' '.' 'O' 'O' 'O' 'O' 'O' 'O' 'O' '.']
     ['.' '.' '.' 'O' 'O' 'O' 'O' 'O' '.' '.']
     ['.' '.' '.' '.' 'O' 'O' 'O' '.' '.' '.']]
    
    [['.' '.' '.' '.' 'O' 'O' 'O' '.' '.' '.']
     ['.' '.' '.' 'O' '.' '.' '.' 'O' '.' '.']
     ['.' '.' 'O' '.' '.' '.' '.' '.' 'O' '.']
     ['.' 'O' '.' '.' '.' '.' '.' '.' '.' 'O']
     ['O' '.' '.' '.' '.' '.' '.' '.' '.' 'O']
     ['O' '.' '.' '.' '.' '.' '.' '.' '.' 'O']
     ['O' '.' '.' '.' '.' '.' '.' '.' '.' 'O']
     ['.' 'O' 'O' '.' '.' '.' '.' '.' '.' 'O']
     ['.' '.' '.' 'O' '.' '.' '.' 'O' 'O' '.']
     ['.' '.' '.' '.' 'O' 'O' 'O' '.' '.' '.']]
    

提交回复
热议问题