Python 如何从左到右绘制答题纸

Python 如何从左到右绘制答题纸,python,opencv-contour,omr,Python,Opencv Contour,Omr,帮助 如何处理此代码 我想从答题纸的上下然后从左到右回答检查 但这段代码不包括左-右和上-下 for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)): # sort the contours for the current question from # left to right, then initialize the index of the # bubbled answer cnts = co

帮助 如何处理此代码 我想从答题纸的上下然后从左到右回答检查 但这段代码不包括左-右和上-下

for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)):
    # sort the contours for the current question from
    # left to right, then initialize the index of the
    # bubbled answer
    cnts = contours.sort_contours(questionCnts[i:i + 5])[0]
    bubbled = None  


    # loop over the sorted contours
    for (j, c) in enumerate(cnts):
        # construct a mask that reveals only the current
        # "bubble" for the question
        mask = np.zeros(thresh.shape, dtype="uint8")
        cv2.drawContours(mask, [c], -1, 255, -1)

        # apply the mask to the thresholded image, then
        # count the number of non-zero pixels in the
        # bubble area
        mask = cv2.bitwise_and(thresh, thresh, mask=mask)
        total = cv2.countNonZero(mask)

        # if the current total has a larger number of total
        # non-zero pixels, then we are examining the currently
        # bubbled-in answer
        if bubbled is None or total > bubbled[0]:
            bubbled = (total, j)

    # initialize the contour color and the index of the
    # *correct* answer
    color = (0, 0, 255)
    k = ANSWER_KEY[q]

    # check to see if the bubbled answer is correct
    if k == bubbled[1]:
        color = (0, 255, 0)
        correct += 1

    # draw the outline of the correct answer on the test
    cv2.drawContours(paper, [cnts[k]], -1, color, -1)
您将在img1中看到答案表,但真正的答案是img2img2中答案1是A 答案2是2,但现在在img1中答案11是2
在开始对所有轮廓进行迭代之前,如何根据y坐标对所有轮廓进行排序。 然后开始以10个批次(每行)进行迭代,并根据x坐标对每个批次进行排序。 这样,您将以正确的顺序获得每个轮廓