简体   繁体   中英

Join 2 non-intersecting non-convex Polygons

  • these are the 2 polygons to be joined:
    在此处输入图像描述

  • this is the result needed:
    在此处输入图像描述

  • how I might approach the problem:
    Find the projection of polygon B on polygon A, create a convex-hull of polygon B and the projection, and join all using shapely.ops.unary_union() , I don't know how to project a polygon onto another polygon.

Edit: found a solution as follows, If you have comments or a better answer please write them down:
-- find nearest point on polygon A shapely.ops.nearest_points
-- buffer that point with half the width of polygon B.
-- find the intersection of the buffered polygon and the exterior of polygon A
-- find the convex-hull of that intersection and polygon B -- join the convex-hull with polygon A

import cv2
from shapely.geometry import Polygon,Point,GeometryCollection
from shapely.ops import nearest_points,unary_union

cnts ,_ = cv2.findContours(im, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
polyA,polyB= [Polygon(c.reshape(-1,2)) for c in cnts]

#find width of polyB
_,(width,_),_ = cv2.minAreaRect(cnts[1])
#find nearest points
pt1,pt2= nearest_points(polyA,polyB)

在此处输入图像描述

# find `projection`
projection = pt1.buffer(int(width)).intersection(polyA.exterior).buffer(3) # buffer with 2 or 3 is necessary

在此处输入图像描述

# find convex_hull
ch = GeometryCollection([polyB.buffer(1),projection.buffer(1)]).convex_hull

在此处输入图像描述

# join
poly3= unary_union([polyA,ch])

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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