【问题标题】:How to make sequential contour in opencv-python from left to right如何在opencv-python中从左到右制作顺序轮廓
【发布时间】:2015-03-14 17:41:49
【问题描述】:

在opencv-python中使用轮廓分割手写数字后,它给出了一个随机输出轮廓。如何获得一个从左到右和从上到下的顺序?

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

【问题讨论】:

  • 请解释为什么你需要一个特殊的排序
  • 我让你的一个标签更具体,并重写了正文以更清晰。

标签: python opencv text-segmentation opencv-contour


【解决方案1】:

您可以使用以下代码对轮廓进行排序:

def sort_contours(cnts, method="left-to-right"):
# initialize the reverse flag and sort index
reverse = False
i = 0

# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
    reverse = True

# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
    i = 1

# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
    key=lambda b:b[1][i], reverse=reverse))

# return the list of sorted contours and bounding boxes
return (cnts)

【讨论】:

    猜你喜欢
    • 2015-01-24
    • 1970-01-01
    • 2016-12-12
    • 2021-05-08
    • 2021-08-08
    • 2021-09-07
    • 2022-09-24
    • 2016-12-03
    • 2016-09-25
    相关资源
    最近更新 更多