【问题标题】:cv2.fillConvexPoly and cv2.polylines yield different edges?cv2.fillConvexPoly 和 cv2.polylines 产生不同的边缘?
【发布时间】:2021-05-04 21:17:47
【问题描述】:

使用 cv2.fillConvexPoly 绘制相邻的填充多边形后,使用 cv2.polylines 绘制多边形之间的边

预期行为:边缘非常适合两个绘制的多边形之间

实际行为:有些边实际上位于一个多边形内! (见下图)

重现代码

# params
h,w = 20,30
points_yx = [(5, 1), (7, 22), (16, 13)]
points_xy = [(x,y) for y,x in points_yx]

# create voronoi partitiion
rect = (0, 0, w,h)
subdiv = cv2.Subdiv2D(rect)
subdiv.insert(points_xy)
polygons, centers = subdiv.getVoronoiFacetList([])
polygons_int = [np.round(poly).astype(np.int) for poly in polygons]

# draw voronoi polygons on image
img = np.zeros((h, w))
for i, poly in enumerate(polygons_int):
    color = i + 1
    cv2.fillConvexPoly(img, poly, color=color, lineType=cv2.LINE_8)
    x,y = points_xy[i]
    img[y, x] = 0

# draw borders of polygons
cv2.polylines(img, polygons_int, isClosed=False, color=0, thickness=0,
              lineType=cv2.LINE_8)

# plot
fig, ax = plt.subplots(1, 1)
ax.imshow(img, interpolation=None)
plt.show()

【问题讨论】:

    标签: opencv opencv-python


    【解决方案1】:

    好的,至少一种解决方案是使用cv2.drawContours 来绘制填充多边形以及多边形边缘:

    # params
    h,w = 20,30
    points_yx = [(5, 1), (7, 22), (16, 13)]
    points_xy = [(x,y) for y,x in points_yx]
    
    # create voronoi partitiion
    rect = (0, 0, w,h)
    subdiv = cv2.Subdiv2D(rect)
    subdiv.insert(points_xy)
    polygons, centers = subdiv.getVoronoiFacetList([])
    polygons_int = [np.round(poly).astype(np.int) for poly in polygons]
    
    # draw voronoi on image
    img = np.zeros((h, w))
    for i, poly in enumerate(polygons_int):
        cv2.drawContours(img, [poly], contourIdx=-1, color=i+1, thickness=cv2.FILLED)
        x,y = points_xy[i]
        img[y, x] = 0
    
    # draw borders of polygons
    cv2.drawContours(img, polygons_int, contourIdx=-1, color=0, thickness=0)
    
    # plot
    fig, ax = plt.subplots(1, 1)
    ax.imshow(img, interpolation=None)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多