【发布时间】:2016-10-11 21:17:56
【问题描述】:
是否有可能从一组沿具有粗曲率的线的点创建一个多边形,以便在两个曲率值之间选择这些点?
我正在尝试使用 python 的 opencv 包 (cv2) 从给定图像中检索近似的曲线四边形。
例如: 给定边缘检测后的图像,如下所示:
在使用 cv2.findContours 找到轮廓后,如下所示:
(旁注:如果这实际上会给出一个方形而不是绕线,那就太好了 - 还需要一种算法来关闭该图像右侧形状中的间隙。膨胀/erosion 可能会起作用,但可能会删除某些可能希望保留的功能。)
之后,我们可以像这样在轮廓上使用 polyDPApprox:
但是,这与曲率无关 - 它只是通过使用与线条的最大偏差来进行近似。如果我们想省略一些精细的细节(想法是这些可能来自错误)并保留曲率较小的点(宽的形状) - 我们可以使用函数来提供这样的东西吗?:
(红色填充只是表示形状将被封闭成曲线四边形。)
这是用于分析输入图像以防万一有人需要的函数:
# input binary image
def find_feature_points(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.namedWindow('img', WINDOW_NORMAL)
cv2.imshow("img", gray)
cv2.waitKey(0)
contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw contours to image
print contours
copy = img.copy()
# img - Image.
# pts - Array of polygonal curves.
# npts - Array of polygon vertex counters.
# ncontours - Number of curves.
# isClosed - Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first vertex.
# color - Polyline color.
# thickness - Thickness of the polyline edges.
# lineType - Type of the line segments. See the line() description.
# shift - Number of fractional bits in the vertex coordinates.
cv2.polylines(img=copy, pts=contours, isClosed=1, color=(0,0,255), thickness=3)
cv2.namedWindow('contour', WINDOW_NORMAL)
cv2.imshow("contour", copy)
cv2.waitKey(0)
# Find approximation to contours
approx_conts = []
for c in contours:
curve = c
epsilon = 200
closed = True
approx_conts.append(cv2.approxPolyDP(curve, epsilon, closed))
# draw them
cv2.drawContours(img, approx_conts, -1, (0, 255, 0), 3)
cv2.namedWindow('approx', WINDOW_NORMAL)
cv2.imshow("approx", img)
cv2.waitKey(0)
return
【问题讨论】:
标签: python opencv math image-processing edge-detection