opencv 如何从OMR表中获得标记的答案?

a8jjtwal  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(152)

我正在试着从一张OMR纸上找出标记的答案。我已经能够发现图像上的黑圈(请看它周围的绿色方块),但我现在卡住了。如何从图像中找到确切的数字。下面是图片:

我希望输出为以下格式:2,3,0,4,0,5,0,0,1,0,1,0,0.
我不想把它和任何答案做比较。我只希望输出在上面的格式。
这就是我到目前为止所做的:

for contour in questionCnts:
    # Calculate the area of the contour
    area = cv2.contourArea(contour)

    # If the area is above a certain threshold, consider it as a marked bubble
    if area > 100:  # Adjust the threshold based on your specific bubble size

        # Calculate the centroid of the contour
        moments = cv2.moments(contour)
        centroid_x = int(moments['m10'] / moments['m00'])
        centroid_y = int(moments['m01'] / moments['m00'])

        # Add the centroid coordinates to the list of marked bubbles
        marked_bubbles.append((centroid_x, centroid_y))

# Print the coordinates of the marked bubbles
for bubble in marked_bubbles:
    print(f"Marked bubble at coordinates: {bubble}")
# Draw circles at the coordinates of the marked bubbles on the image
draw = ImageDraw.Draw(image)
for bubble in marked_bubbles:
    if(bubble == (1, 1)):
      continue
    draw.rectangle([(bubble[0] - 10, bubble[1] - 10), (bubble[0] + 10, bubble[1] + 10)], outline='green')

# Save the modified image
image.save('/content/output_image.png')

Marked bubble at coordinates: (279, 11)
Marked bubble at coordinates: (346, 11)
Marked bubble at coordinates: (13, 28)
Marked bubble at coordinates: (46, 45)
Marked bubble at coordinates: (113, 61)
Marked bubble at coordinates: (179, 78)
Marked bubble at coordinates: (413, 161)
Marked bubble at coordinates: (379, 161)
Marked bubble at coordinates: (312, 161)
Marked bubble at coordinates: (246, 161)
Marked bubble at coordinates: (213, 161)
Marked bubble at coordinates: (146, 161)
Marked bubble at coordinates: (80, 161)

任何帮助都将不胜感激。多谢了。
编辑:我已经添加了坐标。

编辑二:

正如注解中所建议的,我已经更新了代码。我正在获取值,但所有值都不正确。以下是我所做的:

box_size = 38

# Iterating over the coordinates
for x, y in marked_bubbles:
    # Determining the column based on the X value
    column = int(x / box_size) + 1  # Addding 1 because index starts from 0

    # Determine the score based on the quantized Y value of the box
    score = int(y / box_size) # Rounding off to nearest integer

    # Print the result
    print(f"Centroid: ({x}, {y}), Column: {column}, Score: {score}")

我试过玩box_size值,但它不给正确的。下面是输出:

Centroid: (13, 28), Column: 1, Score: 0
Centroid: (46, 45), Column: 2, Score: 1
Centroid: (80, 161), Column: 3, Score: 4
Centroid: (113, 61), Column: 3, Score: 1
Centroid: (146, 161), Column: 4, Score: 4
Centroid: (179, 78), Column: 5, Score: 2
Centroid: (213, 161), Column: 6, Score: 4
Centroid: (246, 161), Column: 7, Score: 4
Centroid: (279, 11), Column: 8, Score: 0
Centroid: (312, 161), Column: 9, Score: 4
Centroid: (346, 11), Column: 10, Score: 0
Centroid: (379, 161), Column: 10, Score: 4
Centroid: (413, 161), Column: 11, Score: 4

我的实施是否正确?还是我错过了什么?谢谢

w8f9ii69

w8f9ii691#

给你这甚至不是几何,只是移动数字。在此功能中,进给标记的 * 中心 *,而不是其左边缘。

num_columns = 13
def columm_from_x(x):
    return int(x/image_width * num_columns + 1.0)

您可以很容易地应用相同的思想来确定气泡在其列中的位置。

相关问题