#### Is there a way to use cv2.approxPolyDP to approximate open curve?

OpenCV 3.4中的cv2.approxPolyDP在闭合曲线的情况下取得了很好的效果。

``````import cv2

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("gray", gray)
cv2.waitKey(0)

_, binary = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

# cv2.imshow("binary", binary)
# cv2.waitKey(0)

_, contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:
epsilon = 0.009 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, closed=True)
cv2.drawContours(img, [approx], -1, (0, 255, 255), 1)

cv2.imshow("approx", img)
cv2.waitKey(0)

cv2.destroyAllWindows()
``````

3 个回复

closed – 如果为真，则近似曲线是闭合的（它的第一个和最后一个顶点是连接的）。 否则，它不会关闭。

``````approx = cv2.approxPolyDP(contour, epsilon, closed=False)
``````

``````import numpy as np
import cv2

hh, ww = img.shape[:2]

# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold
thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)[1]

# get points
points = np.column_stack(np.where(thresh.transpose() != 0))

# list points
for pt in points:
ptx = pt[0]
pty = pt[1]
print(ptx,pty)

# approximate polygon
poly = cv2.approxPolyDP(points, 0.02 * ww, False)

# list polygon points
for p in poly:
px = p[0]
py = p[0]
print(px,py)

# draw polygon on copy of input
result = img.copy()
cv2.polylines(result, [poly], False, (0,0,255), 1)

# save results
cv2.imwrite('curve_polygon.png', result)

cv2.imshow("thresh", thresh)
cv2.imshow("result", result)
cv2.waitKey(0)
``````

1 cv2.approxPolydp() 返回什么？

4 OpenCV - 可视化使用 cv2.approxPolyDP() 提取的多边形曲线

2017-01-26 17:23:21 2 52090   contour
5 OpenCV, cv2.approxPolyDP() 在闭合轮廓上画双线

6 cv2.approxPolyDP检测点的规则是什么？

7 有没有办法使用 CSS3/Canvas 来弯曲/弧形文本

2010-05-15 16:29:58 10 68330   html/ css
8 有没有办法使用度数变化来绘制曲线？

10 有没有办法向曲线渐变添加边缘？

2020-01-08 11:40:25 1 53   html/ css