0

帮助 我如何处理这个代码我想从答题卡上下然后左右回答检查,但是这个代码是左右和上下

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)

img 1 answerSheet img 2 answer 你会在img1中看到答案表,但真正的答案是img2中的img2答案 1 是 A 答案 2 是 2 但现在在img1中答案 11 是 2 我该怎么办

4

1 回答 1

0

在开始迭代所有轮廓之前,根据 y 坐标对所有轮廓进行排序。然后开始以 10 个批次(每行)进行迭代,并根据 x 坐标对每个批次进行排序。这样,您将以正确的顺序获得每个轮廓。

于 2019-10-12T19:11:18.627 回答