简体   繁体   English

如何为具有 x,y 坐标的多边形绘制圆边?

[英]How to draw a rounded edges for a polygon having its x,y coordinates?

I need to draw polygon from X, Y coordinates but with rounded corners I have the points of X, Y我需要从 X,Y 坐标绘制多边形,但圆角我有 X,Y 的点

My code is below, however if there is another library, I can use it.我的代码在下面,但是如果有另一个库,我可以使用它。

Here my output image:这是我的输出图像:

测试图片

and this is the code这是代码

def create_mask(dirct,filename,alistofpoint,height,width):
    myimg = np.zeros((height,width), dtype = "uint8")
    po = np.array(alistofpoint, np.int32)
    myimg_mod=cv2.fillPoly(myimg, [po],(255,255))
    cv2.imwrite(dirct+"//"+filename, myimg_mod)

You can do it by using the Pillow 's ImageDraw module by first using the coordinates to draw a polygon and then outlining it with a wide line with rounded corners.您可以通过使用PillowImageDraw模块来完成它,首先使用坐标绘制多边形,然后用带圆角的宽线勾勒出它的轮廓。

from operator import itemgetter
from pathlib import Path
from PIL import Image, ImageDraw


def create_mask(directory, filename, alistofpoint, height, width):
    mask = Image.new('L', (width, height), color=0)
    draw = ImageDraw.Draw(mask)
    draw.polygon(points, fill=255, outline=255)
    draw.line(points, fill=255, width=5, joint='curve')
    mask.save(Path(directory)/filename)
    return mask


pad = 25
points = [(114, 197), (96, 77), (110, 42), (138, 56,), (166, 124), (166, 174), (154, 195)]
width = max(points, key=itemgetter(0))[0] + pad
height = max(points, key=itemgetter(1))[1] + pad

mask = create_mask('\.', 'output_image.png', points, height, width)
mask.show()

Here's a 3X enlargement showing the polygon without the outline on the left, and with it drawn on the right.这是一个 3 倍放大图,左侧没有轮廓,右侧绘制了多边形。 The effect is a little hard to see at such relatively low image resolutions.在如此相对较低的图像分辨率下,效果有点难以看到。

截图显示绘制轮廓效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM