我在图像中有分割轮廓,我想连接它们,假设它们是参数化曲线。我正在考虑一些曲线拟合或曲线追踪- 但我不知道如何实现它。
我有正好 1 px 宽的分割轮廓:
import itertools
import cv2
# Get all pixel directions
directions = list(itertools.product([-1, 0, 1], repeat=2))
directions.remove((0, 0))
# Load image
img = cv2.imread("image.png", cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
cv2.imshow("Input", img)
# Find contours
_, thresh = cv2.threshold(gray, 25, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
Run Code Online (Sandbox Code Playgroud)
我知道,所有轮廓的终点在哪里:
for cnt in contours:
ends = []
# Get ends of contour
# - first pixel is not always the …Run Code Online (Sandbox Code Playgroud)