有没有办法使用 cv2.approxPolyDP 来近似开曲线?
Is there a way to use cv2.approxPolyDP to approximate open curve?
我想用线段链来近似平滑线。
cv2.approxPolyDP在OpenCV 3.4中在闭合曲线的情况下取得了不错的效果
原点闭合曲线:
近似闭合曲线:
但是在开曲线的情况下,cv2.approxPolyDP没有达到预期的效果
原点开放曲线:
近似开曲线:
我想要的结果应该是一串线段而不是闭合的多边形,像这样(此图是Photoshop制作的,不是Python程序制作的):
有没有办法使用cv2.approxPolyDP来近似开曲线?
我的Python程序如下:
import cv2
img = cv2.imread('1.jpg')
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()
我程序中使用的原图如下。
Close curve photo
Open curve photo
根据 docs for approxPolyDP()
,您可以简单地使用 closed=False
:
closed – If true, the approximated curve is closed (its first and last vertices are connected). Otherwise, it is not closed.
所以你应该能够做到:
approx = cv2.approxPolyDP(contour, epsilon, closed=False)
最后还是没有找到可以直接在OpenCV中使用的方法。但是我找到了一个算法(命名为Ramer-Douglas-Peucker算法)只需要一点代码就可以逼近曲线。
见https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
见https://www.sciencedirect.com/science/article/abs/pii/0167865594900027
这是在 Python/OpenCV 中使用 cv2.approxPolyDP
执行此操作的方法
输入(截屏标题栏)
import numpy as np
import cv2
# read input
img = cv2.imread('curve.png')
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)
我想用线段链来近似平滑线。
cv2.approxPolyDP在OpenCV 3.4中在闭合曲线的情况下取得了不错的效果
原点闭合曲线:
但是在开曲线的情况下,cv2.approxPolyDP没有达到预期的效果
原点开放曲线:
我想要的结果应该是一串线段而不是闭合的多边形,像这样(此图是Photoshop制作的,不是Python程序制作的):
有没有办法使用cv2.approxPolyDP来近似开曲线?
我的Python程序如下:
import cv2
img = cv2.imread('1.jpg')
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()
我程序中使用的原图如下。
Close curve photo Open curve photo
根据 docs for approxPolyDP()
,您可以简单地使用 closed=False
:
closed – If true, the approximated curve is closed (its first and last vertices are connected). Otherwise, it is not closed.
所以你应该能够做到:
approx = cv2.approxPolyDP(contour, epsilon, closed=False)
最后还是没有找到可以直接在OpenCV中使用的方法。但是我找到了一个算法(命名为Ramer-Douglas-Peucker算法)只需要一点代码就可以逼近曲线。
见https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
见https://www.sciencedirect.com/science/article/abs/pii/0167865594900027
这是在 Python/OpenCV 中使用 cv2.approxPolyDP
执行此操作的方法输入(截屏标题栏)
import numpy as np
import cv2
# read input
img = cv2.imread('curve.png')
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)