【发布时间】:2021-01-26 17:15:22
【问题描述】:
我有各种由 cv2.findContours(cv2.CHAINE_APPROX_NONE) 提取的轮廓。之后我还提取了极值点。现在我想找到轮廓中与最左边/最右边相对的点。可以说,轮廓中与最左/最右点具有相同 y 坐标的点。由于大多数函数很难读取坐标的格式,因此诀窍可能是重新排序。
知道怎么解决吗?
###contour extraction
cnts = cv2.findContours(img_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cnts = cnts[0]
(cnts, _) = contours.sort_contours(cnts)
cnt_number = 0
for cnt in cnts:
if cv2.contourArea(cnt) > 500:
contour_lenght = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.005 * contour_lenght, True)
obj_cor = len(approx)
if obj_cor > 4:
cv2.drawContours(blank_image, cnt, -1, (0, 0, 0), 2)
leftmost = tuple(cnt[cnt[:, :, 0].argmin()][0])
rightmost = tuple(cnt[cnt[:, :, 0].argmax()][0])
还想进行线轮廓交点(就像 math.coffee 在这里 Line intersecting contour in openCv 建议的那样),但不确定如何...
###bounding box
x, y, w, h = cv2.boundingRect(approx)
###end of line from leftmost
left_y = leftmost[1]
left_x = leftmost[0] + w
left_line_endpoint = (left_x, left_y)
###line intersect function suggested by mathematical.coffee
numpy.logical_and(...)
【问题讨论】:
标签: python opencv line contour point